在学习《Designing Machine Learning Systems with Python》(中文名《机器学习系统设计——python语言实现》)一书中,第三章第二节第三小节部分的泊松分布的python代码在python3.6上运行时报错

TypeError: '>=' not supported between instances of 'range' and 'int'

错误信息很明显,’>=’符号不支持两个类型不同的字符之间的比较,从代码中我们可以很容易知道

from scipy.stats import poisson
import matplotlib.pyplot as plt
def pois(x = 1000):xr = range(x)ps = poisson(xr)plt.plot(ps.pmf(x/2))plt.show()pois()

关键在于poisson()函数的输入,即xr这个变量,它的类型是range类型,而range类型不能与一个int类型直接判断。我们只需要对它的类型进行下修改就可以了。
我们知道我们的目的是让xr这个变量中的每一个值都与’>=’符号后的int类型数值进行下判断,并将所有结果一起返回。那么我们就可以先试下list类型
输入:

b = list(range(10))
b >= 0

输出:

Traceback (most recent call last):File "<input>", line 1, in <module>
TypeError: '>=' not supported between instances of 'list' and 'int'

看来不行,那么我们就想到了numpy库中也有一个类似的arange()函数,我们测试下。
输入:

import numpy as np
a = np.arange(10)
a >= 0

输出:

array([ True,  True,  True,  True,  True,  True,  True,  True,  True,True])

我们得到了一个array类型的数组。这就是我们想要的答案。返回书中的例子,我们的代码就应该修改为

import numpy as np
from scipy.stats import poisson
import matplotlib.pyplot as plt
def pois(x = 1000):xr = np.arange(x)ps = poisson(xr)plt.plot(ps.pmf(x/2))plt.show()pois()

我们就得到了我们想要的输出。

提示错误“'=' not supported between instances of 'range' and 'int'”相关推荐

  1. 执行Python提示《not supported between instances of str and int》

    1 n = input() 2 if n>=100:print(int(n)/10) 3 else:print(int(n)*10) 报错内容: Traceback (most recent c ...

  2. not supported between instances of 'SafeText' and 'int' 问题出现以及解决

    1.今天更新完代码,突然间报了这个错误:'<' not supported between instances of 'SafeText' and 'int' 2.主要代码如下,其中是最后一行报 ...

  3. 成功解决TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘

    成功解决TypeError: '<' not supported between instances of 'str' and 'int' 目录 解决问题 解决思路 解决方法 解决问题 Type ...

  4. 成功解决极其刁钻bug之TypeError: ‘<=‘ not supported between instances of ‘list‘ and ‘int‘

    成功解决极其刁钻bug之TypeError: '<=' not supported between instances of 'list' and 'int' 目录 解决问题 解决思路 解决方法 ...

  5. 【已解决】TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘_Python系列学习笔记

    TypeError: '<' not supported between instances of 'str' and 'int' 翻译:str类型无法与int类型比较 解决:将<两侧类型 ...

  6. ‘<’ not supported between instances of ‘str’ and ‘int’

    '<' not supported between instances of 'str' and 'int' python 中 字符串 和 整形 进行比较之后 出现的报错信息. python 中 ...

  7. TypeError: not supported between instances of str and int【python3】

    python3报错:TypeError: '>' not supported between instances of 'str' and 'int' **报错原因:**int类型没有转换为st ...

  8. 【PyCharm编辑器】之引用selenium包提示错误:Unresolved reference 'selenium' less... (Ctrl+F1)...

    一.现象还原: 当新建.py文件时,需要引用selenium中的方法时,报错,提示红波浪线: Unresolved reference 'selenium' less... (Ctrl+F1) Thi ...

  9. 在创建jsp页面之后保存时提示错误,并提示以UTF-8格式进行保存。

    在创建jsp页面之后保存时提示错误,并提示以UTF-8格式进行保存. Save could not be completed. Reason: some characters cannot be ma ...

最新文章

  1. 从哪些方面可以选出优质的香港服务器?
  2. idea中package和directory的区别
  3. Python学习 Day 042 - css 浮动 定位
  4. 学完java后学编译原理_一个资深程序员对Java初学者的学习思维路线建议
  5. vue 方法获取返回值_vue.js - vuex异步提交,怎么获取返回数据
  6. c++语言自定义操作符,C++11新特性之自定义字面量
  7. 如何发布自己的 Composer 包
  8. 【Oracle】Oracle GoldenGate简介及搭建过程
  9. [HAOI2011]Problem b BZOJ2301 数学
  10. Spring循环依赖,竟然有这样不可思议的坑!
  11. python时域转频域_理解波动(三):从频域到时域
  12. [CGAL] CGAL的世界-Kernel内核、Traits特征类
  13. [抄袭]年薪五万程序员的生活及他的理财梦
  14. 更新harbor证书
  15. 格式: echo -e \033[字背景颜色 字体颜色m字符串\033[0m
  16. 问题事件名称: APPCRASH 应用程序名: xxx.EXE 应用程序版本:
  17. 记录自己答题带思路Python简单?先来40道基础面试题测试下
  18. 【JVM】G1(Garbage First)垃圾收集器的原理与应用场景
  19. 【问题记录】关于百度网盘客户端打不开
  20. 使用live555 直播来自v4l2的摄像头数据

热门文章

  1. mysql列别_MySQL基础及CRUD
  2. 绝地求生手游7月服务器维护,绝地求生维护公告7月22 | 手游网游页游攻略大全...
  3. python英语单词 扇贝英语安卓下载_扇贝单词app下载-扇贝单词英语版 安卓版v3.6.503-pc6手机下载...
  4. Sentinel+Nacos实现Sentinel的持久化规则,就很nice!
  5. C#——《C#语言程序设计》实验报告——综合练习——委托、Lambda表达式、LINQ、接口
  6. Codeforces 1196A Three Piles of Candies
  7. linux获取指定字符,shell 获取用户输入指定范围的单个字符的两种方法
  8. GIS创新实践【课堂笔记】
  9. 回溯算法【0-1背包问题】
  10. 非常好用的卸载软件——Geek Uninstaller【官网、介绍、使用演示(卸载“格式工厂”)、软件包下载地址(百度网盘)】