0
Я хотел бы создать несколько динамический запрос на основе массива numpy. В идеале, индексы одного массива (uu
) должны быть возвращены с учетом условий для каждого столбца во втором массиве (cond
).гибкий запрос на основе двух массивов numpy
Образец ниже демонстрирует то, что я имею в виду, и он работает с использованием цикла. Мне интересно, есть ли более эффективный метод. Спасибо за вашу помощь.
import numpy as np
# create an array that has four columns, each contain a vector (here: identical vectors)
n = 101
u = np.linspace(0,100,n)
uu = np.ones((4, n)) * u
# ideally I would like the indices of uu
# (so I could access another array of the same shape as uu)
# that meets the following conditions:
# the condition based on which values in uu should be selected
# in the first column (index 0) all values <= 10. should be selected
# in the second column (index 1) all values <= 20. should be selected
cond = np.array([10,20])
# this gives the correct indices, but in a series of 1D solutions
# this would work as a work-around
for i in range(cond.size):
ix = np.where(uu[i,:] <= cond[i])
print(ix)
# this seems like a work-around using True/False
# but here I am not sure how to best convert this to indices
for i in range(cond.size):
uu[i,:] = uu[i,:] <= cond[i]
print(uu)
'уу [я,:]' выбирает строки и столбцы не, правильно? Не уверен, правильно ли я получил вопрос. – Divakar
Не должно всегда иметь столько столбцов, сколько uu? – rmhleo