import math
import numpy as np
def loadDataSet():
dataMat = []
labelMat = []
fr = open("testSet.txt")
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelMat.append(lineArr[2])
return dataMat, labelMat
def sigmoid(inX):
return 1.0 / (1 + np.exp(-inX)) # 使用math.exp()会报错,因为inx是numpy类型的
def gradAscent(dataMatIn, classLabels):
dataMatrix = np.mat(dataMatIn)
labelMat = np.mat(classLabels).transpose() # transpose在不指定参数是默认是矩阵转置
# labelMat = np.mat(classLabels).T
m, n = np.shape(dataMatrix)
alpha = 0.001
maxCycles = 500
weights = np.ones((n, 1))
for k in range(maxCycles):
h = sigmoid(dataMatrix * weights)
# print(type(h))
# print(labelMat)
# print(h)
error = (labelMat - h)
weights = weights + alpha * dataMatrix.transpose() * error
return weights
dataArr, labelMat = loadDataSet()
# print(dataArr)
# print(labelMat)
print(gradAscent(dataArr, labelMat))
运行报错:
C:\Users\ktcuser\AppData\Local\Programs\Python\Python36-32\python.exe F:/Logistic/logistic.py
Traceback (most recent call last):
File "F:/Logistic/logistic.py", line 41, in <module>
print(gradAscent(dataArr, labelMat))
File "F:/Logistic/logistic.py", line 33, in gradAscent
error = (labelMat - h)
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
Process finished with exit code 1
原因:
labelMat.append(int(lineArr[2])) #此处要转换一下为int 否则error = (labelMat - h) 报错
def loadDataSet():
dataMat = []
labelMat = []
fr = open("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])) #此处要转换一下为int 否则error = (labelMat - h) 报错
# labelMat.append(lineArr[2])
return dataMat, labelMat