Reputation: 149
Google colab
This is code for Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 2] = labelencoder_X_1.fit_transform(X[:, 2]) #For month
labelencoder_X_2 = LabelEncoder()
X[:, 3] = labelencoder_X_2.fit_transform(X[:, 3]) #For weekday
onehotencoder = OneHotEncoder(categorical_features = [2])#dummy variable for month
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]
onehotencoder = OneHotEncoder(categorical_features = [13])#dummy variable for week
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]
I am getting this error for this and I am facing following errors here. I have already update all library from anaconda prompt. But can't findout the solution of the problem.
TypeError Traceback (most recent call last)
<ipython-input-18-faafd78b922d> in <module>()
4 labelencoder_X_2 = LabelEncoder()
5 X[:, 3] = labelencoder_X_2.fit_transform(X[:, 3]) #For weekday
----> 6 onehotencoder = OneHotEncoder(categorical_features = [2])#dummy variable for month
7 X = onehotencoder.fit_transform(X).toarray()
8 X = X[:, 1:]
TypeError: __init__() got an unexpected keyword argument 'categorical_features'
Upvotes: 1
Views: 2321
Reputation:
import pandas as pd
df=pd.read_csv('data.csv')
df
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df.town=le.fit_transform(df.town)
df
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe=ColumnTransformer([('OHE',OneHotEncoder(),['town'])],remainder='passthrough')
x=ohe.fit_transform(df.drop('price',axis='columns'))
x=x[:,1:]#dropping the first column to avoid dummy variable trap
x
y
from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg.fit(x,y)
reg.predict([[1,0,3600]])
For more details regarding ColumnTransformer .
Upvotes: 1