1

enter image description here

although I used a virtual environment. I got the following error.

GitHub repo:

I have latest version of sklearn


@dataclass
class DataTransformationConfig:
    preprocessor_obj_file_path=os.path.join('artifacts','preprocessor.pkl')

class DataTransformation:
    def __init__(self):
        self.data_tranformation_config=DataTransformationConfig()

    def get_transfromation_object(self):

        try:
            numerical_column = ["writing_score", "reading_score"]
            categorical_columns = [
                "gender",
                "race_ethnicity",
                "parental_level_of_education",
                "lunch",
                "test_preparation_course",
            ]

            num_pipeline = Pipeline(

                steps=[
                ('imputer',SimpleImputer(strategy='median')),
                ('scaler',StandardScaler())
                ]
            )

            cat_pipeline = Pipeline(
                steps=[
                ('imputer',SimpleImputer(strategy='most_frequent')),
                ('one_hot_encoder',OneHotEncoder()),
                ('scaler',StandardScaler(with_mean=False))  
                ]
            )

            logging.info(f'Categorical columns:{categorical_columns}')
            logging.info(f'Numeric columns:{numerical_column}')
            

            preprocessor=ColumnTransformer(
                [
                ('num_pipeline',num_pipeline,numerical_column),
                ('cat_pipeline',cat_pipeline,categorical_columns)
                ]
            )

            logging.info(f"WHole pipeline{preprocessor}")

            return preprocessor

        except Exception as e:
            raise CustomException(e)
        
    def initiate_data_tranformation(self,train_path,test_path):
        try:
            train_df=pd.read_csv(train_path)
            test_df=pd.read_csv(test_path)

            logging.info('Read train and test data completed')

            logging.info('obtaining preprocessing object')

            preprocessor_obj=self.get_transfromation_object()

            target_column_name="math_score"
            numerical_columns =  ["writing_score", "reading_score"]

            input_feature_train_df=train_df.drop(columns=[target_column_name],axis=1)
            target_feature_train_df=train_df[target_column_name]

            input_feature_test_df=test_df.drop(columns=[target_column_name],axis=1)
            target_feature_test_df=test_df[target_column_name]
            
            logging.info(
                f'Applying preprocessing object on train and test dataframe.'
            )

            input_feature_train_arr = preprocessor_obj.fit_transform(input_feature_train_df)
            input_feature_test_arr = preprocessor_obj.transform(input_feature_test_df)
            
            logging.info(f"train shape{input_feature_train_arr.shape} and test shape {input_feature_test_arr.shape}")
            train_arr=np.c_[
                input_feature_train_arr,np.array(target_feature_train_df)
            ]

            test_arr=np.c_[
                input_feature_test_arr,np.array(target_feature_test_df)

            ]

            logging.info(f'saved preprocessing object')

            save_objects(
                file_path=self.data_tranformation_config.preprocessor_obj_file_path,
                obj=preprocessor_obj
            )

            return (
                train_arr,
                test_arr,
                self.data_tranformation_config.preprocessor_obj_file_path,
            )

        except Exception as e:
            raise CustomException(e,sys)

data_tranformation file

I made virtual env with python 3.8 for I build pipeline here but when run my Flask app i got this error in line where called '''preprocessor.tranform ''' . I got this error while making prediction

  • That usually comes up with different versions of sklearn used for training the saved object and making predictions with it. Are you sure you have the same version in those two places? – Ben Reiniger Apr 03 '23 at 15:36
  • It worked when lower the Python version was. I changed to 3.7 from 3.8. Now it is working fine – VIJAY KUMAR Apr 04 '23 at 16:05
  • Does this answer your question? [AttributeError: 'ColumnTransformer' object has no attribute '\_name\_to\_fitted\_passthrough'](https://stackoverflow.com/questions/75462901/attributeerror-columntransformer-object-has-no-attribute-name-to-fitted-pas) – Ben Reiniger May 31 '23 at 00:39

1 Answers1

0

Column Transformer objects received the _name_to_fitted_passthrough attribute in Sklearn v1.2.0. You're seeing this error because:

  1. Your pickle was created with some version of Sklearn after 1.2.0.
  2. Your application's environment is running some version of sklearn before 1.2.0.

If you check your console logs for when your application loads the pickle, you'll probably see a warning message telling you which version of Sklearn was used to create the pickle, and which version you're currently running.

You can fix it by pinning your application and training scripts to the same version of Sklearn.

AmphotericLewisAcid
  • 1,824
  • 9
  • 26