在 Numpy 中返回一个具有给定形状、填充值和不同输出类型的新数组
要返回一个具有给定形状和类型的新数组,并用填充值填充,请在 Python Numpy 中使用 **numpy.full()** 方法。第一个参数是新数组的形状。第二个参数设置填充值。第三个参数用于设置返回的输出数组的所需数据类型。
dtype 是数组所需的类型。order 表示是否以 C 或 Fortran 连续(行或列方式)顺序在内存中存储多维数据。
NumPy 提供了全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等。它支持各种硬件和计算平台,并且可以很好地与分布式、GPU 和稀疏数组库配合使用。
步骤
首先,导入所需的库:
import numpy as np
要返回一个具有给定形状和类型的新数组,并用填充值填充,请使用 numpy.full() 方法。第三个参数用于设置返回的输出数组的所需数据类型:
arr = np.full((4,5), fill_value = 999, dtype = float)
显示我们的数组:
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
# The 3rd parameter is used to set the desired data-type of the returned output array
arr = np.full((4,5), fill_value = 999, dtype = float)
# 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... [[999. 999. 999. 999. 999.] [999. 999. 999. 999. 999.] [999. 999. 999. 999. 999.] [999. 999. 999. 999. 999.]] Array datatype... float64 Array Dimensions... 2 Our Array Shape... (4, 5) Elements in the Array... 20
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP