I'm having a lot of issues using scikit learn recently and was hoping someone could help me with my problem. I can use other methods to ordinal encode but i want to figure this one out.
for i in range(len(ordinal_orders)):
ord_en = OrdinalEncoder(categories = {0:ordinal_orders[i]})
X_train.loc[:,ordinal_features[i]] = ord_en.fit_transform(X_train.loc[:,ordinal_features[i]].values.reshape(-1,1))
This works fine but when i try and apply this transformation to the test set i get an error.
for i in range(len(ordinal_orders)):
ord_en = OrdinalEncoder(categories = [ordinal_orders[i]])
X_test.loc[:,ordinal_features[i]] = ord_en.transform(X_test.loc[:,ordinal_features[i]].values.reshape(-1,1))
i get the value error
AttributeError Traceback (most recent call last)
<ipython-input-45-24cb27da6829> in <module>
1 for i in range(len(ordinal_orders)):
2 ord_en = OrdinalEncoder(categories = [ordinal_orders[i]])
----> 3 X_test.loc[:,ordinal_features[i]] = ord_en.transform(X_test.loc[:,ordinal_features[i]].values.reshape(-1,1))
4
/opt/anaconda3/lib/python3.8/site-packages/sklearn/preprocessing/_encoders.py in transform(self, X)
698 Transformed input.
699 """
--> 700 X_int, _ = self._transform(X)
701 return X_int.astype(self.dtype, copy=False)
702
/opt/anaconda3/lib/python3.8/site-packages/sklearn/preprocessing/_encoders.py in _transform(self, X, handle_unknown)
105 X_mask = np.ones((n_samples, n_features), dtype=np.bool)
106
--> 107 if n_features != len(self.categories_):
108 raise ValueError(
109 "The number of features in X is different to the number of "
AttributeError: 'OrdinalEncoder' object has no attribute 'categories_'
The ordinal features and orders are
ordinal_features=['LotShape','ExterQual','ExterCond','BsmtQual','BsmtCond',
'BsmtExposure','BsmtFinType1','BsmtFinType2','HeatingQC','KitchenQual',
'FireplaceQu','GarageQual','GarageCond','GarageFinish','Fence','PoolQC']
ordinal_orders=[
#LotShape
['Reg','IR1' ,'IR2','IR3'],
#ExterQual
['Fa','TA','Gd','Ex'],
#ExterCond
['Po','Fa','TA','Gd','Ex'],
#BsmtQual
['None','Fa','TA','Gd','Ex'],
#BsmtCond
['None','Po','Fa','TA','Gd','Ex'],
#BsmtExposure
['None','No','Mn','Av','Gd'],
#BsmtFinType1
['None','Unf','LwQ', 'Rec','BLQ','ALQ' , 'GLQ' ],
#BsmtFinType2
['None','Unf','LwQ', 'Rec','BLQ','ALQ' , 'GLQ' ],
#HeatingQC
['Po','Fa','TA','Gd','Ex'],
#KitchenQual
['Fa','TA','Gd','Ex'],
#FireplaceQu
['None','Po','Fa','TA','Gd','Ex'],
#GarageQual
['None','Po','Fa','TA','Gd','Ex'],
#GarageCond
['None','Po','Fa','TA','Gd','Ex'],
#GarageFinish
['None','Unf','RFn','Fin'],
#Fence
['None','MnWw','GdWo','MnPrv','GdPrv'],
#PoolQC
['None','Fa','Gd','Ex']
]
train and test set is just a generic dataset from kaggle, theyre not an issue here. Any sort of clarification would be nice!
ord_enc
object every time youfit_transform
to a new column. You should to apply it to all the relevant columns at once, preferably with aColumnTransformer
. Then you override it again when you try to transform the test data, and now the object isn't even fitted! $\endgroup$