我有兩個具有相同維度的 numpy 陣列:權重和百分比。百分比是“真實”資料,權重是直方圖中每個“真實”資料的數量。
例如)
weights = [[0, 1, 1, 4, 2]
[0, 1, 0, 3, 5]]
percents = [[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]]
(每一行百分數都是一樣的)
我想以產生權重[x] * [百分比[x]]的方式將這些“相乘”在一起:
results = [[0 * [1] 1 * [2] 1 * [3] 4 * [4] 2 * [5]
[0 * [1] 1 * [2] 0 * [3] 3 * [4] 5 * [5]]
= [[2, 3, 4, 4, 4, 4, 5, 5]
[2, 4, 4, 4, 5, 5, 5, 5, 5]]
請注意,每行的長度可能不同。理想情況下,這可以在 numpy 中完成,但因此它可能最終成為串列串列。
編輯:我已經能夠將這些嵌套的 for 回圈拼湊在一起,但顯然這并不理想:
list_of_hists = []
for index in df.index:
hist = []
# Create a list of lists, later to be flattened to 'results'
for i, percent in enumerate(percents):
hist.append(
# For each percent, create a list of [percent] * weight
[percent]
* int(
df.iloc[index].values[i]
)
)
# flatten the list of lists in hist
results = [val for list_ in hist for val in list_]
list_of_hists.append(results)
uj5u.com熱心網友回復:
有一種np.repeat專為此類操作而設計,但在 2D 情況下不起作用。因此,您需要使用扁平化的陣列視圖。
weights = np.array([[0, 1, 1, 4, 2], [0, 1, 0, 3, 5]])
percents = np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
>>> np.repeat(percents.ravel(), weights.ravel())
array([2, 3, 4, 4, 4, 4, 5, 5, 2, 4, 4, 4, 5, 5, 5, 5, 5])
之后,您需要選擇將其拆分的索引位置:
>>> np.split(np.repeat(percents.ravel(), weights.ravel()), np.sum(weights, axis=1)[:-1])
[array([2, 3, 4, 4, 4, 4, 5, 5]), array([2, 4, 4, 4, 5, 5, 5, 5, 5])]
請注意,這np.split是非常低效的操作,而且您希望用不等長的行制作陣列。
uj5u.com熱心網友回復:
您可以使用串列理解和reduce來自functools:
import functools
res=[functools.reduce(lambda x,y: x y,
[x*[y] for x, y in zip(w, p)])
for w, p in zip(weights, percents)]
輸出:
[[2, 3, 4, 4, 4, 4, 5, 5],
[2, 4, 4, 4, 5, 5, 5, 5, 5]]
或者,僅串列理解解決方案:
res= [[j for i in [x*[y]
for x, y in zip(w, p)]
for j in i]
for w, p in zip(weights, percents)]
輸出:
[[2, 3, 4, 4, 4, 4, 5, 5],
[2, 4, 4, 4, 5, 5, 5, 5, 5]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372352.html
上一篇:陣列中的3個嵌套回圈-VBA
下一篇:Python:用變數名填充串列?
