Clear all

Categorical variables in linear regression

1 Posts
2 Users
0 Reactions
Topic starter

My dataset has categorical variables. How do I include them in a linear regression model in scikit-learn?

1 Answer

Categorical variables are non-numeric and cannot be directly used in linear regression. They need to be transformed into a format suitable for numerical analysis. Try convert them into numerical values using one hot encoding using sklearn. Identify which features in your dataset are categorical. These could be variables like "Gender," "City," or "Product Type." One-hot encoding is a method to represent categorical variables as binary vectors (0s and 1s). For each category in a variable, a new binary column is created. This is how you can do it using sklearn. 

import pandas as pd
from sklearn.preprocessing import OneHotEncoder

# Create a sample DataFrame
data = {'City': ['New York', 'San Francisco', 'Chicago']}
df = pd.DataFrame(data)

# Apply One-Hot Encoding
encoder = OneHotEncoder()
encoded_data = encoder.fit_transform(df[['City']])


After one-hot encoding, you'll get a sparse matrix with binary values representing the presence of each category. In this example, 'New York,' 'San Francisco,' and 'Chicago' would each have their binary column. After one-hot encoding, you can concatenate the new binary columns with the original dataset. 

# Concatenate with original DataFrame
df_encoded = pd.concat([df, pd.DataFrame(encoded_data.toarray(), columns=encoder.get_feature_names_out(['City']))], axis=1)

You can drop the original categorical column as it is no longer needed.

When using one-hot encoding, be mindful of the dummy variable trap. This occurs when the values of one variable can be predicted from the values of the others. To avoid this, drop one of the binary columns for each categorical variable. When working with more complex workflows, consider using scikit-learn pipelines, which streamline the process of data preprocessing and modeling.

This post was modified 6 months ago by Neuraldemy Support

Please close the topic if your issue has been resolved. Add comments to continue adding more context or to continue discussion and add answer only if it is the answer of the question.
Neuraldemy Support Team | Enroll In Our ML Tutorials

Welcome Back!

Login to your account below

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.

Retrieve your password

Please enter your username or email address to reset your password.

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?