逻辑回归预测考试通过

基于examdata.csv数据,建立逻辑回归模型 预测Exam1 = 75, Exam2 = 60时,该同学在Exam3是 passed or failed; 建立二阶边界,提高模型准确度

(1)load the data

import pandas as pd

import numpy as np

data = pd.read_csv('examdata.csv')

data.head()

查看数据信息


data数据信息

(2)#visualize the data

%matplotlib inline

from matplotlib import pyplot as plt

fig1 = plt.figure()

plt.scatter(data.loc[:,'Exam1'],data.loc[:,'Exam2'])

plt.title('Exam1-Exam2')

plt.xlabel('Exam1')

plt.ylabel('Exam2')

plt.show()


Exam1-Exam2

(3)add label mask

mask=data.loc[:,'Pass']==1

print(~mask)


mask

(4) 数据分类可视化

fig2 = plt.figure()

passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])

failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])

plt.title('Exam1-Exam2')

plt.xlabel('Exam1')

plt.ylabel('Exam2')

plt.legend((passed,failed),('passed','failed'))

plt.show()


分类可视化

#define X,y

X = data.drop(['Pass'],axis=1)

y = data.loc[:,'Pass']

X1 = data.loc[:,'Exam1']

X2 = data.loc[:,'Exam2']

X1.head()

(5)训练

#establish the model and train it

from sklearn.linear_model import LogisticRegression

LR = LogisticRegression()

LR.fit(X,y)

(6)预测

#show the predicted result and its accuracy

y_predict = LR.predict(X)

print(y_predict)

from sklearn.metrics import accuracy_score

accuracy =  accuracy_score(y,y_predict)

print(accuracy)

赋值theta0,theta1,theta2

theta0 = LR.intercept_

theta1,theta2 = LR.coef_[0][0],LR.coef_[0][1]

print(theta0,theta1,theta2)

X2_new = -(theta0+theta1*X1)/theta2

print(X2_new)

拟合数据

fig3 = plt.figure()

passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])

failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])

plt.plot(X1,X2_new)

plt.title('Exam1-Exam2')

plt.xlabel('Exam1')

plt.ylabel('Exam2')

plt.legend((passed,failed),('passed','failed'))

plt.show()


拟合数据

下面将使用边界函数进行优化


决策边界

边界函数: 0+11+22=0θ0+θ1X1+θ2X2=0

二阶边界函数:0+11+22+321+422+512=0

对数据重新整合

#create new data

X1_2 = X1*X1

X2_2 = X2*X2

X1_X2 = X1*X2

#生成X_new集合

X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}

X_new = pd.DataFrame(X_new)

print(X_new)

#建立新模型并训练

#establish new model and train

LR2 = LogisticRegression()

LR2.fit(X_new,y)

y2_predict = LR2.predict(X_new)

accuracy2 = accuracy_score(y,y2_predict)

print(accuracy2)

对X1_new 进行排序

X1_new = X1.sort_values()

边界函数: 0+11+22=0θ0+θ1X1+θ2X2=0

二阶边界函数:0+11+22+321+422+512=0θ0+θ1X1+θ2X2+θ3X12+θ4X22+θ5X1X2=0

2++=0:1=(−+(2−4).5)/2,1=(−−(2−4).5)/2ax2+bx+c=0:x1=(−b+(b2−4ac).5)/2a,x1=(−b−(b2−4ac).5)/2a

422+(51++2)2+(0+11+321)=0

theta0 = LR2.intercept_

theta1,theta2,theta3,theta4,theta5 = LR2.coef_[0][0],LR2.coef_[0][1],LR2.coef_[0][2],LR2.coef_[0][3],LR2.coef_[0][4]

a = theta4

b = theta5*X1_new+theta2

c = theta0+theta1*X1_new+theta3*X1_new*X1_new

X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)

print(X2_new_boundary)

优化拟合可视化

fig5 = plt.figure()

passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])

failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])

plt.plot(X1_new,X2_new_boundary)

plt.title('Exam1-Exam2')

plt.xlabel('Exam1')

plt.ylabel('Exam2')

plt.legend((passed,failed),('passed','failed'))

plt.show()

plt.plot(X1_new,X2_new_boundary)

plt.show()


优化拟合

本人目前开始学习机器学习相关知识,相互交流或者课程分享联系V:it1024666

你可能感兴趣的:(逻辑回归预测考试通过)