逻辑回归_相亲成功率

构建数据集

from sklearn import linear_model, metrics
from sklearn.model_selection import train_test_split
import pandas as pd
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
model_data = pd.read_csv("date_data.csv")
model_data.head()
Y = model_data["Dated"]
X = model_data.loc[ :,'income':'assets']
train_data, test_data, train_target, test_target = train_test_split(X, Y, test_size=0.2,random_state=0)

建模

logistic_model = linear_model.LogisticRegression()
logistic_model.fit(train_data, train_target)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False)
test_est = logistic_model.predict(test_data)
train_est = logistic_model.predict(train_data)
test_est_p = logistic_model.predict_proba(test_data)[:,1]
train_est_p = logistic_model.predict_proba(train_data)[:,1]

决策(Decisions)类检验

print(metrics.classification_report(test_target, test_est))
             precision    recall  f1-score   support

          0       0.89      0.73      0.80        11
          1       0.73      0.89      0.80         9

avg / total       0.82      0.80      0.80        20
metrics.accuracy_score(test_target, test_est)
0.8

排序(Rankings)类检验

ROC曲线

fpr_test, tpr_test, th_test = metrics.roc_curve(test_target, test_est_p)
fpr_train, tpr_train, th_train = metrics.roc_curve(train_target, train_est_p)
plt.figure(figsize=[6,6])
plt.plot(fpr_test, tpr_test,color='red')
plt.plot(fpr_train, tpr_train,color='black')
plt.title('ROC curve')
Text(0.5,1,'ROC curve')

[外链图片转存失败(img-tZ9AmEu0-1562725864378)(output_11_1.png)]

test_AUC=metrics.roc_auc_score(test_target, test_est_p)
train_AUC=metrics.roc_auc_score(train_target, train_est_p)
print ("test_AUC:",test_AUC, "train_AUC:",train_AUC)
test_AUC: 0.9393939393939394 train_AUC: 0.9806128830519074

KS曲线

test_x_axis = np.arange(len(fpr_test))/float(len(fpr_test))
train_x_axis = np.arange(len(fpr_train))/float(len(fpr_train))
plt.figure(figsize=[6,6])
plt.plot(fpr_test, test_x_axis, color='blue')
plt.plot(tpr_test, test_x_axis, color='red')
#plt.plot(fpr_train, train_x_axis, color=red)
#plt.plot(tpr_train, train_x_axis, color=red)
plt.title('KS curve')
Text(0.5,1,'KS curve')

[外链图片转存失败(img-rv8qoPl3-1562725864379)(output_14_1.png)]

from scipy.stats import ks_2samp
ks_2samp(fpr_test,tpr_test)
Ks_2sampResult(statistic=0.7142857142857142, pvalue=0.02750034109174311)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值