【问题标题】:How can i create an instance of multi-layer perceptron network to use in bagging classifier?如何创建多层感知器网络实例以用于装袋分类器?
【发布时间】:2021-03-23 01:41:34
【问题描述】:

我正在尝试创建一个多层感知器网络实例以用于装袋分类器。但我不明白如何解决它们。

这是我的代码:



My task is:

1-To apply bagging classifier (with or without replacement) with eight base classifiers created at the previous step.


It would be really great if you show me how can i implement this to my algorithm. I did my search but i couldn't find a way to do that

【问题讨论】:

  • 请编辑您的问题以仅关注 1 个问题。如果您有很多,请每 1 个问题打开 1 个问题,这样您将帮助其他人更轻松地找到答案。

标签: python machine-learning scikit-learn neural-network data-mining


【解决方案1】:

训练你的BaggingClassifier

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.model_selection import train_test_split 
from sklearn.preprocessing import StandardScaler  
from sklearn.neural_network import MLPClassifier 
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import classification_report, confusion_matrix

#Load the digits data:

X,y = load_digits(return_X_y=True)

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=0)
# Feature scaling
scaler = StandardScaler()  
scaler.fit(X_train)
X_train = scaler.transform(X_train)  
X_test = scaler.transform(X_test)
# Finally for the MLP- Multilayer Perceptron
mlp = MLPClassifier(hidden_layer_sizes=(16, 8, 4, 2), max_iter=1001)

clf = BaggingClassifier(mlp, n_estimators=8)
clf.fit(X_train,y_train)

要分析您的输出,您可以尝试:

y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred, labels=clf.classes_)
print(cm)

查看每个类正确预测的实例数:

print(cm[np.eye(len(clf.classes_)).astype("bool")])

查看每个类正确预测实例的百分比:

cm[np.eye(len(clf.classes_)).astype("bool")]/cm.sum(1)

要查看算法的总体准确性:

(y_pred==y_test).mean()

编辑

要访问基于每个基础估计器的预测,即您的 mlps,您可以执行以下操作:

estimators = clf.estimators_
# print(len(estimators), type(estimators[0]))
preds = []
for base_estimator in estimators:
    preds.append(base_estimator.predict(X_test))

【讨论】:

  • 非常感谢您的精彩回答。问题是,我需要为每个分类器找到正确预测的实例数。如何编辑我的代码以将其分为 8 个分类器?到目前为止,我的代码对于该任务是否正确???
  • BaggingClassifier(mlp) 平均超过 10 mlp(请参阅文档)。调整您的分配更改n_estimators。要访问 base_estimators,您可以执行 clf._estimators
  • 感谢您的回答,我将 BaggingClassifier(mlp) 更改为 BaggingClassifier(mlp, n_estimators=8) 。之后如何计算每个基分类器的正确分类测试实例的数量?很抱歉,但我查了一下,但我不明白 clf._estimators 部分。
  • 请参阅编辑
  • 所有估计器产生 X_test.shape[0] 个预测数。如果他们对相同的数据进行预测,他们会有什么不同?
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 2018-03-31
  • 2013-12-11
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2016-08-23
  • 2016-05-06
相关资源
最近更新 更多