Home > other >  How do I resolve this error? "'function' object has no attribute 'StandardScaler
How do I resolve this error? "'function' object has no attribute 'StandardScaler

Time:06-14

def preprocessing(df:pd.DataFrame,scaler:str):
  standard_scaler= preprocessing.StandardScaler()
  not_uv=[]
  for column in df.columns:
    if column != 'uv': # uv is target
      not_uv.append(column)
  if scaler == 'standard':
    standard_df = pd.DataFrame(standard_scaler.fit_transfrom(df[not_uv]), columns = not_uv)
    standard_df = pd.concat([standard_df,df[['uv']]],axis=1)
    return standard_df
preprocessing(df_13,'standard')
AttributeError: 'function' object has no attribute 'StandardScaler'

i want to make preprocssing function whats wrong with my code?

CodePudding user response:

The name of the function def preprocessing(...): is same as preprocessing.StandardScaler(). Which means you are overwriting the memory. Change the name of the function. For Example:

def new_preprocessing(df:pd.DataFrame,scaler:str):
  standard_scaler= preprocessing.StandardScaler()
  not_uv=[]
  for column in df.columns:
    if column != 'uv': # uv is target
      not_uv.append(column)
  if scaler == 'standard':
    standard_df = pd.DataFrame(standard_scaler.fit_transfrom(df[not_uv]), columns = not_uv)
    standard_df = pd.concat([standard_df,df[['uv']]],axis=1)
    return standard_df

CodePudding user response:

This code generates a conflict between user-defined function preprocessing and sklearn built-in function preprocessing. Simply import and make instance like this.

from sklearn.preprocessing import StandardScaler

standard_scaler= StandardScaler()

OR

Change your function name preprocessing to preprocessings or something else. Never declare or make an instance of built-in keywords.

  • Related