栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

@unutbu的解决方案必须摆脱您得到的值错误。如果您希望

vectorize
获得性能,可以这样使用
booleanindexing
-

import numpy as np# Create mask of positions in x (with float datatype) where NaNs are to be putmask = np.asarray(cutoff)[:,None] > np.arange(x.shape[1])# Put NaNs into masked region of x for the desired ouputx[mask] = np.nan

样品运行-

In [92]: x = np.random.randint(0,9,(4,7)).astype(float)In [93]: xOut[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.nanIn [96]: xOut[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 cutoffcutoff_arr = np.asarray(cutoff)# Mask of positions in x which are to be considered for row-wise mean calculationsmask1 = cutoff_arr[:,None] <= np.arange(x.shape[1])# Mask x, calculate the corresponding sum and thus mean values for each rowmasked_mean_vals = (mask1*x).sum(1)/(x.shape[1] -  cutoff_arr)

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

In [61]: x = np.random.randint(0,9,(4,7))In [62]: xOut[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]: mask1Out[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_valsOut[68]: array([ 1.        ,  3.5       ,  3.14285714,  8.        ])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号