Python_记录02:Python小error
1、NameError: name ‘reduce’ is not define
# reduce函数在python3的内建函数移除了,放入了functools模块
添加:
from functools import reduce # python3
2、TypeError: ‘float’ object cannot be interpreted as an integer
#错误代码段
for i in range(n / 2 + 1, n + 1):
s += c(n, i) * p ** i * (1 - p) ** (n - i)
#原因:
#python3的/结果含有浮点数!
#python2中的/取整等价于python3的//
#在python3中,//表示取整除 - 返回商的整数部分(向下取整)
#改为:
for i in range(n // 2 + 1, n + 1):
s += c(n, i) * p ** i * (1 - p) ** (n - i)
3、ValueError: RGBA sequence should have length 3 or 4
利用plot.scatter绘制 散点图(Scatter) 时,出现以下错误:
Traceback (most recent call last):
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\axes_axes.py”, line 4284, in _parse_scatter_color_args
colors = mcolors.to_rgba_array©
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\colors.py”, line 294, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\colors.py”, line 177, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\colors.py”, line 244, in _to_rgba_no_colorcycle
raise ValueError(“RGBA sequence should have length 3 or 4”)
ValueError: RGBA sequence should have length 3 or 4
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “E:/document/code/python/zouBoML/14_SVM/14.1.SVM.intro.py”, line 66, in
plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors=‘k’, s=50, cmap=cm_dark) # 样本
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\pyplot.py”, line 2843, in scatter
_ret = gca().scatter(
File "D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib_init.py", line 1599, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\axes_axes.py”, line 4451, in scatter
self._parse_scatter_color_args(
File “D:\soft\python-3.8.1-amd64\lib\site-packages\matplotlib\axes_axes.py”, line 4293, in _parse_scatter_color_args
raise ValueError(
ValueError: ‘c’ argument has 150 elements, which is not acceptable for use with ‘x’ with size 150, ‘y’ with size 150.
修改前代码:
plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=50, cmap=cm_dark) # 样本
修改后代码:
plt.scatter(x[:, 0], x[:, 1], c=np.squeeze(y), edgecolors='k', s=50, cmap=cm_dark) # 样本
4、KeyError: b’Iris-setosa’
修改前:
it = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}
修改后:
it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2}
参考链接:https://blog.csdn.net/qq_36512295/article/details/98480240
5、安装PIL库
高版本的PIL库包含在pillow中,使用命令:
pip install pillow #即可
参考链接:
https://www.cnblogs.com/mrgavin/p/8177841.html
https://www.cnblogs.com/lyrichu/p/9124504.html