如何创建一个多层感知器网络的实例用于bagging分类器



我正在尝试创建一个多层感知器网络的实例,用于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 to my algorithm. I did my search but i couldn't find a way to do that

训练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))

最新更新