Logistic回归
来源:互联网 发布:网络信息平台管理制度 编辑:程序博客网 时间:2024/05/16 16:56
Logistic 回归
- 优点:
- 易于理解,计算量不大
- 缺点
- 容易欠拟合,精度不高
- 适用数据类型:
- 数值型、标称型
Sigmoid函数
Sigmoid函数:
Sigmoid函数的输入记为z,由下面公式得出:
式中,向量x为分类器的输入数据,向量w为最佳回归系数。
最佳回归系数的确定——梯度上升法
- 思想:要找到某函数的最大值,最好的方法就是沿着该函数的梯度方向探寻。
公式:梯度记为,则函数f(x,y)的梯度(a = ,b=):
用向量来表示的话,梯度上升法的迭代公式(a为步长):
梯度下降法求函数的最小值;梯度上升法求函数的最大值 。
Logistic Python实现
#加载数据def loadDataSet(): data = []#存样本 label = []#存标签 with open('testSet.txt','r') as r_file:#打开数据文件 for line in r_file: line = line.strip().split()#去空格,并切割 data.append([1.0,float(line[0]),float(line[1])])#为方便计算,引入新列 label.append(int(line[2])) return data,label#sigmoid函数def sigmoid(inx): return 1.0/(1+exp(-inx))#梯度上升法def gradAscent(data,label): data = mat(data)#转矩阵 label = mat(label).transpose()#转矩阵,并转置 m,n=shape(data) a = 0.001#设置步长 maxCycles = 500#设置循环次数 weights = ones((n,1)) for i in xrange(maxCycles): h = sigmoid(data*weights) error = label - h weights = weights + a*data.transpose()*error return weights#画决策边界def plotBestFit(weights): import matplotlib.pyplot as plt#导入画图模块 data,label=loadDataSet() dataArray = array(data)#转数组 n = shape(dataArray)[0]#样本数 x1=[];y1=[];x2=[];y2=[] for i in xrange(n): if int(label[i])==1:#正类 x1.append(dataArray[i,1]) y1.append(dataArray[i,2]) else:#负类 x2.append(dataArray[i,1]) y2.append(dataArray[i,2]) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x1,y1,s=30,c='red',marker='s') ax.scatter(x2,y2,s=30,c='green') x = arange(-3.0, 3.0, 0.01) y = (-weights[0] - weights[1]*x)/weights[2]#最佳拟合直线 ax.plot(x, y) plt.xlabel('X1'); plt.ylabel('X2'); plt.show()
注意:函数gradAscent(data,label)计算得出的结果weights是矩阵,作为参数传入函数plotBestFit(weights)时,需要转化为数组。方法:
- plotBestFit(weights.getA())
- plotBestFit(array(weights))
0 0
- Logistic回归
- Logistic回归
- logistic回归
- Logistic 回归
- Logistic 回归
- logistic回归
- Logistic回归
- Logistic回归
- Logistic回归
- Logistic回归
- logistic回归
- Logistic回归
- logistic回归
- Logistic 回归
- logistic回归
- Logistic回归
- logistic回归
- Logistic回归
- udp通讯_飞秋
- CbsCoreSetState
- C语言控制台下实现模拟密码的输入
- hdu 5319 Painter(DFS)
- Linux常用的工具命令
- Logistic回归
- jsp内置对象之application对象
- tcp通讯
- C#泛型类
- 主管给实习生新人的一封邮件,字字珠玑,用心良苦
- javascript_事件
- 排序 数据结构
- 聚集索引与非聚集索引
- 软件编程规范总则