当前位置: 首页 > 面试题库 >

通过索引将numpy数组中的值设置为NaN

赵珂
2023-03-14
问题内容

我想在numpy数组中设置特定值NaN(以将它们从按行均值计算中排除)。

我试过了

import numpy

x = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]])
cutoff = [5, 7]
for i in range(len(x)):
    x[i][0:cutoff[i]:1] = numpy.nan

看着x,我只会看到-9223372036854775808我的期望NaN

我想到了一个替代方案:

for i in range(len(x)):
    for k in range(cutoff[i]):
        x[i][k] = numpy.nan

没发生什么事。我究竟做错了什么?


问题答案:

将适当元素设置为NaN的矢量化方法

@unutbu的解决方案必须摆脱您得到的值错误。如果您希望vectorize获得性能,可以这样使用boolean indexing-

import numpy as np

# Create mask of positions in x (with float datatype) where NaNs are to be put
mask = np.asarray(cutoff)[:,None] > np.arange(x.shape[1])

# Put NaNs into masked region of x for the desired ouput
x[mask] = np.nan

样品运行-

In [92]: x = np.random.randint(0,9,(4,7)).astype(float)

In [93]: x
Out[93]: 
array([[ 2.,  1.,  5.,  2.,  5.,  2.,  1.],
       [ 2.,  5.,  7.,  1.,  5.,  4.,  8.],
       [ 1.,  1.,  7.,  4.,  8.,  3.,  1.],
       [ 5.,  8.,  7.,  5.,  0.,  2.,  1.]])

In [94]: cutoff = [5,3,0,6]

In [95]: x[np.asarray(cutoff)[:,None] > np.arange(x.shape[1])] = np.nan

In [96]: x
Out[96]: 
array([[ nan,  nan,  nan,  nan,  nan,   2.,   1.],
       [ nan,  nan,  nan,   1.,   5.,   4.,   8.],
       [  1.,   1.,   7.,   4.,   8.,   3.,   1.],
       [ nan,  nan,  nan,  nan,  nan,  nan,   1.]])

向量化方法可直接计算适当元素的按行平均值

如果要获取掩盖的平均值,则可以修改较早提出的矢量化方法,以避免NaNs完全处理,更重要的是保留x整数值。这是修改后的方法-

# Get array version of cutoff
cutoff_arr = np.asarray(cutoff)

# Mask of positions in x which are to be considered for row-wise mean calculations
mask1 = cutoff_arr[:,None] <= np.arange(x.shape[1])

# Mask x, calculate the corresponding sum and thus mean values for each row
masked_mean_vals = (mask1*x).sum(1)/(x.shape[1] -  cutoff_arr)

这是这种解决方案的示例运行-

In [61]: x = np.random.randint(0,9,(4,7))

In [62]: x
Out[62]: 
array([[5, 0, 1, 2, 4, 2, 0],
       [3, 2, 0, 7, 5, 0, 2],
       [7, 2, 2, 3, 3, 2, 3],
       [4, 1, 2, 1, 4, 6, 8]])

In [63]: cutoff = [5,3,0,6]

In [64]: cutoff_arr = np.asarray(cutoff)

In [65]: mask1 = cutoff_arr[:,None] <= np.arange(x.shape[1])

In [66]: mask1
Out[66]: 
array([[False, False, False, False, False,  True,  True],
       [False, False, False,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True],
       [False, False, False, False, False, False,  True]], dtype=bool)

In [67]: masked_mean_vals = (mask1*x).sum(1)/(x.shape[1] -  cutoff_arr)

In [68]: masked_mean_vals
Out[68]: array([ 1.        ,  3.5       ,  3.14285714,  8.        ])


 类似资料:
  • 问题内容: 说我有一个数组 np.zeros((4,2)) 我有一个值[4,3,2,1]的列表,我想分配给以下位置:[(0,0),(1,1),(2,1),(3,0) ] 如何不使用for循环或展平数组来做到这一点? 我可以使用花哨的索引来检索值,但不能分配它们。 ======更新========= 感谢@hpaulj,我意识到原始代码中的错误是。 当我使用zeros_like初始化数组时,它默认为

  • 我对Java很陌生,只是有点纠结于数组。我有一个代码块,我已经写了当遵循教程,但正在努力理解它,希望有人能解释给我。 null 对不起,如果这是一个琐碎的问题给你,但这是最好的地方,我可以想到转向。

  • 问题内容: 有没有一种有效的Numpy机制,可以根据条件为true而不是布尔掩码数组来检索数组中位置的整数索引? 例如: 在这种情况下,我想知道指标的地方。是否可以生成这些而不循环? 问题答案: 另外的选择: 这与。

  • 问题内容: 我想通过值而不是索引在JComboBox中设置选定的索引。怎么做?例 好的,我已经稍微修改了我的问题。我忘了我在JComboBox中有一个自定义项目,这使其变得更加困难。我不能做setSelectedItem,因为我在每个项目中都有一个ComboItem。因此,我该如何完成呢? 问题答案: 。您只需阅读javadoc即可找到它。 编辑:由于您更改了问题,我将更改答案。 如果要选择带有“

  • 问题内容: 在Python中,我们可以使用来获取数组中值的索引。 但是,当我尝试执行NumPy数组时: 我得到: AttributeError:“ numpy.ndarray”对象没有属性“ index” 我如何在NumPy数组上执行此操作? 问题答案: 使用来获得,其中一个给定的条件是指数。 例子: 对于称为的2D : 对于一维数组: 请注意,这也适用于像条件,,等等… 您也可以使用方法创建的子

  • 问题内容: 假设我有一个一维numpy数组 我想将此编码为2D一热阵列 有快速的方法吗?比循环遍历设置元素更快。 问题答案: 您的数组定义了输出数组中非零元素的列。您还需要定义行,然后使用花式索引: