学习PYTHON的随手记

目录

1.Python报错'numpy.ndarray' object has no attribute 'append'

2.Python报错ModuleNotFoundError: No module named 'utils.angle'

3.Python报错 TypeError: only size-1 arrays can be converted to Python scalars

4.ValueError: could not broadcast input array from shape (100,1) into shape (100,)

5.TypeError: Cannot interpret '10' as a data type

6. IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

7.RuntimeWarning: overflow encountered in sin

RuntimeWarning: invalid value encountered in subtract


1.Python报错'numpy.ndarray' object has no attribute 'append'

问题:报错'numpy.ndarray' object has no attribute 'append'

解决:numpy有append方法,但是ndarray类型数据没有append属性。试试将代码中的ptn.append(Di)改为ptn=np.append(ptn,Di)


2.Python报错ModuleNotFoundError: No module named 'utils.angle'

问题:ModuleNotFoundError: No module named 'utils.angle'

解决:将下载的代码文件中的utils文件夹中的文件复制,如下图

全部复制,按照报错路径→复制到anaconda中的utils文件夹路径中


3.Python报错 TypeError: only size-1 arrays can be converted to Python scalars

报错的信息如图所示:

翻译成中文就是:

问题:类型错误:只有size-1的数组可以转换为 Python 标量

解决:感觉上是因为变量的维度有问题,所以导致了这个报错,于是把y_mea和x_old都打印出来看看,发现是自己写这一句:

w = math.exp(-((y_mea - x_old ** 2)**2 / (2 * var)))d

的时候,忘记给y_mea加下标了

修改后:

 w = math.exp(-((y_mea[j] - x_old ** 2)**2 / (2 * var)))

4.ValueError: could not broadcast input array from shape (100,1) into shape (100,)

问题:ValueError:无法将输入数组从形状(100,1)广播到形状(100,)

报错代码:

Xpf[0:numSamples,0] = x0 + np.sqrt(Q) * np.random.randn(numSamples,1)

原因就是 np.random.randn(numSamples,1) 生成的是一个(100,1)的二维数组

Xpf[0:numSamples,0] 是一个(100, )的一维数组,所以形状不匹配

解决:把二维数组展开成一维数组,直接用了flatten函数。

代码修改为:

from sympy import flatten
Xpf[0:numSamples,0] = flatten(x0 + np.sqrt(Q) * np.random.randn(numSamples,1))

5.TypeError: Cannot interpret '10' as a data type

原因:调用zero函数时少写了一个括号,应该是

numpy.zeros([ a,b])


6. IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

报错代码行:

auxw = weight[0, i]

问题:在索引处使用了浮点数,应该要转换成int类型或者布尔数组类型

检查发现出错的代码行是:

i = fix((N - j + 1) * u[0, j]) + j

i的取值出问题了 


7.RuntimeWarning: invalid value encountered in subtract

x[0] = x[0] + (x[3] / x[4]) * (np.sin(x[4] * DT + x[2]) - np.sin(x[2]))

问题:在运行程序时出现 RuntimeWarning: invalid value encountered in true_divide的警告,可能是在使用numpy时出现了0除以0的情况造成的。


8.RuntimeWarning: overflow encountered in sin

x[0] = x[0] + (x[3] / x[4]) * (np.sin(x[4] * DT + x[2]) - np.sin(x[2]))

问题:sin函数溢出了 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值