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 and 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
rows函数的使用方法及实例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使⽤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.
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=
You can find the code for this tutorial .
您可以在到本教程的代码。
I hope you enjoyed this article. Thank you for giving it a read!
希望您喜欢这篇⽂章。 感谢您阅读!
[1] NumPy⽂档: :
numpy矩阵运算加速原理

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。