赞
踩
NameError: name 'weights' is not defined
Root Cause: missing code :
- # add this code
- weights = logRegres.gradAscent(dataArr,labelMat)
Issue 2:
- 如果是矩阵会报错:
- x and y must have same first dimension, but have shapes (60,) and (1, 60)
-
- x = arange(-3.0, 3.0, 0.1),len(x) = [3-(-3)]/0.1 = 60
- weights是矩阵的话,y = (-weights[0]-weights[1]*x)/weights[2],len(y) = 1
Modified Code as below:
- # Plotting the logistic regression best-fit line and dataset.
-
- def plotBestFit(weights):
- import matplotlib.pyplot as plt
- dataMat, labelMat = loadDataSet()
- dataArr = np.array(dataMat)
- n = np.shape(dataArr)[0]
- xcord1 = []; ycord1 = []
- xcord2 = []; ycord2 = []
- for i in range(n):
- if int(labelMat[i]) == 1:
- xcord1.append(dataArr[i, 1]); ycord1.append(dataArr[i, 2])
- else:
- xcord2.append(dataArr[i, 1]); ycord2.append(dataArr[i, 2])
- fig = plt.figure()
- ax = fig.add_subplot(111)
- ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
- ax.scatter(xcord2, ycord2, s=30, c='green')
- x = np.arange(-3.0, 3.0, 0.1)
- y = (-weights[0] - weights[1] * x) / weights[2]
- y = y.reshape((60,1)) # add this code to fix this issue
- ax.plot(x, y)
- plt.title('BestFit')
- plt.xlabel('X1'); plt.ylabel('X2')
- plt.show()
Display:
- import logRegres
- import imp
- imp.reload(logRegres)
- weights = logRegres.gradAscent(dataArr,labelMat)
- logRegres.plotBestFit(weights)
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。