美文网首页
逻辑回归(一)

逻辑回归(一)

作者: y_7539 | 来源:发表于2023-01-08 14:20 被阅读0次
image.png
# 添加mask
mask = df["Pass"] == 1

画图

image.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
image.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)
image.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)
image.png

相关文章

  • 机器学习day7-逻辑回归问题

    逻辑回归 逻辑回归,是最常见最基础的模型。 逻辑回归与线性回归 逻辑回归处理的是分类问题,线性回归处理回归问题。两...

  • ML03-逻辑回归(下部分)

    本文主题-逻辑回归(下部分):逻辑回归的应用背景逻辑回归的数学基础逻辑回归的模型与推导逻辑回归算法推导梯度下降算法...

  • ML02-逻辑回归(上部分)

    本文主题-逻辑回归(上部分):逻辑回归的应用背景逻辑回归的数学基础逻辑回归的模型与推导逻辑回归算法推导梯度下降算法...

  • 逻辑回归模型

    1.逻辑回归介绍2.机器学习中的逻辑回归3.逻辑回归面试总结4.逻辑回归算法原理推导5.逻辑回归(logistic...

  • Task 01|基于逻辑回归的分类预测

    知识背景 关于逻辑回归的几个问题 逻辑回归相比线性回归,有何异同? 逻辑回归和线性回归最大的不同点是逻辑回归解决的...

  • 11. 分类算法-逻辑回归

    逻辑回归 逻辑回归是解决二分类问题的利器 逻辑回归公式 sklearn逻辑回归的API sklearn.linea...

  • 机器学习100天-Day4-6逻辑回归

    逻辑回归(Logistic Regression) 什么是逻辑回归 逻辑回归被用于对不同问题进行分类。在这里,逻辑...

  • 逻辑回归

    逻辑回归 线性回归 to 逻辑回归 本质:逻辑回归的本质就是在线性回归的基础上做了一个非线性的映射(变换),使得算...

  • 吴恩达机器学习笔记(2)

    一.逻辑回归 1.什么是逻辑回归? 逻辑回归是一种预测变量为离散值0或1情况下的分类问题,在逻辑回归中,假设函数。...

  • SKlearn_逻辑回归小练习

    逻辑回归 逻辑回归(Logistic regression 或logit regression),即逻辑模型(英语...

网友评论

      本文标题:逻辑回归(一)

      本文链接:https://www.haomeiwen.com/subject/lleucdtx.html