Multivariate Linear Regression in Python – Step 2.) Applying LabelEncoder and OneHotEncoder

with No Comments

#Import libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#Import data
dataset = pd.read_csv(‘multivariate_data.csv’)
x = dataset.iloc[:,:-1].values
y =dataset.iloc[:,4].values

#Encode Categorical Data using LabelEncoder and OneHotEncoder
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder_x=LabelEncoder()
x[:,3]=labelencoder_x.fit_transform(x[:,3])
onehotencoder=OneHotEncoder(categorical_features =[3])
x=onehotencoder.fit_transform(x).toarray()

#Remove Dummy Variable Trap
x=x[:, 1:]

Leave a Reply