问题描述
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,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,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,scaler_file)
37 with open('Absenteeism_Model','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__'>```
解决方法
我不明白您为什么要腌制一个班级而不是该班级的对象
,您介意显示实现MyScaler的零件代码。我认为保存缩放器时MyScaler类的对象有问题。
例如,您已声明:
scaler = MyScaler(X)
然后在这种情况下,您将使用以下代码腌制:
with open ('Custom_Scaler','wb') as file:
pickle.dump(scaler,file)
看看这是否可以解决您的问题。