我是學習者,有點堅持不知道如何創建低于 3 X 3 的矩陣將所有 1 個矩陣都包含在其中
1 , 1 , 1
1 , 1 , 1
1 , 1 , 1
我的代碼:
import numpy as np
arr=np.ones(np.full(1)).reshape(3,3)
arr
uj5u.com熱心網友回復:
如果你想使用reshape():
import numpy as np
arr=np.ones(9).reshape(3,3)
或直接:
import numpy as np
arr=np.ones(shape=(3,3))
或來自另一個矩陣:
import numpy as np
arr1=np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2=np.ones_like(arr1)
閱讀檔案(鏈接)
shape:新陣列的形狀,例如 (2, 3) 或 2。
如果你想要1而不是1.設定爭論dtype = int
dtype:陣列所需的資料型別默認值 None 表示
uj5u.com熱心網友回復:
那這個呢:
>>> np.ones((3,3))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
>>> np.ones((3,3)).dtype
dtype('float64')
如果你想 int
>>> np.ones((3,3), dtype='int')
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
uj5u.com熱心網友回復:
對作者的解決方法略改:user1740577
用戶只需要整數值而不是十進制值
通過添加dtype=intOP的預期輸出可以實作
代碼 :
import numpy as np
np.ones((3,3),dtype=int)
預期輸出:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
uj5u.com熱心網友回復:
嘗試
import numpy as np
np.ones((3,3))
uj5u.com熱心網友回復:
試試這個:- 你需要使用 np.ones 方法
import numpy as np
m = np.ones((3, 3), dtype=float)
print(m)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351764.html
下一篇:按元素添加numpy陣列串列
