Python_记录02:Python小error

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值