Convert Categorical Variable To Numeric Python Sklearn Code Example


Example 1: pandas categorical to numeric


#this will label the different catagories as 0,1,2,3....
dataset["sex"] = dataset["sex"].astype('category').cat.codes

Example 2: transform categorical variables python


from sklearn.preprocessing import LabelEncoder

lb_make = LabelEncoder()
obj_df["make_code"] = lb_make.fit_transform(obj_df["make"])
obj_df[["make", "make_code"]].head(11)

Example 3: how to convert categorical data to binary data in python


MedInc        False
HouseAge False
AveRooms False
AveBedrms False
Population False
AveOccup False
Latitude False
Longitude False
Price False
dtype: bool

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable