我一直在嘗試減去兩個矩陣(不確定它們是否真的是矩陣,因為其中一個是熊貓系列)但結果不正確。我添加了代碼和輸出,我怎樣才能得到正確的 200X1 形狀結果?
*X 是 200x4,w 是 4x1

uj5u.com熱心網友回復:
你可以重塑y_hat:
y - y_hat.reshape(-1,1)
你得到的原因(200,200)是因為 numpy 廣播,它被視為y_hat(1,200),所以為了匹配形狀,numpy 廣播y進入(200,200)和y_hat進入(200,200),然后進行減法。
uj5u.com熱心網友回復:
也許它可以幫助:
rng = np.random.default_rng(2022)
df = pd.DataFrame(rng.integers(0, 10, (5, 4)))
sr = pd.Series(rng.integers(0, 10, (5, )))
>>> df
0 1 2 3
0 7 2 7 0
1 1 6 9 0
2 0 6 8 7
3 8 1 5 0
4 0 4 8 9
>>> sr
0 3
1 9
2 0
3 2
4 6
dtype: int64
>>> df - sr # does not work
0 1 2 3 4
0 4 -7 7 -2 NaN
1 -2 -3 9 -2 NaN
2 -3 -3 8 5 NaN
3 5 -8 5 -2 NaN
4 -3 -5 8 7 NaN
>>> df.sub(sr, axis=0) # work
0 1 2 3
0 4 -1 4 -3
1 -8 -3 0 -9
2 0 6 8 7
3 6 -1 3 -2
4 -6 -2 2 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/438117.html
上一篇:如何調整陣列FPDF中的多單元格
下一篇:OCR:將單詞保存在CSV檔案中
