Logistic regression算法
Python代码
代码语言:javascript
复制import numpy as np
def loadData(filename):
dataset = []; labels = []
with open(filename) as file:
for line in file.readlines():
lineArr = line.strip().split()
dataset.append([1.0, float(lineArr[0]), float(lineArr[1])])
labels.append(int(lineArr[-1]))
return dataset, labels
def sigomid(x):
return 1.0/(1 np.exp(-x))
def gradient_ascent(dataset, labels, alpha, max_iter):
dataMat = np.matrix(dataset)
labelMat = np.matrix(labels).reshape(-1, 1)
m,n = dataMat.shape
w = np.ones((n,1))
for i in range(max_iter):
error = labelMat - sigomid(dataMat*w)
w = alpha * dataMat.T * error
return w
def main():
filename = "dataset.txt"
dataset, labels = loadData(filename)
weights = gradient_ascent(dataset, labels, alpha=0.001, max_iter=500)
print(weights)
if __name__ == '__main__':
main()
结果
代码语言:javascript
复制[[ 4.12414349]
[ 0.48007329]
[-0.6168482 ]]