創建一個 x 形狀 (5,6) 的陣列,其中包含 -30 到 30 之間的 30 個隨機整數
print the cumulative sum of x along axies 0
print the cumulative sum of x along axies 1
預期輸出為 9 和 -32。
我嘗試使用以下代碼
import numpy as np
np.random.seed(100)
l1= np.random.randint(-30,30, size=(5,6))
x= np.array(l1)
print(x.sum(axis=0))
print(x.sum(axis=1))
你能幫我看看這有什么問題嗎?
uj5u.com熱心網友回復:
你的運算式的結果是:
x.sum(axis=0) == array([ -9, -58, -38, 40, 16, 9])
x.sum(axis=1) == array([-68, 47, 1, 12, -32])
正如您所寫的預期結果是9和-32,也許您想計算最后一列和最后一行的總和?
為了得到這些結果,計算:
x[:, -1].sum() (yields 9)
x[-1, :].sum() (yiels -32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/397947.html
標籤:麻木的 numpy-ndarray
