返回一个具有给定形状和类型的新数组,该数组填充了 NumPy 中的类数组


要返回一个具有给定形状和类型的新数组,并用填充值填充,请在 Python NumPy 中使用 **numpy.full()** 方法。第一个参数是新数组的形状。第二个参数将填充值设置为类数组。

dtype 是数组所需的数 据类型。order 建议是将多维数据以 C 连续或 Fortran 连续(行或列方式)顺序存储在内存中。

NumPy 提供了全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持各种硬件和计算平台,并且可以很好地与分布式、GPU 和稀疏数组库配合使用。

步骤

首先,导入所需的库:

import numpy as np

要返回一个具有给定形状和类型的新数组,并用填充值填充,请使用 numpy.full() 方法。第二个参数将填充值设置为类数组:

arr = np.full((2, 2), fill_value = [998, 999])

显示数组:

print("Array...
",arr)

获取数组的类型:

print("
Array datatype...
",arr.dtype)

获取数组的维数:

print("
Array Dimensions...
",arr.ndim)

获取数组的形状:

print("
Our Array Shape...
",arr.shape)

获取数组的元素个数:

print("
Elements in the Array...
",arr.size)

示例

import numpy as np

# To return a new array of given shape and type, filled with a fill value, use the numpy.full() method in Python Numpy
# The 1st parameter is the shape of the new array
# The 2nd parameter sets the fill value as array-like
arr = np.full((2, 2), fill_value = [998, 999])

# Displaying our array
print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size)

输出

Array...
[[998 999]
[998 999]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(2, 2)

Elements in the Array...
4

更新于:2022年2月10日

浏览量 144

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.