您当前的位置: 首页 >  彭世瑜 逻辑回归

机器学习:逻辑回归预测癌症数据

彭世瑜 发布时间:2019-01-25 23:40:40 ,浏览量:3

逻辑回归公式

h θ ( x ) = g ( θ T x ) = 1 1 + e − θ T x h_\theta(x) = g(\theta^Tx)=\frac{1}{1+e^{-\theta^Tx}} hθ​(x)=g(θTx)=1+e−θTx1​ g ( z ) = 1 1 + e − z g(z) = \frac{1}{1+e^{-z}} g(z)=1+e−z1​

sigmoid函数g(z) 过点:(0, 0.5) 映射区间:(负无穷, 正无穷) -> (0, 1) e 是常数 2.71828…

逻辑回归的损失函数

对数似然损失函数 c o s t ( h θ ( x ) , y ) = { − l o g ( h θ ( x ) ) y=1 − l o g ( 1 − h θ ( x ) ) y=0 cost(h_\theta(x), y) = \begin{cases} -log(h_\theta(x))& \text{y=1}\\ -log(1- h_\theta(x))& \text{y=0} \end{cases} cost(hθ​(x),y)={−log(hθ​(x))−log(1−hθ​(x))​y=1y=0​ 完整的损失函数 c o s t ( h θ ( x ) , y ) = ∑ i = 1 m − y i l o g ( h θ ( x ) ) − ( 1 − y i ) l o g ( 1 − h θ ( x ) ) cost(h_\theta(x), y) =\sum_{i=1}^{m}-y_ilog(h_\theta(x)) -(1-y_i)log(1- h_\theta(x)) cost(hθ​(x),y)=i=1∑m​−yi​log(hθ​(x))−(1−yi​)log(1−hθ​(x)) cost 损失值越小,那么预测的类别准确度更高

逻辑回归

线性回归的式子作为逻辑回归的输入

逻辑回归的损失函数,优化 与线性回归原理相同,但由于是分类问题,损失函数不一样,只能通过梯度下降求解

算法 策略 优化 逻辑回归 对数似然损失 梯度下降

损失函数(梯度下降求解) -均方误差(不存在多个局部最低点,只有一个最小值) -对数似然损失(多个局部最小值) -1、多次随机初始化,多次比较最小值结果 -2、求解过程中,调整学习率,劲量改善 -3、尽管没有全局最低点,但是效果都是不错的

逻辑回归,属于1的概率值

哪个类别少,判定概率值是指的这个类别 恶性 正例 良性 反例

逻辑回归应用

广告点击率预测,是否患病,金融诈骗,是否为虚假账号

优点:适合需要得到一个分类概率的场景,简单速度快

缺点:不好处理多分类问题

softmax方法,逻辑回归在多分类问题上的推广

区别

        逻辑回归            朴素贝叶斯
解决问题    二分类             多分类
应用场景    癌症,二分类需要概率  文本分类
参数        正则化力度         没有参数
模型        判别模型           生成模型
先验概率     不需要             需要
共同点      得出的结果都有概率解释

判别模型:K-近邻,决策树,随机森林,神经网络 生成模型:隐马尔科夫模型

代码示例
# -*- coding: utf-8 -*-

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report

# 加载数据
breast = load_breast_cancer()

# 数据拆分
X_train, X_test, y_train, y_test = train_test_split(
    breast.data, breast.target)

# 数据标准化
std = StandardScaler()
X_train = std.fit_transform(X_train)
X_test = std.transform(X_test)

# 训练预测
lg = LogisticRegression()

lg.fit(X_train, y_train)

y_predict = lg.predict(X_test)

# 查看训练准确度和预测报告
print(lg.score(X_test, y_test))
print(classification_report(
    y_test, y_predict, labels=[0, 1], target_names=["良性", "恶性"]))

"""
0.958041958041958
             precision    recall  f1-score   support

         良性       0.98      0.90      0.93        48
         恶性       0.95      0.99      0.97        95

avg / total       0.96      0.96      0.96       143

"""
关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 3浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0547s