I have various array shapes where I am trying to use np.select(). My code for selected_results
works for this case:
# 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]]
However, when I have:
# 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)]
Using selected_results = np.array([np.select(conditions, choice, default=np.nan) for choice in choices])
gives ValueError: list of cases must be same length as list of conditions
.
The expected output is:
>> [['alpha']
[0.119202946]
[-2.521801101837287]
[1.0299826279625939]
[0.63414049]
[0.8116218656784026]]
I understand there is an issue with the shapes, but I don’t know how to have a simple one or two line solution for “selected_results” that is more generally applicable.