基于scikit learn的logistic回归实现



(logistic regression)属于概率型非线性回归,它是研究二分类观察结果与一些影响因素之间关系的一种多变量分析方法。在流行病学研究中,经常需要分析疾病与各危险因素之间的定量关系,为了正确说明这种关系,需要排除一些混杂因素的影响。传统上常常使用Mantel-Haenszel分层分析方法,但这一方法适用于样本含量大,分析因素较少的情况。如果用线性回归分析,由于应变量Y是一个二值变量(通常取值1或0),不满足应用条件,尤其当各因素都处于低水平或高水平时,预测值Y值可能超出0~1范围,出现不合理都现象。用logistic回归分析则可以较好的解决上述问题。

与多重线性回归的比较
logistic回归(Logistic regression) 与 多重线性回归实际上有很多相同之处,最大的区别就在于他们的因变量不同,其他的基本都差不多,正是因为如此,这两种回归可以归于同一个家族,即 广义线性模型(generalized linear model)。这一家族中的模型形式基本上都差不多,不同的就是 因变量不同,如果是连续的,就是多重线性回归,如果是 二项分布,就是logistic回归,如果是poisson分布,就是poisson回归,如果是 负二项分布,就是负二项回归,等等。只要注意区分它们的因变量就可以了。 [1]  
logistic回归的因变量可以是二分非线性差分方程类的,也可以是多分类的,但是二分类的更为常用,也更加容易解释。所以实际中最为常用的就是二分类的logistic回归。
logistic回归目标函数为:
其中Z可表示为:
x为特征值
这里用logistic回归实现的三分类不是常用分类,代码实现如下:
#!/usr/bin/python
# -*- coding:utf-8 -*-

import csv
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt


def iris_type(s):
    it = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}
    return it[s]


if __name__ == "__main__":
    path = u'D:\\testData\\4.iris.data'  # 数据文件路径

    # 路径,浮点型数据,逗号分隔,第4列使用函数iris_type单独处理
    data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})

    # 将数据的0到3列组成x,第4列得到y
    x, y = np.split(data, (4,), axis=1)

    # 为了可视化,仅使用前两列特征
    x = x[:, :2]

    # print x
    # print y

    logreg = LogisticRegression()   # Logistic回归模型
    logreg.fit(x, y.ravel())        # 根据数据[x,y],计算回归参数

    # 画图
    N, M = 500, 500     # 横纵各采样多少个值
    x1_min, x1_max = x[:, 0].min(), x[:, 0].max()   # 第0列的范围
    x2_min, x2_max = x[:, 1].min(), x[:, 1].max()   # 第1列的范围
    t1 = np.linspace(x1_min, x1_max, N)
    t2 = np.linspace(x2_min, x2_max, M)
    x1, x2 = np.meshgrid(t1, t2)                    # 生成网格采样点
    x_test = np.stack((x1.flat, x2.flat), axis=1)   # 测试点

    # # 无意义,只是为了凑另外两个维度
    # x3 = np.ones(x1.size) * np.average(x[:, 2])
    # x4 = np.ones(x1.size) * np.average(x[:, 3])
    # x_test = np.stack((x1.flat, x2.flat, x3, x4), axis=1)  # 测试点

    y_hat = logreg.predict(x_test)                  # 预测值
    y_hat = y_hat.reshape(x1.shape)                 # 使之与输入的形状相同
    plt.pcolormesh(x1, x2, y_hat, cmap=plt.cm.Spectral, alpha=0.1)  # 预测值的显示Paired/Spectral/coolwarm/summer/spring/OrRd/Oranges
    plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', cmap=plt.cm.prism)  # 样本的显示
    plt.xlabel('Sepal length')
    plt.ylabel('Sepal width')
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)
    plt.grid()
    plt.show()

    # 训练集上的预测结果
    y_hat = dt_clf.predict(x)
    y = y.reshape(-1)       # 此转置仅仅为了print时能够集中显示
    print y_hat.shape       # 不妨显示下y_hat的形状
    print y.shape
    result = (y_hat == y)   # True则预测正确,False则预测错误
    print y_hat
    print y
    print result
    c = np.count_nonzero(result)    # 统计预测正确的个数
    print c
    print 'Accuracy: %.2f%%' % (100 * float(c) / float(len(result)))
 
  
运行结果如下图:
 
 

猜你喜欢

转载自blog.csdn.net/liu_xiao_cheng/article/details/52778580
今日推荐