我需要帮助解决我学校的年级生产生的属性错误。
我的任务: 我需要创建一个自定义转换器,以输入到平地机中。
平地机将字典列表传递给我的估算器而不是DataFrame的predict或predict_proba方法。这意味着该模型必须同时使用两种数据类型。因此,我需要提供一个自定义的ColumnSelectTransformer,以代替使用scikit-learn自己的ColumnTransformer。
这是我的自定义转换器的代码,旨在在提供的列中插入空值。
from sklearn.impute import SimpleImputer
simple_cols = ['BEDCERT', 'RESTOT', 'INHOSP', 'CCRC_FACIL', 'SFF', 'CHOW_LAST_12MOS', 'SPRINKLER_STATUS', 'EXP_TOTAL', 'ADJ_TOTAL']
class ColumnSelectTransformer(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
def fit(self, X, y=None):
return self
def transform(self, X):
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
return X[self.columns].values
simple_features = Pipeline([
('cst', ColumnSelectTransformer(simple_cols)),
('imputer', SimpleImputer(strategy='mean')),
])
然后我受命创建一个新管道,并使其与估计量相适应,这是我的尝试。
from sklearn.linear_model import LinearRegression
simple_features_model = Pipeline([
('simple', simple_features),
('linear', LinearRegression()),
])
simple_features_model.fit(data, fine_counts > 0)
管道生成成功
Pipeline(memory=None,
steps=[('simple',
Pipeline(memory=None,
steps=[('cst',
ColumnSelectTransformer(columns=['BEDCERT',
'RESTOT',
'INHOSP',
'CCRC_FACIL',
'SFF',
'CHOW_LAST_12MOS',
'SPRINKLER_STATUS',
'EXP_TOTAL',
'ADJ_TOTAL'])),
('imputer',
SimpleImputer(add_indicator=False, copy=True,
fill_value=None,
missing_values=nan,
strategy='mean', verbose=0))],
verbose=False)),
('linear',
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
normalize=False))],
verbose=False)
但是,当我将我的simple_features_model传递给学校的分级机时
def positive_probability(model):
def predict_proba(X):
return model.predict_proba(X)[:, 1]
return predict_proba
grader.score.ml__simple_features(positive_probability(simple_features_model))
我收到以下错误
AttributeError Traceback (most recent call last)
<ipython-input-87-243f592b48ee> in <module>()
4 return predict_proba
5
----> 6 grader.score.ml__simple_features(positive_probability(simple_features_model))
/opt/conda/lib/python3.7/site-packages/static_grader/grader.py in func(*args, **kw)
92 def __getattr__(self, method):
93 def func(*args, **kw):
---> 94 return self(method, *args, **kw)
95 return func
96
/opt/conda/lib/python3.7/site-packages/static_grader/grader.py in __call__(self, question_name, func)
88 return
89 test_cases = json.loads(resp.text)
---> 90 test_cases_grading(question_name, func, test_cases)
91
92 def __getattr__(self, method):
/opt/conda/lib/python3.7/site-packages/static_grader/grader.py in test_cases_grading(question_name, func, test_cases)
40 for test_case in test_cases:
41 if inspect.isroutine(func):
---> 42 sub_res = func(*test_case['args'], **test_case['kwargs'])
43 elif not test_case['args'] and not test_case['kwargs']:
44 sub_res = func
<ipython-input-87-243f592b48ee> in predict_proba(X)
1 def positive_probability(model):
2 def predict_proba(X):
----> 3 return model.predict_proba(X)[:, 1]
4 return predict_proba
5
/opt/conda/lib/python3.7/site-packages/sklearn/utils/metaestimators.py in __get__(self, obj, type)
108 continue
109 else:
--> 110 getattr(delegate, self.attribute_name)
111 break
112 else:
AttributeError: 'LinearRegression' object has no attribute 'predict_proba'