NumPy 查找数组的大小

NumPy 查找数组的大小

Numpy在Python中经常与SciPy和matplotlib等包一起使用。此外,与Python中的列表相比,Numpy中的数组速度更快,因此这个模块被广泛用于复杂的数学算法和问题。它具有各种函数和方法,可以使我们的矩阵操作变得简单易行。

Numpy数组分为两种类型:

  • 单维数组

  • 多维数组

单维数组

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

多维数组

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

通常,数组有两种类型的尺寸:

  • 数组的大小

  • 数组的内存大小

数组的大小是指数组的长度或其中独立数据元素的总数。

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

方法1:使用Itemsize()函数

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

语法

<array_object_name>.itemsize

返回一个被视为字节数的整数值。

示例1

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

步骤

  • 步骤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函数则计算整个数组占用的内存空间。

它们被应用于各个领域,如机器学习、数据分析和计算物理学,将数据转换为不同形式等。它还被用于对数据进行各种数值和理论计算。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程