[英]TypeError / Array indexing; 'int' object does not support item assignment
I have a function which works on arrays with more than one item, but fails if the array contains only one item. 我有一个函数可以在具有多个项目的数组上工作,但如果数组只包含一个项目,则会失败。 Let's consider this example 让我们考虑这个例子
import numpy as np
def checker(a):
a[a>5] = np.nan
a = np.arange(10)
a = checker(a)
Works, but 工作,但是
a = 1
a = checker(a) # fails
and gives 并给出
Traceback (most recent call last):
a[a>5] = np.nan
TypeError: 'int' object does not support item assignment
I'd like to handle it like MATLAB, and NOT like this version of checker(), which has 4x more lines than the version above. 我想像MATLAB一样处理它,而不是像这个版本的checker(),它的行数比上面的版本多4倍。
def checker(a):
try:
a[a>5] = np.nan
except TypeError:
if a>5: a = np.nan
To create an empty array filled with nan
s, you can use np.fill
: 要创建一个填充了nan
的空数组,可以使用np.fill
:
a=np.empty(np.shape(1))
a.fill(np.nan)
b=False
a[b]=10
You were getting an error because a
wasn't an array, it was a float. 你得到一个错误,因为a
不是一个数组,它是一个浮点数。
In MATLAB everything has atleast 2 dimensons; 在MATLAB中,一切都至少有2个维度; in numpy
indexing can reduce the number of dimensions 在numpy
索引中可以减少维数
np.shape(1)
is ()
? 是()
? This is the same as np.array(1).shape
, ie the shape (size in MATLAB terms) of a single element array. 这与np.array(1).shape
,即单个元素数组的形状(MATLAB术语中的大小)。 It is 0d, as opposed to 2d in MATLAB. 它是0d,而不是MATLAB中的2d。
a = np.empty(np.shape(1))*np.nan
# a = np.array(np.nan) does the same thing
is nan
, a single element array with value nan
. 是nan
,一个值为nan
的单个元素阵列。
a[False]
displays as array([], dtype=float)
, with shape (0,)
; 显示为array([], dtype=float)
,形状为(0,)
; it's now 1d, but without any elements. 它现在是1d,但没有任何元素。
With a 0d array, the only meaningful indexing is a[()]
which returns the element, nan
, a np.float64
. 对于0d数组,唯一有意义的索引是a[()]
,它返回元素nan
, np.float64
。 a.item()
does the same. a.item()
做同样的a.item()
。
And for assignment purposes I can't find a way to change the value of that item 出于分配目的,我找不到改变该项目价值的方法
a[???] = 0
correction, ellipsis can be used, since it stands in for any number of :
including none. 更正,省略号可以使用,因为它代表任意数量:
包括无。
a[...] = 0
# array(0,0)
(you don't wand a=0
since that just reassigns the variable). (你不会使用a=0
因为那只是重新分配变量)。
In general 0d arrays like this are possible, but they are rarely useful. 通常这样的0d数组是可能的,但它们很少有用。
I'm not entirely sure what you are trying to do (I don't have a working Octave session at the moment). 我不完全确定你要做什么(我目前没有工作的Octave会议)。 But this difference in how dimensions change with indexing is the key to your problems. 但是,尺寸如何随着索引而变化的差异是您的问题的关键。
a = np.array([np.nan])
a[np.array([False])] = 0 # no change
a[np.array([True])] = 0 # change
Note that I made the boolean index an array, but just a scalar or list. 请注意,我将布尔索引设为数组,但只是标量或列表。 This is closer to your MATLAB boolean index. 这更接近你的MATLAB布尔索引。
When you multiply a scalar array by a scalar, numpy coerces the result into a scalar: 当您将标量数组乘以标量时,numpy会将结果强制转换为标量:
>>> a = np.empty(())
>>> a
array(10.0)
>>> a * 2
20.0
If you need to keep the scalar as an array, you can use np.asarray(a * 2)
如果你需要将标量保持为数组,可以使用np.asarray(a * 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.