38

10 逻辑回归

 4 years ago
source link: http://www.cnblogs.com/hp-lake/p/11979505.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

10 逻辑回归

分类算法-逻辑回归

应用场景 (二分类)

  • 广告点击率 (典型的二分类问题,点了或者没点,也能得出)
  • 是否为垃圾邮件
  • 是否患病
  • 金融诈骗
  • 虚假账号

逻辑回归定义

  1. 逻辑回归: 是一种分类算法,使用线性回归的式子作为输入,通过sigmoid函数转换为概率问题。
  2. sigmoid函数:1/(1+e^-x), 将输入值x,映射到(0,1), 与概率值联系起来

    yq6jema.png!web
  3. 逻辑回归公式

    zABZBji.png!web

线性回归的输入 --> sigmoid 转换 -->分类 [0,1] 概率值, 阈值一般取0.5

  1. 逻辑回归的损失函数、优化
  • 与线性回归原理相同,但由于是分类问题,损失函数不一样,只能通过梯度下降求解
  • 对数似然损失函数:
    iQzeaa3.png!web

y =1, 目标值为1 ,预测值为1的概率是100%, 则损失最小(接近0)

注: 哪一类别数据量小,则哪一种特征为正例1

zeqQRrq.png!web

目标值是0类,预测1类的概率越大,则损失越大。(预测属于0的概率越大越好)

只判断属于一个类别的概率,这里是属于1的概率,如果属于1的概率较小(小于阈值),则非1,即为0。

Z322emR.png!web
  • 完整损失函数举例:(损失越小,精度越高)

    bammIz6.png!web

损失函数

均方误差

  • 只有一个最小值,不存在多个局部最低点

对数似然函数

  • 多个局部最小值,使用梯度下降时,会存在问题,尽管没有全局最低点,但效果还可以。
  • 改善方法:
    • 随机初始化,多次比较最小值结果
    • 求解过程中,调整学习率
      aYNN3mU.png!web

逻辑回归案例

良/恶性乳腺癌数据

  • 数据描述

    (1)699条样本,共11列数据,第一列用语检索的id,后9列分别是与肿瘤

    相关的医学特征,最后一列表示肿瘤类型的数值。

    (2)包含16个缺失值,用”?”标出。

from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, classification_report
from sklearn.externals import joblib
import pandas as pd
import numpy as np


def logistic():
    """
    逻辑回归做二分类癌症预测
    :return: None
    """
    # 1.读取数据
    # 构造列标签名字
    column = ['Sample code number','Clump Thickness', 'Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion','Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
    data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names=column)
    print(data)

    # 2.缺失值处理
    data = data.replace(to_replace='?', value=np.nan)
    data = data.dropna() # 直接删除nan

    # 3. 进行数据分割
    x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25) # 取特征值,目标值

    # 4.标准化处理 (分类问题,目标值不做标准化)
    std = StandardScaler()
    x_train = std.fit_transform(x_train)
    x_test = std.transform(x_test)

    # 5. 逻辑回归预测
    lg = LogisticRegression(C=1.0)
    lg.fit(x_train, y_train) # 训练lg模型
    print(lg.coef_)
    y_predict = lg.predict(x_test)
    print('准确率:', lg.score(x_test, y_test))
    print('召回率:',classification_report(y_test, y_predict, labels=[2,4], target_names=['良性','恶性']))

if __name__ == '__main__':
    logistic()

逻辑回归总结

  1. 应用:广告点击率预测,是否患病,金融诈骗,是否为虚假账号 (带有概率的二分类问题)
  2. 优点:适合需要得到一个分类概率的场景,简单,速度快
  3. 缺点:不方便处理多分类问题 1 vs 1, 1 vs

生成模型与判别模型比较

  • 有没有先验概率 ( P(C) ) 需不需要总结历史数据

    NrmM3qn.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK