Unnamed: 0 id diet pulse time kind 0 0 1 low fat 85 1 min rest 1 1 1 low fat 85 15 min rest 2 2 1 low fat 88 30 min rest 3 3 2 low fat 90 1 min rest 4 4 2 low fat 92 15 min rest no fat 45 low fat 45 Name: diet, dtype: int64
Now, we can use the following Python code to perform ordinal encoding on the diet column.
import seaborn
from sklearn.preprocessing import OrdinalEncoder
df = seaborn.load_dataset("exercise")
print(df.head())
print(df.diet.value_counts())
ordinal_encoder = OrdinalEncoder()
df[["diet"]] = ordinal_encoder.fit_transform(df[["diet"]])
print(df.diet.value_counts())
Here, we are using the OrdinalEncoder class from the sklearn.preprocessing module to perform ordinal encoding. The ordinal_encoder.fit_transform(df[[“diet”]]) function, learns about the ordinal data from the diet column of the dataset and then, it encodes the data and transforms it.
After the ordinal encoding is done, we can see the diet columns contain only values 0.0 and 1.0. So, the output of the above program will be:
Unnamed: 0 id diet pulse time kind 0 0 1 low fat 85 1 min rest 1 1 1 low fat 85 15 min rest 2 2 1 low fat 88 30 min rest 3 3 2 low fat 90 1 min rest 4 4 2 low fat 92 15 min rest no fat 45 low fat 45 Name: diet, dtype: int64 0.0 45 1.0 45 Name: diet, dtype: int64
Please note that by default the desired data type of the output is numpy.float64. So, we get the numbers 0.0 and 1.0 as the encoded values of the diet column.








































0 Comments