提示错误“'>=' not supported between instances of 'range' and 'int'”
2018-03-29 11:20
946 查看
在学习《Designing Machine Learning Systems with Python》(中文名《机器学习系统设计——python语言实现》)一书中,第三章第二节第三小节部分的泊松分布的python代码在python3.6上运行时报错
错误信息很明显,’>=’符号不支持两个类型不同的字符之间的比较,从代码中我们可以很容易知道
关键在于poisson()函数的输入,即xr这个变量,它的类型是range类型,而range类型不能与一个int类型直接判断。我们只需要对它的类型进行下修改就可以了。
我们知道我们的目的是让xr这个变量中的每一个值都与’>=’符号后的int类型数值进行下判断,并将所有结果一起返回。那么我们就可以先试下list类型
输入:
输出:
看来不行,那么我们就想到了numpy库中也有一个类似的arange()函数,我们测试下。
输入:
输出:
我们得到了一个array类型的数组。这就是我们想要的答案。返回书中的例子,我们的代码就应该修改为
我们就得到了我们想要的输出。
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 'str' and 'int'
- Python---TypeError: '<' not supported between instances of 'str' and 'int'
- Python3.6:“猜数字游戏”TypeError: '<' not supported between instances of 'str' and 'int'
- TypeError: '<' not supported between instances of 'str' and 'int'
- TypeError: '<' not supported between instances of 'treeNode' and 'treeNode'
- 编译vlc-android 提示错误:libavcodec versions 56 and later are not supported yet解决办法
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- hibernate错误提示: No Hibernate Session bound to thread, and configuration does not allow creation of no
- 打开eclipse的opens the android sdk and avd manager提示错误:error:location of the android sdk has not been .
- 打开eclipse的opens the android sdk and avd manager提示错误:error:location of the android sdk has not been
- 关于xml配置文件无元素提示和the root element of the document is not <xsd:schema>.错误
- switch 错误:a label can only be part of a statement and a declaration is not a statement
- SDK更新后,重新打开Eclipse时提示错误Location of the Android SDK has not been setup in the preferences
- Project facet Java version 1.8 is not supported.错误提示解决方法
- switch引发的错误:a label can only be part of a statement and a declaration is not a statement
- 创建Maven项目时提示web.xml is missing and <failOnMissingWebXml> is set to true错误解决方案
- UserWarning: The installed version of numexpr 2.4 is not supported in pandas and will be not be used
- mysql错误提示Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)的一个解决办法
- Scala中类型不对错误(error: value % is not a member of scala.collection.immutable.Range.Inclusive)
- Mabatis错误提示Parameter index out of range的处理方法