对数几率回归模型(学习时间和考试通过率的预测)

from collections import  OrderedDict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

examDict={'学习时间':[-3,-2,-1,0,0.1,0.4,0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.3,2.50,2.75,3.00,3.25,3.4,3.7,3.50,4.00,4.25,4.50,4.75,5.00,5.50,6.00,10.00,11.00],
          '通过考试':[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0]}
examOrdereDict=OrderedDict(examDict)
examDF=pd.DataFrame(examOrdereDict)
exam_x=examDF['学习时间']
exam_y=examDF['通过考试']
x_train,x_test,y_train,y_test=train_test_split(exam_x,exam_y,test_size=0.8)
print('训练特征数量:',x_train.shape,'测试特征数量:',x_test.shape,'样本特征数量:',exam_x.shape)
x_train=x_train.values.reshape(-1,1)
x_test=x_test.values.reshape(-1,1)
exam_x=exam_x.values.reshape(-1,1)
model=LogisticRegression()
model.fit(x_train,y_train)
model.score(x_test,y_test)
print(model.predict_proba([[3]]))
a=model.intercept_
b=model.coef_
za=a+b*exam_x
ypred=1/(1+np.exp(-za))
print(ypred)
plt.xlim((-5, 13))
plt.scatter(exam_x,ypred,color="red",label='exam data',linewidth=0.5)
plt.plot(exam_x,ypred, color='blue',label="predict line",linewidth=3)
plt.legend(loc='lower right')
plt.show()

结果:

对数几率回归模型(预测部分没有写但比较简单也就是一行代码,在sklearn文档上找一下就可以了)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值