机器学习 task 1 逻辑回归

一、概要


  • 逻辑回归(Logistic regression,简称LR),一种分类算法,常用于二分类,也可用于多分类。
  • 逻辑回归模型
    • sigmoid函数
    • 似然函数和损失函数
    • 梯度下降法

二、主要知识点


  • LR的最终目的是找到w的最佳取值,使得预测结果更准确。
    在这里插入图片描述
    在这里插入图片描述

sigmoid

  • 函数图像如下:
  • sigmod将预测结果限定在[0-1]之间,正好可以作为该结果出现的概率

似然函数和损失函数

  • LR中,w的取值是通过最大化似然函数或最小化损失函数得到的,常用梯度下降,牛顿法等。
    参考链接

三、code实战


  • 各种画图函数没学好,emm…
  1. 绘制散点图:plt.scatter()
import matplotlib.pyplot as plt
plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, 
norm=None, vmin=None, vmax=None, alpha=None,
 linewidths=None, verts=None, edgecolors=None, *, 
 data=None, **kwargs)
  • 参数说明:
    x,y:实数或数组,所有散点的x,y值
    s:实数或数组,点的面积
    c:字符或数组,点的颜色,默认是蓝色‘b’
    marker:点的形状样式,默认是’o’(圆点)
    norm:将数据亮度转化到0-1之间,只有c是一个浮点数的数组的时候才使用。默认colors.Normalize
    vmin,vmax:实数,norm存在时忽略。用来进行亮度数据的归一化
    alpha实数,0-1之间
    linewidths:实数或数组,点的长度
  1. 划分数据集:
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(iris_features_part, iris_target_part, test_size = 0.2, random_state = 2020)
  1. 导入逻辑回归模型:
from sklearn.linear_model import LogisticRegression
## 定义 逻辑回归模型 
clf = LogisticRegression(random_state=0, solver='lbfgs')
  • LogisticRegression()
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=0, solver='lbfgs', tol=0.0001,
          verbose=0, warm_start=False)
  1. 预测:
    clf.predict()
## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
  1. 精确度:
    metrics.accuracy_score()
##训练集和测试集上的预测精度
from sklearn import metrics
print(metrics.accuracy_score(y_train,train_predict))
print(metrics.accuracy_score(y_test,test_predict))

四、总结

  1. 各种画图函数
  2. 逻辑回归模型具备可解释性,实现起来相对简单,是一个非常强大的分类器。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值