Python 我如何创建多层感知器网络的实例以用于bagging分类器?
我试图创建一个多层感知器网络的实例,用于bagging分类器。但我不知道如何修复它们 这是我的密码:Python 我如何创建多层感知器网络的实例以用于bagging分类器?,python,machine-learning,scikit-learn,neural-network,data-mining,Python,Machine Learning,Scikit Learn,Neural Network,Data Mining,我试图创建一个多层感知器网络的实例,用于bagging分类器。但我不知道如何修复它们 这是我的密码: 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
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
要训练您的
打包分类器
:
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)
要查看每个类正确预测的实例的百分比,请执行以下操作:
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()
编辑
要访问基于每基估计器的预测,即您的MLP,您可以执行以下操作:
estimators = clf.estimators_
# print(len(estimators), type(estimators[0]))
preds = []
for base_estimator in estimators:
preds.append(base_estimator.predict(X_test))
请编辑您的问题以仅关注1个问题。如果你有很多问题,请每一个问题打开一个问题,这样你可以帮助其他人更容易地找到答案。非常感谢你给出了这个漂亮的答案。问题是,我需要找到每个分类器正确预测的实例数。如何编辑代码以将其划分为8个分类器?到目前为止,我的代码对该任务正确吗?
BaggingClassifier(mlp)
为您提供的平均值超过10 mlp(请参阅文档)。调整您的作业更改n\u估计器。要访问基本估计量,您可以执行clf.\u估计量
谢谢您的回答,我将BaggingClassifier(mlp)
更改为BaggingClassifier(mlp,n\u estimators=8)
。然后如何计算每个基本分类器正确分类的测试实例数?很抱歉,我查了一下,但我不明白clf.\u估计量部分。请参阅编辑所有估计量生成X_检验。形状[0]预测数。如果它们根据相同的数据进行预测,它们会有什么不同?