📜  创建一个填充所有数组的 Numpy 数组

📅  最后修改于: 2022-05-13 01:55:19.781000             🧑  作者: Mango

创建一个填充所有数组的 Numpy 数组

在本文中,我们将学习如何在给定数组的形状和类型的情况下创建一个全为一的 Numpy 数组。
我们可以使用 Numpy.ones() 方法来完成这项任务。该方法采用三个参数,如下所述 -

shape : integer or sequence of integers
order  : C_contiguous or F_contiguous
         C-contiguous order in memory(last index varies the fastest)
         C order means that operating row-rise on the array will be slightly quicker
         FORTRAN-contiguous order in memory (first index varies the fastest).
         F order means that column-wise operations will be faster. 
dtype : [optional, float(byDefault)] Data type of returned array.  

代码#1:

Python3
# Python Program to create array with all ones
import numpy as geek
 
a = geek.ones(3, dtype = int)
print("Matrix a : \n", a)
 
b = geek.ones([3, 3], dtype = int)
print("\nMatrix b : \n", b)


Python3
# Python Program to create array with all ones
import numpy as geek
 
c = geek.ones([5, 3])
print("\nMatrix c : \n", c)
 
d = geek.ones([5, 2], dtype = float)
print("\nMatrix d : \n", d)


输出:

Matrix a : 
 [1 1 1]

Matrix b : 
 [[1 1 1]
 [1 1 1]
 [1 1 1]]


代码#2:

Python3

# Python Program to create array with all ones
import numpy as geek
 
c = geek.ones([5, 3])
print("\nMatrix c : \n", c)
 
d = geek.ones([5, 2], dtype = float)
print("\nMatrix d : \n", d)

输出:

Matrix c : 
 [[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

Matrix d : 
 [[ 1.  1.]
 [ 1.  1.]
 [ 1.  1.]
 [ 1.  1.]
 [ 1.  1.]]