![](https://img.haomeiwen.com/i15301298/29c7e4ba9269c02f.png)
# 添加mask
mask = df["Pass"] == 1
画图
![](https://img.haomeiwen.com/i15301298/8a4f55beed25ea09.png)
#x和y
x = df.drop(["Pass"], axis=1)
y = df["Pass"]
x1 = df["Exam1"]
x2 = df["Exam2"]
#训练模型
logis_model = LogisticRegression()
logis_model.fit(x, y)
#预测结果
y_predict = logis_model.predict(x)
from sklearn.metrics import accuracy_score
#准确率预测
accuracy_score(y, y_predict) #准确率0.89
#获取模型上的系数
the1, the2 = logis_model.coef_[0][0], logis_model.coef_[0][1]
the0 = logis_model.intercept_
#t边界函数 θ0 + θ1*x1 + θ2*x2 = 0
x2_new = -(the0 + the1*x1)/the2
x2_new
![](https://img.haomeiwen.com/i15301298/2ffdd8d796eb4d7c.png)
二阶边界函数 θ0 + θ1x1 + θ2x2 + θ3x1^2 + θ4x2^2 + θ5x1x2 = 0
ax^2 + bx + c = 0:x1=(-b + (b^2 - 4ac)^5)/2a,x1=(-b - (b^2 - 4ac)^5)/2a
θ4x2^2 + (θ2 + θ5x1)x2 + (θ0 + θ1x1 + θ3*x1^2)
# 创建新的数据集
x1_2 = x1 * x1
x2_2 = x2 * x2
x1_x2 = x1 * x2
#转成df结构
x_new = {"x1": x1, "x2": x2, "x1_2": x1_2, "x2_2": x2_2, "x1_x2": x1_x2}
x_new = pd.DataFrame(x_new)
![](https://img.haomeiwen.com/i15301298/119d00e5fecdea0a.png)
#模型训练
l2 = LogisticRegression()
l2.fit(x_new, y)
#预测,准确率
y_predict2 = l2.predict(x_new)
accuracy_score(y, y_predict2) #准确率1.0
# 模型上的系数
the1 = l2.coef_[0][0]
the2 = l2.coef_[0][1]
the3 = l2.coef_[0][2]
the4 = l2.coef_[0][3]
the5 = l2.coef_[0][4]
the0 = l2.intercept_
#对x1排序
x1_new = x1.sort_values()
#计算边界函数的a、b、c
a = the4
b = the2 + the5 * x1_new
c = the0 + the1*x1_new + the3*x1_new* x1_new
# 新的边界
x2_new_boundray = (-b + np.sqrt(b*b -4*a*c)) / (2*a)
![](https://img.haomeiwen.com/i15301298/66e484a402493b32.png)
网友评论