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

逻辑回归(二)

作者: y_7539 | 来源:发表于2023-01-08 14:28 被阅读0次
image.png
image.png
# 模型训练
log_model = LogisticRegression()
log_model.fit(new_x, y)

from sklearn.metrics import accuracy_score

# 查看准确率
predict_y = log_model.predict(new_x)
accuracy_score(y, predict_y)   #准确率0.8135593220338984

# 模型系数
the0 = log_model.intercept_
the1, the2, the3, the4, the5 = log_model.coef_[0]
print(the0, the1, the2, the3, the4, the5)

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

# 求出新的边界
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)
x2_new_boundray2 = (-b - np.sqrt(b*b -4*a*c)) / (2*a)

# 添加mask
mask = df["pass"] == 1

plt.figure()
passed = plt.scatter(df["test1"][mask], df["test2"][mask])
failed = plt.scatter(df["test1"][~mask], df["test2"][~mask])
t = plt.plot(x1_new, x2_new_boundray)
t2 = plt.plot(x1_new, x2_new_boundray2)
plt.xlabel("test1")
plt.ylabel("test2")
plt.legend((passed, failed), ("passed", "failed"))
plt.show()
image.png
# 封装边界函数
def f(x):
    # 求出新的边界
    a = the4
    b = the2 + the5 * x
    c = the0 + the1*x + the3*x* x
    # 新的边界
    boundray1 = (-b + np.sqrt(b*b -4*a*c)) / (2*a)
    boundray2 = (-b - np.sqrt(b*b -4*a*c)) / (2*a)
    return boundray1, boundray2
def boundray(model, x):
    """
    :param model: trained model
    :param x: sorted x1
    :return: boundray1 boundray2  boundray line
    """
    # 模型系数
    the0 = model.intercept_
    the1, the2, the3, the4, the5 = model.coef_[0]
    # 求出新的边界
    a = the4
    b = the2 + the5 * x
    c = the0 + the1*x + the3*x* x
    # 新的边界
    boundray1 = (-b + np.sqrt(b*b -4*a*c)) / (2*a)
    boundray1 = (-b - np.sqrt(b*b -4*a*c)) / (2*a)
    return boundray1, boundray2
x2_new_boundray1 = []
x2_new_boundray2 = []
for x in x1_new:
    x2_new_boundray1.append(boundray(log_model, x)[0])
    x2_new_boundray2.append(boundray(log_model, x)[1])
image.png
#生成数据画边界
x1_range = np.array([-0.9 + x/10000 for x in range(19000)])
x2_new_boundray1 = []
x2_new_boundray2 = []
for x in x1_range:
    x2_new_boundray1.append(f(x)[0])
    x2_new_boundray2.append(f(x)[1])
image.png

相关文章

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

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

  • 16-逻辑回归

    逻辑回归 应用场景:二分类问题。逻辑回归也能得出概率值 逻辑回归为什么叫逻辑回归:因为是二分类,结果非此即彼,与逻...

  • 机器学习之逻辑回归

    本文将通过以下几部分来讲解逻辑回归: 一.分类和回归任务的区别 二.逻辑回归不是回归 ...

  • 机器学习随笔

    逻辑回归- 逻辑回归的重要性二项逻辑回归概率和odds的定义概率odds伯努利分布sigmoid 函数及特性- 似...

  • 逻辑回归(上)

    逻辑回归- 逻辑回归的重要性二项逻辑回归概率和odds的定义概率odds伯努利分布sigmoid 函数及特性- 似...

  • 【原创】逻辑回归基本概念梳理

    逻辑回归 前言 1、逻辑回归是经典二分类方法、不是回归方法 2、机器算法中,先选简单算法、后复杂,逻辑回归特别简单...

  • 逻辑回归

    逻辑回归 0.引言 本文主要参考了李航的《统计学习方法》,算做学习笔记。 1. 二元逻辑回归模型定义 二元逻辑回归...

  • 逻辑斯谛回归&最大熵模型

    1、逻辑斯谛回归模型 逻辑斯谛回归是经典的分类方法。二项逻辑斯谛回归模型是如下条件概率分布: 下面我们考察逻辑斯谛...

  • 逻辑回归(二)

    代价函数(Cost Function) 对于线性回归模型,我们定义的代价函数J(θ)为: 现在对于逻辑回归模型我们...

  • 逻辑回归公式推导

    逻辑回归 逻辑回归到底是分类还是回归?它是经典的二分类算法 机器学习算法选择:先逻辑回归再用复杂的,能简单的还是用...

网友评论

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

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