## 查看其对应的wprint('the weight of Logistic Regression:\n',clf.coef_)## 查看其对应的w0print('the intercept(w0) of Logistic Regression:\n',clf.intercept_)## 由于这个是3分类,所有我们这里得到了三个逻辑回归模型的参数,其三个逻辑回归组合起来即可实现三分类。
## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)## 由于逻辑回归模型是概率预测模型(前文介绍的 p = p(y=1|x,\theta)),所有我们可以利用 predict_proba 函数预测其概率
train_predict_proba = clf.predict_proba(x_train)
test_predict_proba = clf.predict_proba(x_test)print('The test predict Probability of each class:\n',test_predict_proba)## 其中第一列代表预测为0类的概率,第二列代表预测为1类的概率,第三列代表预测为2类的概率。## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))
The test predict Probability of each class:[[1.32525870e-042.41745142e-017.58122332e-01][7.02970475e-012.97026349e-013.17667822e-06][3.37367886e-027.25313901e-012.40949311e-01][5.66207138e-036.53245545e-013.41092383e-01][1.06817066e-026.72928600e-013.16389693e-01][8.98402870e-046.64470713e-013.34630884e-01][4.06382037e-043.86192249e-016.13401369e-01][1.26979439e-018.69440588e-013.57997319e-03][8.75544317e-011.24437252e-011.84312617e-05][9.11209514e-018.87814689e-029.01671605e-06][3.86067682e-043.06912689e-016.92701243e-01][6.23261939e-037.19220636e-012.74546745e-01][8.90760124e-011.09235653e-014.22292409e-06][2.32339490e-034.47236837e-015.50439768e-01][8.59945211e-044.22804376e-015.76335679e-01][9.24814068e-017.51814638e-024.46852786e-06][2.01307999e-029.35166320e-014.47028801e-02][1.71215635e-025.07246971e-014.75631465e-01][1.83964097e-043.17849048e-016.81966988e-01][5.69461042e-014.30536566e-012.39269631e-06][8.26025475e-011.73971556e-012.96936737e-06][3.05327704e-045.15880492e-014.83814180e-01][4.69978972e-032.90561777e-017.04738434e-01][8.61077168e-011.38915993e-016.83858427e-06][6.99887637e-042.48614010e-017.50686102e-01][5.33421842e-028.31557126e-011.15100690e-01][2.34973018e-023.54915328e-016.21587370e-01][1.63311193e-033.48301765e-016.50065123e-01][7.72156866e-012.27838662e-014.47157219e-06][9.30816593e-016.91640361e-021.93708074e-05]]
The accuracy of the Logistic Regression is:0.9583333333333334
The accuracy of the Logistic Regression is:0.8