How to convert Categorical features to Numerical Features in Python?
It’s difficult to create machine learning models that can’t have features that have categorical values, such models cannot function. categorical variables have string-type values. thus we have to convert string values to numbers. This can be accomplished by creating new features based on the categories and setting values to them. In this article, we are going to see how to convert Categorical features to Numerical Features in Python
Step 1: Import the necessary packages and modules
Step 2: Import the CSV file
Step 3: Get all features with categorical values
We use df.info() to find categorical features. Categorical features have Dtype as “object”.
In the given database columns “origin” and “name” is object type.
Step 4: Convert string values of origin column to numerical values
We will fit the “origin” column using preprocessing.LabelEncoder().fit() method.
Step 5: Get the unique values out of the categorical features
We will use label_encoder.classes_ attribute for this purpose.
classes_:ndarray of shape (n_classes,)
Holds the label for each class.
['europe', 'japan', 'usa']
Step 6: Transforming the categorical values