当前位置:   article > 正文

NameError: name ‘weights‘ is not defined_nameerror: name 'weights' is not defined

nameerror: name 'weights' is not defined

Error display:

NameError: name 'weights' is not defined

Root Cause: missing code :

  1. # add this code
  2. weights = logRegres.gradAscent(dataArr,labelMat)
Issue 2: 

 

  1. 如果是矩阵会报错:
  2. x and y must have same first dimension, but have shapes (60,) and (1, 60)
  3. x = arange(-3.0, 3.0, 0.1),len(x) = [3-(-3)]/0.1 = 60
  4. weights是矩阵的话,y = (-weights[0]-weights[1]*x)/weights[2],len(y) = 1

Modified Code as below:

  1. # Plotting the logistic regression best-fit line and dataset.
  2. def plotBestFit(weights):
  3. import matplotlib.pyplot as plt
  4. dataMat, labelMat = loadDataSet()
  5. dataArr = np.array(dataMat)
  6. n = np.shape(dataArr)[0]
  7. xcord1 = []; ycord1 = []
  8. xcord2 = []; ycord2 = []
  9. for i in range(n):
  10. if int(labelMat[i]) == 1:
  11. xcord1.append(dataArr[i, 1]); ycord1.append(dataArr[i, 2])
  12. else:
  13. xcord2.append(dataArr[i, 1]); ycord2.append(dataArr[i, 2])
  14. fig = plt.figure()
  15. ax = fig.add_subplot(111)
  16. ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
  17. ax.scatter(xcord2, ycord2, s=30, c='green')
  18. x = np.arange(-3.0, 3.0, 0.1)
  19. y = (-weights[0] - weights[1] * x) / weights[2]
  20. y = y.reshape((60,1)) # add this code to fix this issue
  21. ax.plot(x, y)
  22. plt.title('BestFit')
  23. plt.xlabel('X1'); plt.ylabel('X2')
  24. plt.show()

Display:

  1. import logRegres
  2. import imp
  3. imp.reload(logRegres)
  4. weights = logRegres.gradAscent(dataArr,labelMat)
  5. logRegres.plotBestFit(weights)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/670016
推荐阅读
相关标签
  

闽ICP备14008679号