我正在嘗試通過添加帶有“1”的列來運行用戶定義的函式來處理矩陣
def updated_x (input_x):
if input_x.shape[0] < input_x.shape[1]:
input_x = np.transpose(input_x)
ones = np.ones((len(input_x), 1), dtype=int)
updated_x = np.concatenate((ones, input_x), axis=1)
return updated_x
我有以下輸入值:
input2 = np.array([5,4,6])
input1 = np.array([[5,4,8,9],[5,5,5,6]])
input3 = np.array([[2,4],[1,2],[2,3],[4,12]])
for i in [input1, input2, input3]:
print(updated_x(i), )
我收到三個陣列中的 2 個錯誤
[[ 1 2 4]
[ 1 3 5]
[ 1 6 7]
[ 1 9 10]]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-117-8fc2b754194b> in <module>
1 for i in [input1, input2, input3]:
----> 2 print(updated_x(i), )
<ipython-input-115-41916a0377b0> in updated_x(input_x)
4
5 def updated_x(input_x):
----> 6 if input_x.shape[0] < input_x.shape[1]:
7 input_x = np.transpose(input_x)
8
IndexError: tuple index out of range
uj5u.com熱心網友回復:
input2有形狀(3,)。嘗試在函式呼叫之前對其進行重塑:
input2 = input2.reshape(1,-1)
或者在函式體的最開始添加一些檢查代碼:
def updated_x (input_x):
if len(input_x.shape)<2:
input_x = input_x.reshape(1,-1)
#..........
#..........
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430706.html
下一篇:使用numpy廣播減去二維陣列
