-1

I have written a pipeline for processing the data, but my program gives this error:

AttributeError: 'numpy.ndarray' object has no attribute 'fit'

The reason why I am creating a new class was that I have tried to implement LabelEncoder in the pipeline directly but it gives me some different error.

My class looks like this:

class myLabelEncoder(BaseEstimator, TransformerMixin):
    
    def __init__(self):
        self.encoder = LabelEncoder()
        self.X = None
    def fit(self,X,y = None):
        self.X = X
        return self.encoder.fit(X)
    
    def transform(self, X, y = None):
        return self.encoder.transform(X)

And here is the pipeline:

#other transformers...

label_transformer = Pipeline(steps = [('imputer',SimpleImputer(missing_values = np.nan, 
strategy = 'most_frequent')),('label',myLabelEncoder)])


pipeline =ColumnTransformer(
        transformers = [('cat', categorical_transformer, cat_features),
                        ('num', numeric_transformer, num_features),  
                        ('ord', ordinal_transformer, ord_features),
                       ('lab', label_transformer, label_features)]) 

logistic = LogisticRegression()

pca = PCA()

clf = Pipeline(steps = [('preprocessor', pipeline),
                        ('pca', pca),
                        ('classifier',logistic)])

#creating parameters for tuning pca and logistic regression

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


clf.fit(X_train, y_train)

I think the problem is in the class code, but I could not find the solution although I searched similar posts about this error.

Here are 5 samples of dataset:

2 sample rows for X =

 {'hotel': {54261: 'City Hotel', 77908: 'City Hotel'},
 'meal': {54261: 'SC', 77908: 'BB'},
 'lead_time': {54261: 49, 77908: 9},
 'adr': {54261: 93.15, 77908: 155.08},
 'stays_in_weekend_nights': {54261: 1, 77908: 2},
 'stays_in_week_nights': {54261: 0, 77908: 1},
 'adults': {54261: 2, 77908: 2},
 'children': {54261: 0.0, 77908: 0.0},
 'previous_cancellations': {54261: 0, 77908: 0},
 'previous_bookings_not_canceled': {54261: 0, 77908: 0},
 'total_of_special_requests': {54261: 0, 77908: 1},
 'customer_type': {54261: 'Transient', 77908: 'Transient'},
 'market_segment': {54261: 'Online TA', 77908: 'Online TA'},
 'distribution_channel': {54261: 'TA/TO', 77908: 'TA/TO'},
 'reserved_room_type': {54261: 'A', 77908: 'A'},
 'assigned_room_type': {54261: 'A', 77908: 'A'},
 'arrival_date_month': {54261: 'July', 77908: 'September'}}

2 sample rows for y:

{54261: 1, 77908: 0, 38772: 0, 100452: 0, 2318: 1}

Thank you for your time.

1 Answers1

1

You forgot to instantiate your label encoder class in label_transformer: replace myLabelEncoder with myLabelEncoder().

Sanjar Adilov
  • 1,039
  • 7
  • 15