Regressão Logística

Machine learning com PySpark

Andrew Collier

Data Scientist, Fathom Data

Curva Logística

Uma curva logística.

Machine learning com PySpark

Curva Logística

Uma curva logística com sombreamento acima do limite

Machine learning com PySpark

Curva Logística

Uma curva logística com sombreamento abaixo do limite

Machine learning com PySpark

Curva Logística

Uma curva logística deslocada para a direita

Machine learning com PySpark

Curva Logística

Uma curva logística deslocada para a esquerda

Machine learning com PySpark

Curva Logística

Uma curva logística com transição gradual

Machine learning com PySpark

Curva Logística

Uma curva logística com transição rápida

Machine learning com PySpark

Carros, revisitando

Preparar para modelagem:

  • juntar os preditores em uma única coluna (chamada features) e
  • dividir os dados em conjuntos de treino e teste.
+---+----+------+------+----+-----------+----------------------------------+-----+
|cyl|size|mass  |length|rpm |consumption|features                          |label|
+---+----+------+------+----+-----------+----------------------------------+-----+
|6  |3.0 |1451.0|4.775 |5200|9.05       |[6.0,3.0,1451.0,4.775,5200.0,9.05]|1.0  |
|4  |2.2 |1129.0|4.623 |5200|6.53       |[4.0,2.2,1129.0,4.623,5200.0,6.53]|0.0  |
|4  |2.2 |1399.0|4.547 |5600|7.84       |[4.0,2.2,1399.0,4.547,5600.0,7.84]|1.0  |
|4  |1.8 |1147.0|4.343 |6500|7.84       |[4.0,1.8,1147.0,4.343,6500.0,7.84]|0.0  |
|4  |1.6 |1111.0|4.216 |5750|9.05       |[4.0,1.6,1111.0,4.216,5750.0,9.05]|0.0  |
+---+----+------+------+----+-----------+----------------------------------+-----+
Machine learning com PySpark

Construir um modelo de Regressão Logística

from pyspark.ml.classification import LogisticRegression

Crie um classificador de Regressão Logística.

logistic = LogisticRegression()

Aprenda com os dados de treino.

logistic = logistic.fit(cars_train)
Machine learning com PySpark

Previsões

prediction = logistic.transform(cars_test)
+-----+----------+---------------------------------------+
|label|prediction|probability                            |
+-----+----------+---------------------------------------+
|0.0  |0.0       |[0.8683802216422138,0.1316197783577862]|
|0.0  |1.0       |[0.1343792056399585,0.8656207943600416]|
|0.0  |0.0       |[0.9773546766387631,0.0226453233612368]|
|1.0  |1.0       |[0.0170508265586195,0.9829491734413806]|
|1.0  |0.0       |[0.6122241729292978,0.3877758270707023]|
+-----+----------+---------------------------------------+
Machine learning com PySpark

Precisão e revocação

Quão bem o modelo funciona nos dados de teste?

Consulte a matriz de confusão.

+-----+----------+-----+
|label|prediction|count|
+-----+----------+-----+
|  1.0|       1.0|    8| - VP (verdadeiro positivo)
|  0.0|       1.0|    4| - FP (falso positivo)
|  1.0|       0.0|    2| - FN (falso negativo)
|  0.0|       0.0|   10| - VN (verdadeiro negativo)
+-----+----------+-----+
# Precisão (positivo)
TP / (TP + FP)
0.6666666666666666
# Revocação (positivo)
TP / (TP + FN)
0.8
Machine learning com PySpark

Métricas ponderadas

from pyspark.ml.evaluation import MulticlassClassificationEvaluator

evaluator = MulticlassClassificationEvaluator()

evaluator.evaluate(prediction, {evaluator.metricName: 'weightedPrecision'})
0.7638888888888888

Outras métricas:

  • weightedRecall
  • accuracy
  • f1
Machine learning com PySpark

ROC e AUC

Uma curva ROC

ROC = "Receiver Operating Characteristic"

  • VP versus FP
  • limiar = 0 (canto superior direito)
  • limiar = 1 (canto inferior esquerdo)

AUC = "Área sob a curva"

  • idealmente AUC = 1
Machine learning com PySpark

Vamos fazer Regressão Logística!

Machine learning com PySpark

Preparing Video For Download...