numpy矩阵运算加速原理_numpy的基本原理

numpy矩阵运算加速原理

Over the course of this article, we shall learn the various features and functions of the Python library, NumPy

在本文的整个过程中,我们将学习Python库NumPy的各种特性和功能。

NumPy is one of the Python libraries, that supports multi-dimensional, substantial arrays as well as matrices. It also supports a large collection of mathematical functions to operate on these arrays. NumPy provides a strong base for many other data science and data visualization libraries.

NumPy是Python库之一,它支持多维的,实质性的数组以及矩阵。 它还支持在这些数组上运行的大量数学函数。 NumPy为许多其他数据科学和数据可视化库提供了坚实的基础。

In this article we will create, index and manipulate arrays using NumPy. (You will need to have prior knowledge on how tuples and lists work in Python.)

在本文中,我们将使用NumPy创建,索引和操作数组。 (您将需要具有有关元组和列表在Python中如何工作的先验知识。)

First, we’ll look at NumPy arrays. A NumPy array consists of values that can be indexed with slices, but also with boolean or integer arrays (masks). The shape of an array denotes the size of an array along each dimension and it is expressed using a tuple of integers as well. For a standard 2D array, the shape gives the number of rows followed by the number of columns.

首先,我们来看一下NumPy数组。 NumPy数组由可以用切片索引的值组成,但也可以用布尔或整数数组(掩码)索引。 数组的形状表示数组在每个维度上的大小,它也使用整数元组表示。 对于标准2D数组,形状将给出行数,然后是列数。

Before we start, make sure you have all the necessary libraries installed in your system.

在开始之前,请确保您已在系统中安装了所有必需的库。

Using conda:

使用conda:

conda install numpy

Using pip:

使用点子:

pip install numpy 

使用NumPy创建数组(Creating arrays using NumPy)

>>> import numpy as np
>>> a = np.array([1,22,333])
>>> print(a)
[1 22 333]

Let’s play around with arrays a little before we get into more of NumPy’s features and functions. We can check the size of our array in Python using the ‘.shape’ function.

在深入了解NumPy的特性和功能之前,让我们来研究一下数组。 我们可以使用'.shape'函数在Python中检查数组的大小。

>>> print(a.shape)
(3,)

Here we see the shape is (3, ) which means we’ve a 1 dimensional array of size 3. If we want to look at specific elements within the array, we use the same notations as we do in Python lists.

在这里,我们看到形状是(3,),这意味着我们有一个大小为3的一维数组。如果我们要查看数组中的特定元素,我们将使用与Python列表相同的符号。

>>> print(a[0])
1>>> print(a[1])
22>>> print(a[2])
333

Change a value at a specific index:

更改特定索引处的值:

>>> a[0] = 11
>>> print(a)
[ 11 22 333]

As you can see, the element at the first position has now changed from 1 to 11.

如您所见,第一个位置的元素现在已从1更改为11。

使用NumPy创建2D数组 (Create a 2D array using NumPy)

>>> b = np.array([[4,5,6],[7,8,9]])
>>> print(b)
[[4 5 6]
[7 8 9]]

Print the shape

打印形状

>>> b.shape
(2, 3)

This shows us that we’ve 2 rows and 3 columns.

这说明我们有2行3列。

Now to look at the individual elements in the 2D array we can do the following:

现在查看2D数组中的各个元素,我们可以执行以下操作:

>>> print(b[0,0])
4

We can make numpy arrays of different values, not just integers

我们可以制作不同值的numpy数组,而不仅仅是整数

>>> c = np.array([9.0, 8.7, 6.5])
>>> print(c)
[9. 8.7 6.5]

其他NumPy函数来构造数组 (Other NumPy functions to construct arrays)

NumPy provides different functions to create arrays. For example, If I wanted to create an array that is filled with zeros, I can do so easily using NumPy’s ‘.zeros’ function as shown below

NumPy提供了不同的函数来创建数组。 例如,如果我想创建一个由零填充的数组,则可以使用NumPy的'.zeros'函数轻松实现,如下所示

>>> d = np.zeros((5,6))
>>> print(d)
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

Similarly we can make an array filled with 1’s using the ‘.ones’ function

类似地,我们可以使用'.ones'函数创建一个由1填充的数组

>>> e = np.ones((5,6))
>>> print(e)
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]

Furthermore, you can create an array that is filled with some constant value by using the ‘.full’ function wherein you will need to mention the size of the array in the first part of the parenthesis and mention the constant value in the second part of the parenthesis. An instance of this is shown below where we create a 3x3 array filled with the value ‘7.8’ .

此外,您可以使用'.full'函数创建一个填充有一定常量值的数组,其中您需要在括号的第一部分中提及数组的大小,并在第二部分的中提及常量。括号。 下面显示了一个实例,我们在其中创建了一个3x3数组,其中填充了值'7.8'。

>>> f = np.full((3,3), 7.8)
>>> print(f)
[[7.8 7.8 7.8]
[7.8 7.8 7.8]
[7.8 7.8 7.8]]

NumPy also provides the random function. Using this we can create an array that consists of random values between 0 and 1

NumPy还提供随机函数。 使用此方法,我们可以创建一个由0到1之间的随机值组成的数组

>>> g= np.random.random((4,4))
>>> print(g)
[[0.21243056 0.4998238 0.46474266 0.24573327]
[0.80314845 0.94159578 0.65609858 0.0559475 ]
[0.80367609 0.35230391 0.91716958 0.03513166]
[0.37717325 0.00882003 0.82166044 0.7435783 ]]

使用NumPy进行索引 (Indexing using NumPy)

Numpy offers several ways to index arrays. Similar to python lists, NumPy arrays can be sliced. Since arrays can be multi-dimensional, you will need to specify the slice for each dimension of the array.

Numpy提供了几种索引数组的方法。 与python列表类似,可以对NumPy数组进行切片。 由于数组可以是多维的,因此您需要为数组的每个维度指定切片。

Let’s create a two-dimensional array with random values.

让我们创建一个带有随机值的二维数组。

>>> i = np.array([[12,23,34], [45,56,67], [78,89,90]])
>>> print(i)
[[12 23 34]
[45 56 67]
[78 89 90]]

For this example, lets use slicing to pull out the a part of the array that consists of the first 2 rows and columns 1 and 2

对于此示例,让我们使用切片来提取由前2行以及第1和第2列组成的数组的一部分

>>> j = i[:2, 1:3]
>>> print(j)
[[23 34]
[56 67]]

A slice of an array (as shown above) is a view into the same data.

数组的一个切片(如上所示)是对相同数据的视图。

>>> print(i[0,1])
23

You can change the value at any position in the array.

您可以在数组的任何位置更改值。

>>> j[0,0]=75
>>> print(i[0,1])
75>>> print(i)
[[ 1 75 3]
[ 4 5 6]
[ 7 8 9]]

NumPy中的布尔数组索引 (Boolean array indexing in NumPy)

Boolean array indexing type of indexing is used to select the elements of an array that satisfy some condition. So lets create an array of some random values, apply a certain condition and see how boolean array indexing works.

索引的布尔数组索引类型用于选择满足某些条件的数组元素。 因此,让我们创建一个包含一些随机值的数组,应用一个特定条件,然后看看布尔数组索引是如何工作的。

>>> k = np.array([[26,78], [51,42], [30,89]])
>>> print(k)
[[26 78]
[51 42]
[30 89]]>>> print(k>50)
[[False True]
[ True False]
[False True]]

We can also use boolean array indexing to create a new one dimensional array consisting of all the elements that are actually greater than 50

我们还可以使用布尔数组索引来创建一个新的一维数组,其中包含实际上大于50的所有元素

>>> print(k[k>50])
[78 51 89]

NumPy数学函数 (NumPy Math Functions)

Basic math functions operate element wise on arrays. Which means that an element in one array corresponds to an element in another array in the same position. You can use both math operators or the functions provided by NumPy. Both give us the same outputs. Lets perform a few math operations on arrays.

基本数学函数在数组上按元素进行运算。 这意味着一个数组中的元素与相同位置的另一个数组中的元素相对应。 您可以使用数学运算符或NumPy提供的函数。 两者都给我们相同的输出。 让我们对数组执行一些数学运算。

  1. Addition

    加成
>>> l = np.array([[1,2], [3,4]])
>>> m = np.array([[5,6], [7,8]])
>>> print(l+m)
[[ 6 8]
[10 12]]

This adds elements in the arrays according to their corresponding positions. We can acquire similar results using the NumPy function.

这将根据元素在数组中的相应位置添加元素。 我们可以使用NumPy函数获得类似的结果。

>>> print(np.add(l,m))
[[ 6 8]
[10 12]]

In addition to this, NumPy has functions for subtraction, multiplication division and computing the sum of the array as well.

除此之外,NumPy还具有用于减法,乘法除法以及计算数组和的功能。

2. Subtraction

2.减法

# Subtraction using math operators
>>> print(l-m)
[[ -4 -4]
[-4 -4]]# Subtraction using NumPy functions
>>> print(np.subtract(l,m))
[[ -4 -4]
[-4 -4]]

3. Multiplication

3.乘法

# Multiplication using math operators
>>> print(l*m)
[[ 5 12]
[21 32]]# Multiplication using NumPy functions
>>> print(np.multiply(l,m))
[[ 5 12]
[21 32]]

4. Division

4.师

# Division using math operators
>>> print(l/m)
[[0.2 0.33333333]
[0.42857143 0.5 ]]# Division using NumPy functions
>>> print(np.divide(l,m))
[[0.2 0.33333333]
[0.42857143 0.5 ]]

5. Sum

5.总和

>>> print(l)
[[1 2]
[3 4]]#sum of all the elements
>>> print(np.sum(l))
10#sum of columns
>>> print(np.sum(l, axis=0))
[4 6]#sum of row
>>> print(np.sum(l, axis=1))
[3 7]

You can find the code for this tutorial here.

您可以在此处找到本教程的代码。

I hope you enjoyed this article. Thank you for giving it a read!

希望您喜欢这篇文章。 感谢您阅读!

[1] NumPy documentation: https://numpy.org/doc/

[1] NumPy文档: https : //numpy.org/doc/

翻译自: https://medium.com/@jendcruz23/fundamentals-of-numpy-a7e94d260845

numpy矩阵运算加速原理

你可能感兴趣的:(python,numpy,人工智能,算法,深度学习)