【问题标题】:Can't get attribute 'MyScaler' on <module '__main__'>无法在 <module '__main__'> 上获取属性 'MyScaler'
【发布时间】:2021-01-24 04:26:30
【问题描述】:

我的一个笔记本中有一个课程如下:

class MyScaler(BaseEstimator,TransformerMixin):
    def __init__(self,columns,with_mean=True,with_std=True,copy=True):
        self.scaler = StandardScaler(copy,with_mean,with_std)
        self.columns = columns
        self.mean_ = None
        self.var_ = None

    def fit(self,X,y=None):
        self.scaler.fit(X[self.columns],y)
        self.mean_ = np.array(np.mean(X[self.columns]))
        self.var_ = np.array(np.var(X[self.columns]))
        return self

    def transform(self,X,y=None,copy=None):
        initial_col_order = X.columns
        X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]),columns=self.columns)
        X_not_scaled = X.loc[:,~X.columns.isin(self.columns)]
        return pd.concat([X_not_scaled,X_scaled],axis=1)[initial_col_order]

我将这个类腌制为:

with open('Custom_Scaler','wb') as file:
    pickle.dump(MyScaler,file)

我有另一个模块“LogReg_Absent_Module”,我正在尝试取消腌制这个文件。我还在该模块中定义了这个类,如下所示:

import pandas as pd
import numpy as np
import pickle
from sklearn.preprocessing import StandardScaler
from sklearn.base import BaseEstimator, TransformerMixin

#The custom scaler that only scales the non-dummy value columns.
class MyScaler(BaseEstimator,TransformerMixin):
    def __init__(self,columns,with_mean=True,with_std=True,copy=True):
        self.scaler = StandardScaler(copy,with_mean,with_std)
        self.columns = columns
        self.mean_ = None
        self.var_ = None

    def fit(self,X,y=None):
        self.scaler.fit(X[self.columns],y)
        self.mean_ = np.array(np.mean(X[self.columns]))
        self.var_ = np.array(np.var(X[self.columns]))
        return self

    def transform(self,X,y=None,copy=None):
        initial_col_order = X.columns
        X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]),columns=self.columns)
        X_not_scaled = X.loc[:,~X.columns.isin(self.columns)]
        return pd.concat([X_not_scaled,X_scaled],axis=1)[initial_col_order]

#The class that we are going to use from here on to predict new data
class absenteeism_model():
    def __init__(self,model_file,scaler_file):
        with open('Absenteeism_Model','rb') as model_file,open('Custom_Scaler','rb') as scaler_file:
            self.log_reg = pickle.load(model_file)             #Load the previously saved model 
            self.scaler = pickle.load(scaler_file)             #and scaler.
            self.data = None

从一个新笔记本,当我尝试model = absenteeism_model('Absenteeism_Model','Custom_Scaler')

我明白了:

<ipython-input-66-8631c175353f> in <module>
----> 1 model = absenteeism_model('Absenteeism_Model','Custom_Scaler')

~\LogReg_Absent_Module.py in __init__(self, model_file, scaler_file)
    37         with open('Absenteeism_Model','rb') as model_file,open('Custom_Scaler','rb') as scaler_file:
    38             self.log_reg = pickle.load(model_file)             #Load the previously saved model
---> 39             self.scaler = pickle.load(scaler_file)             #and scaler.
    40             self.data = None
    41 

AttributeError: Can't get attribute 'MyScaler' on <module '__main__'>```

   






【问题讨论】:

    标签: python module attributeerror


    【解决方案1】:

    我不明白你为什么腌制一个类而不是那个类的对象

    【讨论】:

    • 我尝试腌制 MyScaler 类的对象,但仍然得到 AttributeError。
    【解决方案2】:

    您介意展示您已实现 MyScaler 的部分代码吗?我认为在您保存缩放器时,您的 MyScaler 类的对象存在问题。

    例如你已经声明:

    scaler = MyScaler(X)
    

    那么在这种情况下,您将使用以下代码进行腌制:

     with open ('Custom_Scaler' , 'wb') as file: 
      pickle.dump(scaler, file)
    

    看看这是否能解决您的问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多