我有各种数组形状,我尝试使用 np.select()。我的代码
selected_results
适用于这种情况:
# Given arrays
results = [[array(['alpha'], dtype=object) array([0.16134485])
array([-2.19302435, 0.57976273]) array([1.25348942, 0.77778261])
array([0.56136549, 0.56136549]) array([1.12076068, 0.53834429])]
[array(['alpha'], dtype=object) array([0.11778484])
array([-0.86983912, 0.24081955]) array([0.84209155, 0.84209155])
array([0.66394431, 0.66394431]) array([0.51797309, 0.51797309])]
[array(['alpha'], dtype=object) array([0.11920295])
array([-2.5218011 , 0.25602172]) array([1.02998263, 0.88191816])
array([0.63414049, 0.63414049]) array([0.81162187, 0.61289923])]]
conditions = [array([False, False]), array([False, False]), array([ True, True])]
choices = [array([array(['alpha'], dtype=object), array(['alpha'], dtype=object),
array(['alpha'], dtype=object)], dtype=object), array([array([0.16134485]), array([0.11778484]), array([0.11920295])],
dtype=object), array([array([-2.19302435, 0.57976273]),
array([-0.86983912, 0.24081955]),
array([-2.5218011 , 0.25602172])], dtype=object), array([array([1.25348942, 0.77778261]), array([0.84209155, 0.84209155]),
array([1.02998263, 0.88191816])], dtype=object), array([array([0.56136549, 0.56136549]), array([0.66394431, 0.66394431]),
array([0.63414049, 0.63414049])], dtype=object), array([array([1.12076068, 0.53834429]), array([0.51797309, 0.51797309]),
array([0.81162187, 0.61289923])], dtype=object)]
selected_results = np.array([np.select(conditions, choice, default=np.nan) for choice in choices])
print(selected_results)
>> [['alpha' 'alpha']
[0.119202946 0.119202946]
[-2.521801101837287 0.25602172377257837]
[1.0299826279625939 0.881918155681708]
[0.63414049 0.63414049]
[0.8116218656784026 0.612899235]]
但是,当我有:
# Given arrays
results = [['alpha']
[0.161344848]
[-2.19302435146961]
[1.2534894161924872]
[0.5613654925053716]
[1.120760679329349]
['alpha']
[0.117784836]
[-0.8698391195603491]
[0.8420915464087234]
[0.663944309731699]
[0.517973094]
['alpha']
[0.119202946]
[-2.521801101837287]
[1.0299826279625939]
[0.63414049]
[0.8116218656784026]]
conditions = [array([False]), array([False]), array([ True])]
choices = [array(['alpha', 0.161344848, -2.19302435146961, 1.2534894161924872,
0.5613654925053716, 1.120760679329349, 'alpha', 0.117784836,
-0.8698391195603491, 0.8420915464087234, 0.663944309731699,
0.517973094, 'alpha', 0.119202946, -2.521801101837287,
1.0299826279625939, 0.63414049, 0.8116218656784026], dtype=object)]
使用
selected_results = np.array([np.select(conditions, choice, default=np.nan) for choice in choices])
给出
ValueError: list of cases must be same length as list of conditions
预期的输出是:
>> [['alpha']
[0.119202946]
[-2.521801101837287]
[1.0299826279625939]
[0.63414049]
[0.8116218656784026]]
我知道有一个问题形状,但我不知道如何为“selected_results”提供一个更普遍适用的简单的一两行解决方案。
问题在于,在你的第二种情况下,
conditions
中有 3 个条件,但你的
choices
数组中每个元素只有 1 个选择。为了解决这个问题,需要为
choices
中的每个条件提供一个选择。
你可以通过将你的单个
choice
数组分成多个大小相等的块来实现。由于你有 3 个条件,因此你应该将
choice
数组分成 3 个相等的块,每个块代表一个条件的选择。
以下是如何做到这一点:
import numpy as np
results = ['alpha', 0.161344848, -2.19302435146961, 1.2534894161924872, 0.5613654925053716, 1.120760679329349,
'alpha', 0.117784836, -0.8698391195603491, 0.8420915464087234, 0.663944309731699, 0.517973094,
'alpha', 0.119202946, -2.521801101837287, 1.0299826279625939, 0.63414049, 0.8116218656784026]
conditions = np.array([False, False, True])
# 将 choices 数组分成 3 个相等的块
choices = np.array_split(results, len(conditions))
# 使用 np.select 选择每个块中相应条件的值
selected_results = np.array([np.select([conditions[i]], [choice], default=np.nan) for i, choice in enumerate(choices)])
# 将结果展平为一维数组
selected_results = selected_results.flatten()
print(selected_results)
这将输出预期的结果:
['alpha' 0.119202946 -2.521801101837287 1.0299826279625939
0.63414049 0.8116218656784026]
这个解决方案通过将
choices
数组拆分为每个条件对应一个块来解决数组形状问题。然后,它使用
np.select
为每个条件选择相应的块,并生成预期结果的一维数组。