理论参考-特别是梯度下降权值更新向量化的过程
http://www.cnblogs.com/nsnow/p/4540700.html
http://www.cnblogs.com/nsnow/p/4540700.html
http://blog.youkuaiyun.com/zouxy09/article/details/20319673
1定义特征与标签矩阵
2定义sigmoid函数
3根据推出的权值更新公式进行迭代-最后算出权值
可选的训练方式有梯度上升,随机梯度上升,改进的随机梯度上升(alpha会变化)
4测试样本乘以权值,结果输入sigmoid函数,输出大于0.5为1类,小于等于0.5为0类
#coding:utf-8
from numpy import *
#获取数据,拼接成两个矩阵
def loadDataSet():
dataMat = [];
labelMat = []
fr = open('F:/python/pythonwork/testSet.txt')
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelMat.append(int(lineArr[2]))
return dataMat, labelMat
#定义sigmoid函数
def sigmoid(inX):
return 1.0 / (1 + exp(-inX))
#定义梯度上升训练方法(负号梯度下降)-每次全部样本参与更新迭代
def gradAscent(dataMatIn, classLabels):
dataMatrix = mat(dataMatIn) # convert to NumPy matrix
labelMat = mat(classLabels).transpose() # convert to NumPy matrix
m, n = shape(dataMatrix) #矩阵的行,列
alpha = 0.001
maxCycles = 500 #迭代次数
weights = ones((n, 1)) #初始权重 3x1
for k in range(maxCycles): # heavy on matrix operations
h = sigmoid(dataMatrix * weights) # matrix mult 100x3 3x1
error = (labelMat - h) # vector subtraction
weights = weights + alpha * dataMatrix.transpose() * error # matrix mult transpose矩阵转账-梯度下降权值更新向量化
return weights
#随机梯度上升算法-单个样本值更新迭代
def stocGradAscent0(dataMatrix, classLabels):
m, n = shape(dataMatrix)
alpha = 0.01
weights = ones(n) # initialize to all ones
for i in range(m):
h = sigmoid(sum(dataMatrix[i] * weights))
error = classLabels[i] - h
weights = weights + alpha * error * dataMatrix[i]
return weights
#改进的随机梯度上升算法--alpha每次调整-会随着迭代次数不断减小, 但永远不会减小到0
def stocGradAscent1(dataMatrix, classLabels, numIter=150):
m, n = shape(dataMatrix)
weights = ones(n) # initialize to all ones
for j in range(numIter):
dataIndex = range(m)
for i in range(m):
alpha = 4 / (1.0 + j + i) + 0.0001 # apha decreases with iteration, does not
randIndex = int(random.uniform(0, len(dataIndex))) # go to 0 because of the constant
h = sigmoid(sum(dataMatrix[randIndex] * weights))
error = classLabels[randIndex] - h
weights = weights + alpha * error * dataMatrix[randIndex]
del (dataIndex[randIndex])
return weights
#测试样本分类
def classifyVector(inX, weights):
prob = sigmoid(sum(inX * weights))
if prob > 0.5:
return 1.0
else:
return 0.0