In [ ]:
from sklearn.preprocessing import LabelEncoder
import joblib
label_encoder = LabelEncoder()
iris_df["encoded.variety"] = label_encoder.fit_transform(iris_df["variety"])
iris_df.sort_values("sepal.width", inplace=True)
print("Regola di mappatura:")
print(
dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))
)
# Salva il label encoder
joblib.dump(label_encoder, "encoder")
iris_df.head()
Regola di mappatura:
{'Setosa': 0, 'Versicolor': 1, 'Virginica': 2}
Out[ ]:
| sepal.length | sepal.width | petal.length | petal.width | variety | encoded.variety | |
|---|---|---|---|---|---|---|
| 60 | 5.0 | 2.0 | 3.5 | 1.0 | Versicolor | 1 |
| 62 | 6.0 | 2.2 | 4.0 | 1.0 | Versicolor | 1 |
| 119 | 6.0 | 2.2 | 5.0 | 1.5 | Virginica | 2 |
| 68 | 6.2 | 2.2 | 4.5 | 1.5 | Versicolor | 1 |
| 41 | 4.5 | 2.3 | 1.3 | 0.3 | Setosa | 0 |