Logistic Regression
Setting Up a Logistic Regression Classifier
Load in required libraries
from pyspark.ml.classification import LogisticRegression
from pyspark.ml.tuning import ParamGridBuilder, CrossValidator
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.mllib.evaluation import BinaryClassificationMetrics
Initialize Logistic Regression object
lr = LogisticRegression(labelCol="label", featuresCol="features")
Create a parameter grid for tuning the model
lrparamGrid = (ParamGridBuilder()
.addGrid(lr.regParam, [0.01, 0.1, 0.5, 1.0, 2.0])
.addGrid(lr.elasticNetParam, [0.0, 0.25, 0.5, 0.75, 1.0])
.addGrid(lr.maxIter, [1, 5, 10, 20, 50])
.build())
Define how you want the model to be evaluated
lrevaluator = BinaryClassificationEvaluator(rawPredictionCol="rawPrediction", metricName = "areaUnderROC")
Define the type of cross-validation you want to perform
# Create 5-fold CrossValidator
lrcv = CrossValidator(estimator = lr,
estimatorParamMaps = lrparamGrid,
evaluator = lrevaluator,
numFolds = 5)
Fit the model to the data
lrcvModel = lrcv.fit(train)
print(lrcvModel)
Score the testing dataset using your fitted model for evaluation purposes
lrpredictions = lrcvModel.transform(test)
Evaluate the model
print('Accuracy:', lrevaluator.evaluate(lrpredictions))
print('AUC:', BinaryClassificationMetrics(lrpredictions['label','prediction'].rdd).areaUnderROC)
print('PR:', BinaryClassificationMetrics(lrpredictions['label','prediction'].rdd).areaUnderPR)
Last updated
Was this helpful?