Error when trying .transform for OrdinalEncoder from Scikit Learn 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)
&lt;ipython-input-45-24cb27da6829&gt; in &lt;module&gt;
      1 for i in range(len(ordinal_orders)):
      2     ord_en = OrdinalEncoder(categories = [ordinal_orders[i]])
----&gt; 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         &quot;&quot;&quot;
--&gt; 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 
--&gt; 107         if n_features != len(self.categories_):
    108             raise ValueError(
    109                 &quot;The number of features in X is different to the number of &quot;

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!
It is really hard to figure out the logic behind what you are doing, it look odd
But assuming you are trying to apply a preprocessing step to a data frame I would go as follows:
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import OrdinalEncoder


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']
]

transformer = make_column_transformer((OrdinalEncoder(categories= ordinal_orders),ordinal_features), remainder = &quot;passthrough&quot;).fit(X_train)

Then you can apply the transformer to both X_train and X_test
transformer.transform(X_train) or transformer.transform(X_test)