我是个学习者,有点受不了不知道我如何创建下面的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
这个怎么样:
>>> 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]])
如果您想使用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表示
试试这个:-您需要使用np.ones方法
import numpy as np
m = np.ones((3, 3), dtype=float)
print(m)
尝试
import numpy as np
np.ones((3,3))