对数据进行二项式逻辑回归分类(准确度百分之百),并将数据结果可视化

import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error,r2_score


data = pd.read_csv("./examdata.csv",encoding="gbk")
data.head()
x1 = data.loc[:,"Exam1"]
x2 = data.loc[:,"Exam2"]
x3 = data.drop(['Pass'],axis=1)
y = data.loc[:,"Pass"]
mask = data.loc[:,"Pass"]==1

x11 = np.array(x1).reshape(-1,1)
x22 = np.array(x2).reshape(-1,1)

LR = LogisticRegression()
LR.fit(x3,y)
y_predict = LR.predict(x3)
y_test = LR.predict([[40,100]])
print('ok'if y_test == 1 else 'ng')
accuray = accuracy_score(y,y_predict)
print(accuray)


# t0 = LR.intercept_
# t1,t2=LR.coef_[0][0],LR.coef_[0][1]
# x2_new = -(t0+t1*x1)/t2

x1_2 = x1*x1
x2_2 = x2*x2
x1_x2 = x1*x2
##创建二阶边界函数
x_new = {'x11':x1,'x22':x2,'x1_2 ':x1_2 ,'x2_2':x2_2,'x1_x2':x1_x2}
x_new = pd.DataFrame(x_new)
print(x_new)
LR2 = LogisticRegression()
LR2.fit(x_new,y)
# print(t0)
t0 = LR2.intercept_
t1 = LR2.coef_[0][0]
t2 = LR2.coef_[0][1]
t3 = LR2.coef_[0][2]
t4 = LR2.coef_[0][3]
t5 = LR2.coef_[0][4]
print(t1,t2)
print(t3,t4,t5)
y2_predict = LR2.predict(x_new)
print(y2_predict)
accuray2 = accuracy_score(y,y2_predict)
print(accuray2)

x1_new = x1.sort_values()#排序
print(x1_new)

a = t4
b = t5*x1_new + t2
c = t0 + t1 *x1_new + t3 *x1_new*x1_new
x2_new_2 = (-b+np.sqrt(b*b-4*a*c))/(2*a)
print(x2_new_2)


fig = plt.figure()
# # plt.subplot(1,2,1)
ok = plt.scatter(x1[mask],x2[mask])
ng = plt.scatter(x1[~mask],x2[~mask])
plt.legend((ok,ng),('ok','ng'))
plt.plot(x1_new,x2_new_2)
# # plt.subplot(1,2,2)
# # plt.scatter(x1,x2,c='b',marker="s")
# # plt.xlabel("test1")
# # plt.ylabel("test2")
plt.show()

 结果展示:

数据: 

Exam1Exam2Pass
34.6236596278.024692820
30.2867107743.894997520
35.8474087772.902198030
60.1825993986.30855211
79.0327360575.344376441
45.0832774856.316371780
61.1066645496.511425881
75.0247455746.554013541
76.098786787.420569721
84.4328199643.533393311
95.8615550738.225278060
75.0136583930.603263230
82.3070533776.48196331
69.3645887697.718691961
39.5383391476.036810850
53.9710521589.207350141
69.0701440652.740469731
67.9468554846.678574110
70.6615095592.927137891
76.9787837347.575963651
67.3720275542.838438320
89.6767757565.799365931
50.5347882948.855811530
34.2120609844.20952860
77.9240914568.972359991
62.2710136769.954457951
80.1901807544.821628931
93.114388838.800670340
61.8302060250.256107890
38.785803864.995680960
61.3792894572.807887311
85.4045193957.051983981
52.1079797363.127623770
52.0454047769.432860121
40.2368937471.167748020
54.6351055552.213885880
33.9155001198.869435740
64.1769888780.908060591
74.7892529641.573415230
34.1836400375.237720340
83.9023936656.308046221
51.5477202746.856290260
94.4433677765.568921611
82.3687537640.618255160
51.0477517745.822701460
62.2226757652.060991950
77.1930349370.45821
97.7715992886.727822331
62.073063896.768824121
91.564974588.696292551
79.9448179474.163119351
99.2725269360.9990311
90.5467141143.390601811
34.5245138560.396342460
50.2864961249.804538810
49.5866772259.808950990
97.6456339668.861572721
32.5772001795.598547610
74.2486913769.824571231
71.7964620678.453562251
75.3956114785.759936671
35.2861128247.020513950
56.253817539.261472510
30.0588224549.592973870
44.6682617266.450086150
66.5608944741.092098080
40.4575509897.535185491
49.0725632251.883211820
80.2795740192.116060811
66.7467185760.991394031
32.7228330443.307173060
64.0393204278.031688021
72.3464942396.227592971
60.4578857473.09499811
58.8409562275.858448311
99.827857872.369251931
47.2642691188.4758651
50.458159875.809859531
60.4555562942.508409440
82.2266615842.719878540
88.9138964269.80378891
94.8345067245.69430681
67.3192574766.589353181
57.2387063259.514281981
80.36675690.96014791
68.4685217985.59430711
42.0754545478.8447860
75.4777020190.4245391
78.6354243596.647427171
52.3480039960.769505260
94.0943311377.159105091
90.4485509787.508791761
55.4821611435.570703470
74.4926924284.845136851
89.8458067145.358283611
83.4891627448.38028581
42.2617008187.103850941
99.3150088168.775409471
55.3400175664.931938011
74.77589389.52981291
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值