Logistic回归---从疝气病症预测马的死亡率

本文介绍了一种改进的随机梯度上升算法,并通过Logistic回归进行分类预测。该算法利用随机选取样本来减少周期性波动,适用于大规模数据集。通过对马匹结肠炎数据集的测试,评估了算法的有效性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python  
# -*- coding: utf-8 -*-  
#coding=utf-8

from numpy import *

#打开文本文件并逐行读取,每行前两个值为x1,x2,第3列为类别标签
#为方便计算,将x0设为1.0
def loadDataSet():
    dataMat = []; labelMat = []
    fr = open('testSet.txt')
    for line in fr.readlines():
        lineArr = line.split('\t')
        dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
        labelMat.append(int(lineArr[2]))
    return dataMat, labelMat

def sigmoid(inX):
    return 1.0/(1 + exp(-inX))

#改进的随机梯度上升算法
def stocGradAscent(dataMatrix, classLabel, numIter = 150):
    m, n = shape(dataMatrix)
    weights = ones(n)  #回归系数值初始化为1
    for j in range(numIter):
        dataIndex = range(m)
        for i in range(m):
            alpha = 4/(1.0 + j + i) + 0.0001  #alpha每次迭代是需要调整,不断减小,但不会到0,缓解数据波动
            randIndex = int(random.uniform(0, len(dataIndex)))  #随机选取更新
            h = sigmoid(sum(dataMatrix[randIndex] * weights))
            error = classLabel[randIndex] - h
            weights = weights + alpha * error * dataMatrix[randIndex]  #用alpha*gradient更新回归系数值,随机选取样本,减少周期性波动
            del(dataIndex[randIndex])
    return weights

#测试算法,用Logistic回归进行分类
#输入参数为回归系数和特征向量
def classifyVector(inX, weights):
    prob = sigmoid(sum(inX * weights))
    if prob > 0.5:
        return 1.0
    else:
        return 0.0

#打开测试集和训练集,并对数据进行格式化处理
def colicTest():
    frTrain = open('horseColicTraining.txt')
    frTest = open('horseColicTest.txt')
    trainingSet = []; trainingLabels = []
    for line in frTrain.readlines():
        currLine = line.strip().split('\t')
        lineArr = []
        for i in range(21):
            lineArr.append(float(currLine[i]))
        trainingSet.append(lineArr)
        trainingLabels.append(float(currLine[21]))
    trainWeights = stocGradAscent(array(trainingSet), trainingLabels, 1000)  #计算回归系数向量
    errorCount = 0; numTestVec = 0.0
    #计算测试集分类错误率
    for line in frTest.readlines():
        numTestVec += 1.0
        currLine = line.strip().split('\t')
        lineArr = []
        for i in range(21):
            lineArr.append(float(currLine[i]))
        if int(classifyVector(array(lineArr), trainWeights)) != int(currLine[21]):
            errorCount += 1
    errorRate = (float(errorCount)/numTestVec)
    print "the error rate of this test is : %f" %errorRate
    return errorRate

#多次测试
def multiTest():
    numTests = 10; errorSum = 0.0
    for k in range(numTests):
        errorSum += colicTest()
    print "after %d iterations the average error rate is : %f" %(numTests, errorSum/float(numTests))

测试:

>>> import Logistic
>>> multiTest()
the error rate of this test is : 0.358209
the error rate of this test is : 0.388060
the error rate of this test is : 0.343284
the error rate of this test is : 0.402985
the error rate of this test is : 0.402985
the error rate of this test is : 0.253731
the error rate of this test is : 0.388060
the error rate of this test is : 0.417910
the error rate of this test is : 0.298507
the error rate of this test is : 0.402985
after 10 iterations the average error rate is : 0.365672
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值