利用一条直线对给定数据点进行拟合(该线称为最佳拟合直线),这个拟合过程叫回归分析,利用logistic回归分类的主要思想是:根据现有数据对分类边界线建立回归公式。
logistic
优点:计算代价不高,易于理解和实现
缺点:容易欠拟合,分类精度可能不高
适用的数据类型:数值型和标称型数据
对于二分类问题的分类函数最佳可想到单位阶跃函数,1和0分类分明,而现实难以实现。在数据上选择更易处理的sigmoid函数
整体和单位阶跃函数相似
极大似然计算可知:对线性回归,logistic回归和一般回归的认识
w :=w +(y - δ)X
#logistic 学习笔记
from numpy import *
def loadDataSet(filepath):
dataMat = [] # 100*3
labelMat =[]
fr = open(filepath)
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0 ,float(lineArr[0]), float(lineArr[1])]) #补X0=1.0
labelMat.append(int(lineArr[2])) #1*100
return dataMat,labelMat
def sigmoid(inX):
return 1.0/(1+exp(-inX))
def gradAscent(dataMatIn,classLabels):
dataMatrix = mat(dataMatIn)
labelMat = mat(classLabels).transpose() #转换成数组和转置100*1
m,n = shape(dataMatrix)
alpha = 0.001 #步进系数
maxCycles = 500
weights = ones((n,1))#系数初识化为 1
for k in range(maxCycles):
h = sigmoid(dataMatrix*weights)
error = (labelMat - h)
weights = weights + alpha * dataMatrix.transpose() * error # matrix mult
return weights
测试
from ML_Learn.com.ML.Class.logistic import logistic
dataMat,labelMat = logistic.loadDataSet('/Users/hjw/Documents/Java/python/ML_Learn/ML/ML_Learn/com/ML/Class/logistic/resources/testSet.txt')
weights = logistic.gradAscent(dataMat,labelMat)
print(weights)
# [[ 4.12414349]
# [ 0.48007329]
# [-0.6168482 ]]