python - TypeError:“管道”对象在自定义分类器中不可调用
问题描述
我正在研究一个分类问题。我为我的分类器创建了一个类。我在这门课上有问题。我有一个 get_clf_pipeline 方法,它返回分类器管道,由 train 方法使用。此方法不需要访问实例或类数据,我尝试将其设为静态方法,但该方法效果不佳。我该如何解决这个问题?
class Event_classifier:
def __init__(self):
self.clf_pipeline = self.get_clf_pipeline()
def get_clf_pipeline(self):
""" Return the pipeline
"""
categorical_features = ['CRole', 'Clevel', 'Gender']
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
('onehot', OneHotEncoder(handle_unknown='ignore'))])
preprocessor = ColumnTransformer(
transformers=[
('cat', categorical_transformer, categorical_features)])
estimators = [
('rf',RandomForestClassifier(n_estimators=200,class_weight='balanced')),
('mnb', MultinomialNB()),
('svr', make_pipeline(StandardScaler(with_mean=False),
LinearSVC(random_state=42)))
]
stacked_clf = StackingClassifier(
estimators=estimators, final_estimator=LogisticRegression(class_weight='balanced')
)
clf_pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', stacked_clf)])
return clf_pipeline
def train(self, X,y):
"""Trains the classifier on input data
"""
print(type(self.clf_pipeline))
self.clf_pipeline(X,y)
def predict(self):
"""Predict on test data
"""
if (X.shape) == 1:
X = X.T
y_pred = self.clf_pipeline.predict(X)
return y_pred
创建类实例并训练数据
ec = Event_classifier()
ec.train(X_train, y_train)
print('Model training complete')
我收到以下错误
<class 'sklearn.pipeline.Pipeline'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-129-36d8dfcdfc12> in <module>
1 ec = Event_classifier()
2
----> 3 ec.train(X_train, y_train)
4 print('Model training complete')
5
<ipython-input-126-de5f651c0a3d> in train(self, X, y)
35 """
36 print(type(self.clf_pipeline))
---> 37 self.clf_pipeline(X,y)
38
39 def predict(self):
TypeError: 'Pipeline' object is not callable
解决方案
您self.clf_pipeline
是一个Pipeline
对象,因此self.clf_pipeline(X,y)
尝试在输入上调用管道X, y
,但是(如错误所示)Pipeline
s 不是函数。大概你想要类似的东西self.clf_pipeline.fit(X, y)
。
还有一件事跳出来:什么时候X.shape == 1
(在你的predict
方法中)?
推荐阅读
- python - 将 PIL.GifImagePlugin.GifImageFile 转换为 3 维 numpy 数组
- snowflake-cloud-data-platform - Talend 作业在来自 tSnowflakeInput 组件的简单 SELECT 上引发错误
- pycharm - 在使用 pytest 集成的 PyCharm 中,可以通过测试抑制 print() 输出吗?
- php - PHP MySQL循环遍历行并在日期更改时打印日期
- javascript - 如何使用 axios 和 node js 将数据发送到后端
- python - 模块'keras.backend'没有属性'unique_object_name'keras错误
- css - 嵌套的 Flexbox - 外部容器不弯曲
- android - 推送通知自定义图片,如何设置不展开Android
- python - Python“make_transforms”在对齐适合图像时显示列表索引错误
- ios - 在 iOS 上使用来自 URL 的自定义 vtt 文件和 AVPlayerItem