我有一个2D numpy数组,想要用1D数组中的相应值替换每一行中的每个NaN
。例如,这个矩阵:
[[1. 2. NaN]
[4. 5. 6.]
[NaN NaN 9.]]
使用向量[3. 7. 8.]
将转换为:
[[1. 2. 8.]
[4. 5. 6.]
[3. 7. 9.]]
如何在不迭代索引的情况下执行此操作?
答案 0 :(得分:1)
使用numpy.where
并广播:
.box
numpy.isnan()
为您提供一系列布尔值,其中NaN的值为'[!] There was an error parsing 'Gemfile': You cannot specify the same gem twice with different version requirements. You specified: sdoc (~> 0.4.0) and sdoc (>= 0). BUndlr cannot continue.
from c:/Users/MacKenzie/Documents/Programming/Projects/odot/Gemfile:16
------------------------------------
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
> gem 'tzinfo-data' platforms: [:mingw, :mswin, :x64_mingw, :jruby}
source 'https://rubygems.org'
-------------------------------------'
,>>> a = np.array([[1., 2., np.nan],
[4., 5., 6.],
[np.nan, np.nan, 9.]])
>>> v = np.array([3, 7, 8])
>>> np.where(np.isnan(a), v, a)
array([[ 1., 2., 8.],
[ 4., 5., 6.],
[ 3., 7., 9.]])
为