逻辑回归笔记

### Step1:库函数导入

  from sklearn.model_selection import train_test_split
  #测试集占总数据中的30%, 设置随机状态,方便后续复现本次的随机切分
  X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size = 0.3, random_state=100)

###  Step2:模型训练 

  from sklearn.linear_model import LogisticRegression
  logisticRegre = LogisticRegression()
  #训练
  logisticRegre.fit(X_train, y_train)

 

 

###Step3:模型参数查看和评估

##查看其对应的w
print('the weight of Logistic Regression:',logisticRegre.coef_.shape)
##查看其对应的w0
print('the intercept(w0) of Logistic Regression:',logisticRegre.intercept_)

# 对模型进行评估
print('逻辑回归训练集准确率:%.9f'% logisticRegre.score(X_train,y_train))
print('逻辑回归测试集准确率:%.9f'% logisticRegre.score(X_test,y_test))

##train_predict=logisticRegre.predict(X_train)
##test_predict=logisticRegre.predict(X_test)
from sklearn import metrics
import seaborn as sns
##利用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))

##查看混淆矩阵(预测值和真实值的各类情况统计矩阵)
confusion_matrix_result=metrics.confusion_matrix(test_predict,y_test)
##print('The confusion matrix result:\n',confusion_matrix_result)

##利用热力图对于结果进行可视化
plt.figure(figsize=(8,6))
sns.heatmap(confusion_matrix_result,annot=True,cmap='Blues')
plt.xlabel('Predictedlabels')
plt.ylabel('Truelabels')
plt.show()

 

  •  Step4:数据和模型可视化 
  •  Step5:模型预测
posted @ 2021-08-11 17:51  KJXY  阅读(39)  评论(0)    收藏  举报