Python 如何使用动态数组执行Numpy广播

Python 如何使用动态数组执行Numpy广播

“广播”是指NumPy在进行算术运算时如何处理不同维度的数组。较小的数组在较大的数组上“广播”,在一定的限制下,以确保它们的形状一致。广播允许您矢量化数组操作,使您能够使用C而不是Python进行循环。

这样可以实现无需不必要的数据复制,从而实现高效的算法实现。在某些情况下,广播是一个负面的想法,因为它导致了浪费的内存利用,从而减慢了计算速度。

在本文中,我们将向您展示如何使用Python在NumPy数组上执行广播。

执行给定数组的广播步骤 –

  • 步骤1. 创建两个具有兼容维度的数组

  • 步骤2. 打印给定的数组

  • 步骤3. 对两个数组执行算术运算

  • 步骤4. 打印结果数组

将两个不同维度的数组相加

使用arange()函数创建一个由0到n-1的数字组成的numpy数组(arange()函数返回在给定间隔内均匀间隔的值。在半开区间[start,stop]内,生成值)并将其与某个常数相加。

示例

import numpy as np
# Getting list of numbers from 0 to 7
givenArray = np.arange(8)

# Adding a number to the numpy array 
result_array = givenArray + 9
print("The input array",givenArray)
print("Result array after adding 9 to the input array",result_array)

输出

The input array [0 1 2 3 4 5 6 7]
Result array after adding 9 to the input array [ 9 10 11 12 13 14 15 16]

给定的数组具有一个维度(轴),长度为8,而9是一个没有维度的简单整数。由于它们的维度不同,Numpy尝试沿某个轴广播(只是拉伸)较小的数组,使其适合数学运算。

对具有兼容维度的两个数组求和

使用arange()函数从0到n-1创建两个NumPy数组,并使用reshape()函数对其进行重塑(重塑数组而不影响其数据)。两个数组具有兼容的维度(3,4)和(3,1),并添加对应的元素。

示例

import numpy as np
# Getting the list of numbers from 0 to 11 and reshaping it to 3 rows and 4 columns
givenArray_1 = np.arange(12).reshape(3, 4)

# Printing the shape(rowsize, columnsize) of array
print("The shape of Array_1 = ", givenArray_1.shape)

# Getting list of numbers from 0 to 2 and reshaping it to 3 rows and 1 columns
givenArray_2 = np.arange(3).reshape(3, 1)
print("The shape of Array_2 = ", givenArray_2.shape)

# Summing both the arrays
print("Input array 1 \n",givenArray_1)
print("Input array 2 \n",givenArray_2)
print("Summing both the arrays:")
print(givenArray_1 + givenArray_2)

输出

The shape of Array_1 =  (3, 4)
The shape of Array_2 =  (3, 1)
Input array 1 
 [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11] ]
Input array 2 
 [[0]
  [1]
  [2]]
Summing both the arrays:
[[ 0  1  2  3]
 [ 5  6  7  8]
 [10 11 12 13]]

给定的givenArray_2沿着第二个维度扩展以匹配givenArray_1的维度。由于两个数组的维度是兼容的,这是可能的。

将具有不兼容维度的两个数组相加

创建两个具有不兼容维度(6,4)和(6,1)的NumPy数组。当我们尝试添加两个数组的相应元素时,它会引发以下错误。

示例

import numpy as np
# Getting a list of numbers from 0 to 11 and reshaping it to 3 rows and 4 columns
givenArray_1 = np.arange(20).reshape(6, 4)

# Printing the shape(rowsize, columnsize) of array
print("The shape of Array_1 = ", givenArray_1.shape)

# Getting list of numbers from 0 to 5 and reshaping it to 3 rows and 1 columns
givenArray_2 = np.arange(6).reshape(6, 1)
print("The shape of Array_2 = ", givenArray_2.shape)

# Summing both the arrays
print("Summing both the arrays:")
print(givenArray_1 + givenArray_2)

输出

Traceback (most recent call last):
  File "main.py", line 3, in 
    givenArray_1 = np.arange(20).reshape(6, 4)
ValueError: cannot reshape array of size 20 into shape (6,4)

行数为6,列数为4。

无法插入到大小为20的矩阵中(需要一个大小为6*4=24的矩阵)。

求和Numpy多维数组和线性数组

使用arange()函数创建一个多维数组,并使用reshape()函数将其重塑为一些随机行数和列数。使用arange()函数创建另一个线性数组,并对这两个数组求和。

示例1

import numpy as np
# Getting list of numbers from 0 to 14 and reshaping it to 5 rows and 3 columns
givenArray_1 = np.arange(15).reshape(5, 3)

# Printing the shape(rowsize, columnsize) of array
print("The shape of Array_1 = ", givenArray_1.shape)

# Getting list of numbers from 0 to 2
givenArray_2 = np.arange(3)
print("The shape of Array_2 = ", givenArray_2.shape)

# Summing both the arrays
print("Array 1 \n",givenArray_1)
print("Array 2 \n",givenArray_2)
print("Summing both the arrays: \n",givenArray_1 + givenArray_2)

输出

The shape of Array_1 =  (5, 3)
The shape of Array_2 =  (3,)
Array 1 
 [[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]
  [12 13 14]]
Array 2 
 [0 1 2]
Summing both the arrays: 
 [[ 0  2  4]
  [ 3  5  7]
  [ 6  8 10]
  [ 9 11 13]
  [12 14 16]]

给定的线性数组被扩展以匹配给定数组1(多维数组)的维度。由于两个数组的维度是兼容的,这是可能的。

示例2

import numpy as np
givenArray_1 = np.arange(240).reshape(6, 5, 4, 2)
print("The shape of Array_1: ", givenArray_1.shape)

givenArray_2 = np.arange(20).reshape(5, 4, 1)
print("The shape of Array_2: ", givenArray_2.shape)

# Summing both the arrays and printing the shape of it
print("Summing both the arrays and printing the shape of it:")
print((givenArray_1 + givenArray_2).shape)

输出

The shape of Array_1:  (6, 5, 4, 2)
The shape of Array_2:  (5, 4, 1)
Summing both the arrays and printing the shape of it:
(6, 5, 4, 2)

重要的是要理解,可以沿着多个维度传播多个数组。 Array1的维度为(6, 5, 4, 2),而array2的维度为(5, 4, 1)。维度数组是通过将array1沿第三维度拉伸和array2沿第一和第二维度(6, 5, 4, 2)形成的。

结论

Numpy广播比在数组上循环更快。从第一个示例开始。用户可以通过循环数组,将相同的数字添加到数组中的每个元素中。这种方法的缓慢原因有两个:循环需要与Python循环进行接口,这会减慢C实现的速度。其次,NumPy使用步幅代替循环。将步幅设置为0允许您无限循环地遍历组件,而不会有内存开销。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程