MLPClassifier(solver=’sgd’, activation=’relu’,alpha=1e-4,hidden_layer_sizes=(50,50), random_state=1,max_iter=10,learning_rate_init=.1)
参数说明(分类和回归参数一致):
属性说明:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor,MLPClassifier
from sklearn.preprocessing import StandardScaler
data1 = np.array(data)
X = data1[:, 0:2]
Y = data1[:, 2]
scaler = StandardScaler() # 标准化转换
scaler.fit(X) # 训练标准化对象
X = scaler.transform(X) # 转换数据集
clf_class= MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5,2), random_state=1)
clf_class.fit(X,Y)
y_pred = clf_class.predict([[9.317029, 14.739025]])
print('分类预测结果:', y_pred)
y_pred_pro = clf_class.predict_proba([[9.317029, 14.739025]])
print('分类预测概率:', y_pred_pro)
index=0
for w in clf_class.coefs_:
index += 1
print('第{}层网络层:'.format(index))
print('仅重矩阵:', w.shape)
print('系数矩阵:', w)
print(clf_class.classes_)
print(clf_class.loss_)
print(clf_class.activation)
print(clf_class.intercepts_)
print(clf_class.n_iter_)
data_ = [
[ -0.017612,14.053064,14.035452],[ -1.395634, 4.662541, 3.266907],[ -0.752157, 6.53862,5.786463],[ -1.322371, 7.152853, 5.830482],
[0.423363,11.054677,11.47804 ],[0.406704, 7.067335, 7.474039],[0.667394,12.741452,13.408846],[ -2.46015,6.866805, 4.406655],
[0.569411, 9.548755,10.118166],[ -0.026632,10.427743,10.401111],[0.850433, 6.920334, 7.770767],[1.347183,13.1755,14.522683],
[1.176813, 3.16702,4.343833],[ -1.781871, 9.097953, 7.316082],[ -0.566606, 5.749003, 5.182397],[0.931635, 1.589505, 2.52114 ],
[ -0.024205, 6.151823, 6.127618],[ -0.036453, 2.690988, 2.654535],[ -0.196949, 0.444165, 0.247216],[1.014459, 5.754399, 6.768858],
[1.985298, 3.230619, 5.215917],[ -1.693453,-0.55754, -2.250993],[ -0.576525,11.778922,11.202397],[ -0.346811,-1.67873, -2.025541],
[ -2.124484, 2.672471, 0.547987],[1.217916, 9.597015,10.814931],[ -0.733928, 9.098687, 8.364759],[1.416614, 9.619232,11.035846],
[1.38861,9.341997,10.730607],[0.317029,14.739025,15.056054]
]
data1 = np.array(data_)
print(data1)
X = data1[:, 0:2]
Y = data1[:, 2]
scaler = StandardScaler() # 标准化转换
scaler.fit(X) # 训练标准化对象
X = scaler.transform(X) # 转换数据集
clf = MLPRegressor(solver='lbfgs',alpha=1e-5, hidden_layer_sizes=(5,2), random_state=1)
clf.fit(X,Y)
pred = clf.predict([[0.317029, 14.739025]])
print('回归预测结果:', pred)
index=0
for w in clf.coefs_:
index += 1
print('第{}层网络层:'.format(index))
print('仅重矩阵:', w.shape)
print('系数矩阵:', w)
from sklearn.datasets import load_digits
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn import metrics
data = load_digits()
x = data.data
y = data.target
print(x.shape,y.shape)
stander = MinMaxScaler()
x = stander.fit_transform(x)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3)
clf = MLPClassifier(hidden_layer_sizes=(100,100))
clf.fit(x_train,y_train)
y_pred = clf.predict((x_test))
print("训练集评分:", clf.score(x_train,y_train))
print("测试集评分:", clf.score(x_test,y_test))
print("混淆矩阵:")
print(metrics.confusion_matrix(y_test,y_pred))
print("分类报告:", metrics.classification_report(y_test,y_pred))
print("W权重:",clf.coefs_[:1])
print("损失值:",clf.loss_)