Créer un modèle

Feature Engineering avec PySpark

John Hogue

Lead Data Scientist, General Mills

RandomForestRegressor

Paramètres de base du modèle

  • featuresCol="features"
  • labelCol="label"
  • predictionCol="prediction"
  • seed=None

Valeurs de nos paramètres

  • featuresCol="features"
  • labelCol="SALESCLOSEPRICE"
  • predictionCol="Prediction_Price"
  • seed=42
Feature Engineering avec PySpark

Entraîner une forêt aléatoire

from pyspark.ml.regression import RandomForestRegressor
# Initialiser le modèle avec les colonnes à utiliser
rf = RandomForestRegressor(featuresCol="features",
                           labelCol="SALESCLOSEPRICE",
                           predictionCol="Prediction_Price",
                           seed=42
                           )
# Entraîner le modèle
model = rf.fit(train_df)
Feature Engineering avec PySpark

Prédire avec un modèle

# Faire des prédictions
predictions = model.transform(test_df)
# Inspecter les résultats
predictions.select("Prediction_Price", "SALESCLOSEPRICE").show(5)
+------------------+---------------+
|  Prediction_Price|SALESCLOSEPRICE|
+------------------+---------------+
|426029.55463222397|         415000|
| 708510.8806005502|         842500|
| 164275.7116183204|         161000|
| 208943.4143642175|         200000|
|217152.43272221283|         205000|
+------------------+---------------+
only showing top 5 rows
Feature Engineering avec PySpark

Évaluer un modèle

from pyspark.ml.evaluation import RegressionEvaluator
# Sélectionner les colonnes pour calculer l'erreur de test
evaluator = RegressionEvaluator(labelCol="SALESCLOSEPRICE", 
                                predictionCol="Prediction_Price")
# Créer les métriques d'évaluation
rmse = evaluator.evaluate(predictions, {evaluator.metricName: "rmse"})
r2 = evaluator.evaluate(predictions, {evaluator.metricName: "r2"})
# Afficher les métriques du modèle
print('RMSE: ' + str(rmse))
print('R^2: ' + str(r2))
RMSE: 22898.84041072095
R^2: 0.9666594402208077
Feature Engineering avec PySpark

Modélisons des données !

Feature Engineering avec PySpark

Preparing Video For Download...