前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python从零开始第六章机器学习②逻辑回归实战(2)

Python从零开始第六章机器学习②逻辑回归实战(2)

作者头像
用户1359560
发布2018-12-27 17:19:03
3550
发布2018-12-27 17:19:03
举报
文章被收录于专栏:生信小驿站生信小驿站

  • 训练模型
代码语言:javascript
复制
log_regress = linear_model.LogisticRegression()
# Train the model
log_regress.fit(X = train_features ,
                y = train_label)


# Check trained model intercept
print(log_regress.intercept_)
# Check trained model coefficients
print(log_regress.coef_)
[3.8742755]
[[-0.85532933 -2.30146606 -0.03444764 -0.29622237 -0.00644779  0.00482113
  -0.01987031]]
  • 获取预测结果和预测概率
代码语言:javascript
复制
  # In[*]   

# Make predictions
preds = log_regress.predict(X=test_features)
print(preds)
 # In[*]   
 # Predict the probablities
pred_probs = log_regress.predict_proba(X=test_features)
print(pred_probs)
  • 获取预测生存状态与实际生存状态的交叉矩阵
代码语言:javascript
复制
print(pd.crosstab(preds, test_label))
Survived   0   1
row_0           
0         92  24
1         14  48
  • 获取预测准确值
代码语言:javascript
复制
 # In[*] 
 # get the accuracy of the prediction
log_regress.score(X = test_features ,
                  y = test_label)
0.7865168539325843
  • 除了使用crosstab()函数生成混淆矩阵之外,您还可以使用Scikit中的metrics模块中的confusion_matrix()函数来学习:
代码语言:javascript
复制
 # In[*] 
from sklearn import metrics
# view the confusion matrix
metrics.confusion_matrix(
    y_true = test_label, # True labels
    y_pred = preds) # Predicted labels
  • 获取模型的其他评价指标
代码语言:javascript
复制
print(metrics.classification_report(
      y_true = test_label,
      y_pred = preds))
             precision    recall  f1-score   support

          0       0.79      0.87      0.83       106
          1       0.77      0.67      0.72        72

avg / total       0.79      0.79      0.78       178
  • 绘制ROC曲线
代码语言:javascript
复制
   # In[*]  
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# convert the probabilities from ndarray to
# dataframe
df_prob = pd.DataFrame(
    pred_probs,
    columns=['Death', 'Survived'])
fpr, tpr, thresholds = roc_curve(
    test_label, df_prob['Survived'])
# find the area under the curve (auc) for the
# ROC
roc_auc = auc(fpr, tpr)
plt.title(
    'Receiver Operating Characteristic Curve')
plt.plot(fpr, tpr, 'black',
         label='AUC = %0.2f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.1])
plt.ylim([-0.1,1.1])
plt.ylabel('True Positive Rate (TPR)')
plt.xlabel('False Positive Rate (FPR)')
plt.show()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档