from sklearn.neural_network import MLPClassifier
#初始化输入矩阵
X = [[0., 0.], [1., 1.]]
#初始化目标值
y = [0, 1]
#实例化一个人工神经网络分类器并传入数据训练
clf = MLPClassifier(solver='sgd', alpha=1e-5, activation='logistic',hidden_layer_sizes=(5, 2), max_iter=2000, tol=1e-4)
clf.fit(X, y)
#打印预测类别
predicted_value = clf.predict([[2., 2.], [-1., -2.]])
print(predicted_value)
#打印预测类别的概率值
predicted_proba = clf.predict_proba([[2., 2.], [-1., -2.]])
print(predicted_proba)
#打印三组权重矩阵的形状
print([coef.shape for coef in clf.coefs_])
#打印三组权重矩阵的参数
print([coef for coef in clf.coefs_])
运行结果:
[1 1]
[[0.46120433 0.53879567]
[0.45146875 0.54853125]]
[(2, 5), (5, 2), (2, 1)]
[array([[-0.50343439, 0.01371935, -0.09313015, 0.48554675, -0.11381708],
[ 0.1311852 , 0.52720183, 0.45345865, 0.20616084, -0.22983084]]), array([[ 0.18912099, 0.28796118],
[-0.20872189, 0.08920981],
[ 0.07243314, 0.27113297],
[-0.52251994, -0.35376903],
[ 0.06183225, -0.10000771]]), array([[ 0.53757696],
[-0.7241105 ]])]
Process finished with exit code 0