Ajuster les données

Feature Engineering avec PySpark

John Hogue

Lead Data Scientist, General Mills

Pourquoi transformer les données ?

Transformation

Feature Engineering avec PySpark

Qu'est-ce que la mise à l'échelle MinMax

Mise à l'échelle MinMax

Feature Engineering avec PySpark

Mise à l'échelle MinMax

# define min and max values and collect them
max_days = df.agg({'DAYSONMARKET': 'max'}).collect()[0][0]
min_days = df.agg({'DAYSONMARKET': 'min'}).collect()[0][0]

# create a new column based off the scaled data df = df.withColumn("scaled_days", (df['DAYSONMARKET'] - min_days) / (max_days - min_days))
df[['scaled_days']].show(5)
+--------------------+
|         scaled_days|
+--------------------+
|0.044444444444444446|
|0.017777777777777778|
| 0.12444444444444444|
| 0.08444444444444445|
| 0.09333333333333334|
+--------------------+
only showing top 5 rows
Feature Engineering avec PySpark

Qu'est-ce que la standardisation ?

Transformer vers une loi normale standard

  • z = (x - μ)/ σ
  • Moyenne μ de 0
  • Écart-type σ de 1

Standardisation

Feature Engineering avec PySpark

Standardisation

mean_days = df.agg({'DAYSONMARKET': 'mean'}).collect()[0][0]
stddev_days = df.agg({'DAYSONMARKET': 'stddev'}).collect()[0][0]

# Create a new column with the scaled data df = df.withColumn("ztrans_days", (df['DAYSONMARKET'] - mean_days) / stddev_days)
df.agg({'ztrans_days': 'mean'}).collect()
[Row(avg(ztrans_days)=-3.6568525985103407e-16)]
df.agg({'ztrans_days': 'stddev'}).collect()
[Row(stddev(ztrans_days)=1.0000000000000009)]
Feature Engineering avec PySpark

Qu'est-ce que la mise à l'échelle logarithmique

Distribution non mise à l'échelle

Courbe de distribution brute

Distribution en échelle logarithmique

Courbe de distribution en log

Feature Engineering avec PySpark

Mise à l'échelle logarithmique

# import the log function
from pyspark.sql.functions import log
# Recalculate log of SALESCLOSEPRICE
df = df.withColumn('log_SalesClosePrice', log(df['SALESCLOSEPRICE']))
Feature Engineering avec PySpark

Passons à la pratique !

Feature Engineering avec PySpark

Preparing Video For Download...