Error display:

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 pltdataMat, 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)

NameError: name ‘weights‘ is not defined相关推荐

  1. NameError: name xx is not defined

    这个问题有很多类型的,这里说下我遇到的这个类型 首先我是获取input 值之后提示NameError: name xx is not defined 最后发现是python 2x 与 python 3 ...

  2. Python报错:NameError: name ‘wraps‘ is not defined

    @wraps(func) NameError: name 'wraps' is not defined 解决方法: (1)安装functiontools:pip install functiontoo ...

  3. NameError: name 'go' is not defined

    最近在学习fbprophet时,按照安装步骤,搭建好环境之后,使用官方教程进行学习.在执行到下面这句时报错 fig = plot_plotly(m, forecast) # This returns ...

  4. Python 解决 :NameError: name ‘reload‘ is not defined 问题

    Python 解决 :NameError: name 'reload' is not defined 问题 参考文章: (1)Python 解决 :NameError: name 'reload' i ...

  5. 【错误记录】执行 Python 程序报错 ( NameError: name ‘reload‘ is not defined )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 Windows 的 cmd 命令行运行 python 脚本时 , 报如下错误 : 执行 python ApkTool.py -analyse - ...

  6. py+selenium 报错NameError: name 'NoSuchElementException' is not defined【已解决】

     报错:NameError: name 'NoSuchElementException' is not defined  如图 解决方法: 头部加一句:from selenium.common.exc ...

  7. 成功解决NameError: name ‘norm‘ is not defined

    成功解决NameError: name 'norm' is not defined 目录 解决问题 解决思路 解决方法 解决问题 NameError: name 'norm' is not defin ...

  8. 成功解决NameError: name 'file' is not defined

    成功解决NameError: name 'file' is not defined 目录 解决问题 解决思路 解决方法 解决问题 NameError: name 'file' is not defin ...

  9. 成功解决NameError: name 'apply' is not defined

    成功解决NameError: name 'apply' is not defined 目录 解决问题 解决思路 解决方法 解决问题 NameError: name 'apply' is not def ...

最新文章

  1. 脚本:获取CSDN文章的访问量
  2. python文件名带日期变量_Python实现文件按照日期命名的方法
  3. 极致无边界!2021 神策数据 Tech-Day 即将开幕
  4. NYOJ 题目528 找球号(三)
  5. 我在德国做SAP CRM One Order redesign工作的心得
  6. C#操作 MongoDB【原创】
  7. matlab zigzag算法,ZIGZAG扫描的MATLAB实现
  8. apk提取加密素材_从apk包中提取unity资源
  9. ktor框架用到了netty吗_Netty系列相关面试题汇总
  10. 人设倒了扶起来:Lazarus 组织利用含木马的IDA Pro 攻击研究员
  11. UE4中Component和Subobject的区别
  12. 此声明没有存储类或类型说明符
  13. 随笔:谈谈考研二战如何准备
  14. php c端,tob端和toc端是什么意思
  15. UOS服务器操作系统部署EKL
  16. html调用手机NFC,怎样使用手机的NFC功能模拟门禁?
  17. Origin中输入希腊字母
  18. Springboot高校学生实习档案管理毕业设计-附源码221508
  19. android之RecyclerView.Adapter介绍
  20. 腾讯云服务器域名解析操作详解

热门文章

  1. 计算机控制系统生产现场应用,浅析计算机控制系统在工业现场生产中的应用.doc...
  2. C# 使用SharpGL-Perspective和LookAt
  3. 微信小程序并发服务器架构,「系统架构」如何设计一个健壮高效的微信小程序登录方案...
  4. python能制作ppt动画效果吗_我可以用Python制作动画吗?
  5. Flooded! POJ - 1877 模拟题
  6. 视频服务器是什么?要如何搭建?
  7. C++:identifier “string“ is undefined
  8. 百分位(percentile)是什么概念?怎么理解第95个百分位(95th percentile)
  9. 全球首家 MUJI 酒店,这里有一份事无巨细的探店体验
  10. 【HTML5+CSS】美观的button设置