【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
2020-11-26 09:53
标签:机器学习 python 逻辑回归 (转载请注明出处:http://blog.csdn.net/buptgshengod)
[[ 4.12414349] [ 0.48007329] [-0.6168482 ]] 【机器学习算法-python实现】逻辑回归的实现(LogicalRegression) 标签:机器学习 python 逻辑回归 原文地址:http://blog.csdn.net/buptgshengod/article/details/247150351.背景知识
(1)回归
(2)sigmoid函数
(3)梯度上升算法
2.代码
def loadDataSet():
dataMat = []; labelMat = []
fr = open(‘/Users/hakuri/Desktop/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
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))
for k in range(maxCycles): #heavy on matrix operations
h = sigmoid(dataMatrix*weights) #matrix mult
error = (labelMat - h) #vector subtraction
weights = weights + alpha * dataMatrix.transpose()* error #matrix mult
return weights
3.代码
上一篇:PHP 布尔类型
下一篇:C语言中static的使用
文章标题:【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
文章链接:http://soscw.com/index.php/essay/22620.html