查找NumPy数组的大小


NumPy 通常与 Python 中的 SciPy 和 matplotlib 等包一起使用。此外,由于 NumPy 中的数组比 Python 中的列表速度更快,因此该模块被广泛用于复杂的数学算法和问题。它具有各种函数和方法,使我们能够轻松简单地执行矩阵运算。

在 NumPy 数组中,有两种类型的数组:

  • 一维数组

  • 多维数组

一维数组(1D 数组)

数组中存储的元素的数据类型可以是字符串、整数、布尔值或浮点数。这些数组称为单维或一维数组。如此定义的数组的形状将是线性的,通常表示为一维数组。

多维数组

这些数组可以存储任何数据类型的元素,例如字符串、整数、数组、布尔值等。它们包含数组嵌套在数组中,也称为嵌套数组,并具有非线性结构。

通常,数组以两种类型衡量:

  • 数组的大小

  • 数组的内存大小

数组大小指的是数组的长度或该特定数组中存在的独立数据元素的总数。

而数组的内存大小指的是数组在系统硬件中占用存储该特定数组中数据的空间(对于较小的数组通常以字节为单位)。

方法 1:使用 itemsize() 函数

itemsize() 函数返回数组中每个元素占用的内存空间。返回值被认为是以字节为单位。

语法

<array_object_name>.itemsize

返回整数,表示字节数。

示例 1

在下面的示例中,我们将使用 itemsize() 函数计算存储在**一维 NumPy 数组**中的每个元素占用的内存。除了大小、长度和占用的总内存空间外,还将显示数组本身。

算法

  • **步骤 1** - 定义数组

  • **步骤 2** - 声明数组

  • **步骤 3** - 打印大小

  • **步骤 4** - 打印 itemsize

import numpy

#creation of a numpy array
arr = numpy.array([1,2,3,4,5])

#Displays length of the array
print(f"Length of the array: {numpy.size(arr)}") 

#Displays memory size in bytes
print(f"Space occupied by one element: {arr.itemsize} bytes")

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes")

输出

Length of the array: 5
Space occupied by one element: 8 bytes
Total memory occupied by the element: 40 bytes

示例 2

在下面的示例中,我们计算了**多维 NumPy 数组**的相同信息。

import numpy

#creation of a numpy array
arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]])

arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']])

#Displays length of the array
print(f'Length of the 2D array: {numpy.size(arr)}')

#Displays memory size in bytes
print(f"Space occupied by one element: {arr.itemsize} bytes")

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes")

print()

print(f"Length of the 3D array: {numpy.size(arr_2)}")
print(f"Space occupied by one element: {arr_2.itemsize} bytes")
print(f"Total memory occupied by the element: {arr_2.itemsize*numpy.size(arr_2)} bytes")

输出

Length of 2D array: 2
Space occupied by one element: 8 bytes
Total memory occupied by the element: 16 bytes

Length of 3D array: 2
Space occupied by one element: 8 bytes
Total memory occupied by the element: 16 bytes

方法 2:使用 nbytes() 函数

nbytes() 是 Python 中 NumPy 模块中的一个方法。此函数返回 NumPy 数组占用的总空间(以字节为单位),这与返回单个元素占用的内存空间的 itemsize 函数不同。

语法

<array_object_name>.nbytes

返回整数,表示数组占用的总字节空间。

示例 1

在本例中,创建了一个**一维 NumPy 数组**来解释占用的内存大小,使用 nbytes 函数。数组的长度也与之一起显示。

import numpy

arr = numpy.array([1,2,3,4,5])

#Displays length of the array
print(f"Length of the array: {numpy.size(arr)}") 

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.nbytes} bytes")

输出

Length of the array: 5
Total memory occupied by the element: 40 bytes

示例 2

在下面的示例中,我们计算了与上面示例中使用的相同数据值的**多维数组**的长度和内存大小。

import numpy

arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]])
arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']])

#Displays length of the array
print(f'Length of the 2D array: {numpy.size(arr)}')

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.nbytes} bytes")

print()

print(f"Length of the 3D array: {numpy.size(arr_2)}")
print(f"Total memory occupied by the element: {arr_2.nbytes} bytes")

输出

Length of 2D array: 2
Total memory occupied by the element: 16 bytes

Length of 3D array: 2
Total memory occupied by the element: 16 bytes

结论

与其他数据结构相比,NumPy 数组消耗的内存更少,因此它们用于处理大型数据集并执行复杂的数学计算。itemsize 函数将计算每个元素的大小,而 nbytes 函数将计算整个数组占用的内存空间。

它们被用于各个领域,例如机器学习、数据分析和计算物理学,将数据转换为各种形式等等。它也用于数据的各种数值和理论计算。

更新于:2023年8月23日

434 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.