在NumPy中返回一个新的未初始化的三维数组并改变其顺序
要在Python NumPy中返回一个新的未初始化的三维数组,可以使用**numpy.empty()**方法。第一个参数是空数组的形状。顺序可以使用“**order**”参数更改。我们已将顺序设置为“F”,即Fortran风格。
dtype 是数组所需的输出数据类型,例如 numpy.int8。默认为 numpy.float64。“order”参数指定是否以行主序(C 风格)或列主序(Fortran 风格)在内存中存储多维数据。
empty() 函数返回一个具有给定形状、dtype 和 order 的未初始化(任意)数据的数组。对象数组将初始化为 None。
NumPy 提供全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持各种硬件和计算平台,并且与分布式、GPU 和稀疏数组库配合良好。
步骤
首先,导入所需的库:
import numpy as np
使用 Python NumPy 中的 numpy.empty() 方法返回一个新的未初始化的三维数组。顺序使用“order”参数更改。我们已将顺序设置为“F”,即 Fortran 风格:
arr = np.empty([3, 3, 3], order ='F')
显示数组:
print("Array...
",arr)获取数组的类型:
print("
Array type...
", arr.dtype)
获取数组的维度:
print("
Our Array Dimensions...
", arr.ndim)获取数组中元素的数量:
print("
Number of elements...
", arr.size)
示例
import numpy as np
# To return a new Three-Dimensional array, without initializing entries, use the numpy.empty() method in Python Numpy
# The 1st parameter is the Shape of the empty array
# The order is changed using the "order" parameter
# We have set the order to "F" i.e. Fortran-style
arr = np.empty([3, 3, 3], order ='F')
# Display the array
print("Array...
",arr)
# Get the type of the array
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimensions...
", arr.ndim)
# Get the number of elements in the Array
print("
Number of elements...
", arr.size)输出
Array... [[[4.67632299e-310 6.90012448e-310 6.89999064e-310] [6.90012601e-310 6.90012601e-310 6.90012603e-310] [6.90012435e-310 6.90012602e-310 6.90012603e-310]] [[0.00000000e+000 6.89999064e-310 6.90012601e-310] [6.90012602e-310 6.90012601e-310 6.90012600e-310] [6.89999066e-310 6.90012598e-310 6.90012602e-310]] [[6.90012601e-310 6.89999064e-310 6.89999064e-310] [6.90012603e-310 6.89999064e-310 6.89999064e-310] [6.90012601e-310 6.90012603e-310 1.10670705e-321]]] Array type... float64 Our Array Dimensions... 3 Number of elements... 27
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP