I have a numpy array with a lot of NaN values and in some rows non-NA values.
我有一个numpy数组,有很多NaN值,在某些行中有非NA值。
Now, I want to get all indices of the rows that have a non-NA value (i.e. a numeric values) and want to extract the values for these specific row indices from a different numpy array. So we have:
现在,我想得到具有非NA值(即数值)的行的所有索引,并希望从不同的numpy数组中提取这些特定行索引的值。所以我们有:
Y_test, a np array of length 698 with on some rows numeric values
Y_test,一个长度为698的np数组,带有一些行数值
Y_test
array([ nan, 13., nan, 10., nan])
Y_pred, a np array of length 698 with numeric values on each row
Y_pred,一个长度为698的np数组,每行都有数值
Y_pred
array([ 0.96, 0.57, 0.33, 0.43, 0.83])
I want to only extract the numeric values of np_array2 on the indices of the rows with numeric values in np_array1.
我想只在np_array1中使用数值的行的索引提取np_array2的数值。
For example:
array([ 0.57, 0.43])
I am doing this to calculate the error between the non-NA rows from Y_test and the corresponding rows of Y_pred.
我这样做是为了计算Y_test的非NA行和Y_pred的相应行之间的误差。
Can anyone help me?
谁能帮我?
1 个解决方案
#1
1
You can use isnan
to find the indices where an array is nan
and then just use the inverse.
您可以使用isnan查找数组为nan的索引,然后使用反向。
np.isnan(Y_test)
array([ True, False, True, False, True], dtype=bool)
Y_pred[~np.isnan(Y_test)]
array([ 0.57, 0.43])
EDIT
Per further input I understand that your two arrays have different sizes. In that case you can use flatnonzero
as @PaulPanzer pointed out.
根据进一步的输入,我知道你的两个数组有不同的大小。在这种情况下,您可以使用@nonulzero指出@PaulPanzer。
np.flatnonzero(~np.isnan(Y_test))
array([1, 3])
Y_pred[np.flatnonzero(~np.isnan(Y_test))]
#1
1
You can use isnan
to find the indices where an array is nan
and then just use the inverse.
您可以使用isnan查找数组为nan的索引,然后使用反向。
np.isnan(Y_test)
array([ True, False, True, False, True], dtype=bool)
Y_pred[~np.isnan(Y_test)]
array([ 0.57, 0.43])
EDIT
Per further input I understand that your two arrays have different sizes. In that case you can use flatnonzero
as @PaulPanzer pointed out.
根据进一步的输入,我知道你的两个数组有不同的大小。在这种情况下,您可以使用@nonulzero指出@PaulPanzer。
np.flatnonzero(~np.isnan(Y_test))
array([1, 3])
Y_pred[np.flatnonzero(~np.isnan(Y_test))]