Logistic Regression using Mahout

15
Logistic Regression Logistic Regression Using Using Apache Mahout Apache Mahout CS 267 : Data Mining Presentation Guided by : Dr. Tran -Tanuvir Singh

description

A presentation on using the Logistic regression model on mahout.

Transcript of Logistic Regression using Mahout

Page 1: Logistic Regression using Mahout

Logistic RegressionLogistic RegressionUsingUsing

Apache MahoutApache Mahout

CS 267 : Data Mining PresentationGuided by : Dr. Tran

-Tanuvir Singh

Page 2: Logistic Regression using Mahout

OutlineOutline• Logistic Regression Model• Mahout Overview• Logistic Regression using Mahout• Training and Testing ML Model• Demo

Page 3: Logistic Regression using Mahout

Logistic Regression Logistic Regression ModelModel

• Logistic regression is a model used for prediction of the probability of occurrence of an event. It makes use of several predictor variables that may be either numerical or categories.

• It is used to predict a binary response from a binary predictor, used for predicting the outcome of a categorical dependent variable (i.e., a class label) based on one or more predictor variables (features).

• Logistic regression is the standard industry workhorse that underlies many production fraud detection and advertising quality and targeting products.

Page 4: Logistic Regression using Mahout

MahoutMahout• Apache Mahout is a project of the Apache

Software Foundation to produce free implementations of distributed or otherwise scalable machine learning algorithms focused primarily in the areas of collaborative filtering, clustering and classification.

• The advantage of Mahout over other approaches becomes striking as the number of training examples gets extremely large. What large means can vary enormously.

• As the input exceeds 1 to 10 million training examples, something scalable like Mahout is needed.

Page 5: Logistic Regression using Mahout

Logistic Regression Using MahoutLogistic Regression Using Mahout• Mahout’s implementation of Logistic regression

uses Stochastic Gradient Descent (SGD) algorithm

• This algorithm is a sequential (nonparallel) algorithm, but it’s fast.

• While working with large data, the SGD algorithm uses a constant amount of memory regardless of the size of the input.

• Mahout includes a command line example of logistic regression program.

• For production use, the logistic regression stuff mostly is not run from the command line, but is integrated more tightly into some data flow.

Page 6: Logistic Regression using Mahout

• Mahout’s implementation of Logistic Regression using SGD supports the following command line program names:

Valid program names are:

ocat : Print a file or resource as the logistic regression models would see it

orunlogistic : Run a logistic regression model against CSV data

otrainlogistic : Train a logistic regression using stochastic gradient descent

Logistic Regression Using MahoutLogistic Regression Using Mahout

Page 7: Logistic Regression using Mahout

BUILDING THE MODEL

You can build a model to determine the REQUIRED field from the selected features with thefollowing command:

$ bin/mahout trainlogistic --input Input.csv \--output ./model \--target TARGETVARIABLE--categories 2 \--predictors Predictor1 Predictor2 . . . --types numeric \--features 20 --passes 100 --rate 50

......

This command specifies that the input comes from the resource named Input.csv,that the resulting model is stored in the file ./model, that the target variable is in thefield named TARGETVARIABLE and that it has two possible values. The command also specifies that the algorithm should use variables x and y as predictors, both with numerical types. The remaining options specify internal parameters for the learning algorithm.

Logistic Regression Using MahoutLogistic Regression Using Mahout

Page 8: Logistic Regression using Mahout

Handle What it does--quiet Produces less status and progress output.--input <file-or-resource> Uses the specified file or resource as input.--output <file-for-model> Puts the model into the specified file.--target <variable> Uses the specified variable as the target.--categories <n> Specifies how many categories the target variable has.--predictors <v1> ... <vn> Specifies the names of the predictor variables.

--types

<t1> ... <tm> Gives a list of the types of the predictor variables. Each type should be one of numeric, word, or text. Types can be abbreviated to their first letter. If too few types are given, the last one is used again as necessary. Use word for categorical variables.

--passes

Specifies the number of times the input data should be reexamined during training. Small input files may need to be examined dozens of times. Very large input files probably don’t even need to be completely examined.

--lambda Controls how much the algorithm tries to eliminate variables from the final model. A value of 0 indicates no effort is made. Typical values are on the order of 0.00001 or less.

--rate Sets the initial learning rate. This can be large if you have lots of data or use lots of passes because it’s decreased progressively as data is examined.

--noBias

Eliminates the intercept term (a built-in constant predictor variable) from the model. Occasionally this is a good idea, but generally it isn’t since the SGD learning algorithm can usually eliminate the intercept term if warranted.

--features Sets the size of the internal feature vector to use in building the model. A larger value here can be helpful, especially with text-like input data.

Logistic Regression Using MahoutLogistic Regression Using Mahout Handles for Trainlogistic:

Page 9: Logistic Regression using Mahout

EVALUATING THE MODEL

You can evaluate the model by running it on the same training dataset as:

$ bin/mahout runlogistic --input test.csv --model ./model --auc --confusion......

This command takes as input the model created in our trainlogistic command and produces the Area Under the Curve and Confusion Matrix.for the learning algorithm.

Logistic Regression Using MahoutLogistic Regression Using Mahout

Handle What it does--quiet Produces less status and progress output.--auc Prints AUC score for model versus input data after reading data.--scores Prints target variable value and scores for each input example.--confusion Prints confusion matrix for a particular threshold (see --threshold).--input <input> Reads data records from specified file or resource.--model <model> Reads model from specified file.

Page 10: Logistic Regression using Mahout

Demo - DatasetDemo - Dataset• Let’s take an example data set:

"x","y","shape","color","k","k0","xx","xy","yy","a","b","c","bias"0.923307513352484,0.0135197141207755,21,2,4,8,0.852496764213146,...,10.711011884035543,0.909141522599384,22,2,3,9,0.505537899239772,...,1. . .0.67132937326096,0.571220482233912,23,1,5,2,0.450683127402953,...,10.548616112209857,0.405350996181369,24,1,5,3,0.300979638576258,...,10.677980388281867,0.993355110753328,25,2,3,9,0.459657406894831,...,1

• Now we will run our Logistic regression model to predict the value of Color based on the predictors"x","y","shape","k","k0","xx","xy","yy","a","b","c","bias"

Page 11: Logistic Regression using Mahout

Demo – Dataset Demo – Dataset ExplainedExplained

Variable Description Possible values

x The x coordinate of a point Numerical from 0 to 1

y The y coordinate of a point Numerical from 0 to 1

shape The shape of a point Shape code from 21 to 25

color Whether the point is filled or not 1=empty, 2=filled

k The k-means cluster ID derived using only x and yInteger cluster code from 1 to 10

k0 The k-means cluster ID derived using x, y, and color Integer cluster code from 1 to 10

xx The square of the x coordinate Numerical from 0 to 1

xy The product of the x and y coordinates Numerical from 0 to 1

yy The square of the y coordinate Numerical from 0 to 1

a The distance from the point (0,0) Numerical from 0 to

b The distance from the point (1,0) Numerical from 0 to

c The distance from the point (0.5,0.5) Numerical from 0 to

bias A constant 1

Page 12: Logistic Regression using Mahout

Demo – Training the Demo – Training the ModelModel

• To determine the color field from the x and y features with the following command:

$ bin/mahout trainlogistic --input color.csv \--output ./model \--target color --categories 2 \--predictors x y --types numeric \--features 20 --passes 100 --rate 50

...• After the model is trained, you can run the model on the training data

again to evaluate how it does (even though we know it won’t do well).$ bin/mahout runlogistic --input donut.csv --model ./model \--auc --confusionAUC = 0.57confusion: [[27.0, 13.0], [0.0, 0.0]]

• AUC can range from 0 to 0.5 for a model that’s no better than random to 1 for a perfect model.

• The value here of 0.57 indicates a model that’s hardly better than random.

Page 13: Logistic Regression using Mahout

Demo – Selecting Demo – Selecting FeaturesFeatures

• You can get more interesting results if you use additional variables for training. For instance, this command allows the model to use x and y as well as a, b, and c to build the model:

$ bin/mahout trainlogistic --input .colorcsv --output model \--target color --categories 2 \--predictors x y a b c --types numeric \ --features 20 --passes 100 --rate 50

• If you run this revised model on the training data, you’ll get a very different result than with the previous model:

$ bin/mahout runlogistic --input color.csv --model model \--auc –confusion

Result:AUC = 1.00confusion: [[27.0, 0.0], [0.0, 13.0]]entropy: [[-0.1, -1.5], [-4.0, -0.2]]

• Now you have a perfect value of 1 for AUC, and you can see from the confusion matrix that the model was able to classify all of the training examples perfectly.

Page 14: Logistic Regression using Mahout

Demo – EvaluationDemo – Evaluation• You can get more interesting results if you use additional variables

for training. Now you can run this same model on additional data in the donut-test.csv resource.

• Because this new data was not used to train the model, there are a few surprises in store for the model.

$ bin/mahout runlogistic --input color-test.csv --model model \--auc –confusion

ResultAUC = 0.97confusion: [[24.0, 2.0], [3.0, 11.0]]entropy: [[-0.2, -2.8], [-4.1, -0.1]]

• On this withheld data, you can see that the AUC has dropped to 0.97, which is still quite good.

Page 15: Logistic Regression using Mahout

ReferenceReference• https://

mahout.apache.org/users/classification/logistic-regression.html• https://blog.trifork.com/2014/02/04/an-introduction-to-mahouts-

logistic-regression-sgd-classifier/• For incorporating your code by using the libraries:

http://skife.org/mahout/2013/02/14/first_steps_with_mahout.html• Mahout in Action

Sean Owen, Robin Anil, Ted Dunning, and Ellen Friedman