What is Numpy?Numpy is an open-source library. It is a numerical Python library. It contains a multi-dimensional array and matrix data structures and many mathematical, algebraic and transformation functions.
什么是Numpy? Numpy是一个开源库。 这是一个数字Python库。 它包含多维数组和矩阵数据结构以及许多数学,代数和转换函数。
What Numpy has to offer?Numpy is used to increase the speed of one’s python scripts. Python is inherently slow using a for loop but Numpy makes an iteration operation quicker. Numpy uses vectorization causing less memory to be used in the system and to perform operations even more quicker.
Numpy提供什么? Numpy用于提高一个人的python脚本的速度。 使用for循环,Python本质上很慢,但是Numpy使迭代操作更快。 Numpy使用矢量化,从而减少了系统中要使用的内存,并更快地执行了操作。
How to install Numpy?The easiest way to install NumPy is by using Pip.
如何安装Numpy? 安装NumPy的最简单方法是使用Pip。
pip install numpy
Using Numpy?To use Numpy and it’s functionality, it is necessary to always “import numpy” in your coding environment before using it. Conventionally, we import numpy using the following command:
使用Numpy吗? 要使用Numpy及其功能,必须在使用之前始终在编码环境中“导入numpy” 。 按照惯例,我们使用以下命令导入numpy:
import numpy as np
Numpy ArraysNumpy arrays are homogeneous. They have only one type of data unlike the Python array which is heterogeneous. This is one reason why Numpy Arrays are faster than Python Arrays.
Numpy数组 Numpy数组是同构的。 与只有异类的Python数组不同,它们只有一种类型的数据。 这就是为什么Numpy数组比Python Array更快的原因之一。
Creation of Numpy ArraysAfter importing numpy, we can create numpy arrays as seen below.
创建Numpy数组导入numpy之后,我们可以创建numpy数组,如下所示。
import numpy as nparray_even = np.array([2, 4, 6, 8])
We created an array of even numbers. This is a one-dimensional array.
我们创建了一个偶数数组。 这是一维数组。
Dimension of Numpy ArraysNumpy arrays have dimensions returned in this format:
numpy的数组 numpy的阵列的尺寸已经返回该格式尺寸:
(a,b)where ‘a’ represents the number of rows in the array and ‘b’ represents the number of columns in the array.
To check for dimension of arrays, for example ‘array_even’ as above, we use “.shape” method:
要检查阵列的尺寸,例如“array_even”如上述,我们使用“.shape”的方法:
array_even.shapeThis returns (1,4) representing 1 row and 4 columns.
Size of Numpy ArraysWe can check for size of a NumpyArray using the “.size” method. The size of an array is an integer value representing the product of the number of rows and columns. The ‘array_even’ example as above would return a value of 4 after running and printing:
numpy的数组的大小 ,我们可以检查使用“.size”方法的NumpyArray的大小。 数组的大小是一个整数值,表示行数和列数的乘积。 上面的'array_even'示例在运行和打印后将返回值4:
array_even.size
Transpose of NumpyArraysThis interchanges the row with the column of an array and it is performed with the “.T” method. For example:
NumpyArrays的转置将行与数组的列互换,并使用“ .T”方法执行。 例如:
import numpy as nparray_l = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
array_l_transposed = array_l.T
Default ArraysYou can override default datatypes of an array datatype by specifying it’s datatype during creation. For example:
默认数组您可以通过在创建过程中指定数组数据类型的默认数据类型来覆盖它的默认数据类型。 例如:
import numpy as nparray_a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype =float)
The array created is a 2-dimensional array and the values are integer values initially but are overwritten to float datatypes. When array_a is printed, the following is gotten :
创建的数组是二维数组,其值最初是整数值,但被覆盖为float数据类型。 打印array_a时 ,得到以下内容:
[[1. 2. 3. 4.][5. 6. 7. 8.]]
[[1。 2. 3. 4。] [5。 6. 7. 8.]]
The code snippet below illustrates other ways how Arrays could be created:
下面的代码段说明了如何创建数组的其他方式:
import numpy as np
#The below will create an array(4 rows and 3 columns) filled with zeros.
#Note that the parameter of the method is shape.np.zeros((4, 3))#The below will create an array(1 row and 3 columns) filled with ones.
#Note that the parameter of the method is shape.np.ones((1, 3))#The below will create an array(3 rows and 4 columns) filled with 5.
#Note that the first parameter of the method is shape
#And the second parameter is the array element you intend to fill with.np.full((3, 4), 5)#The below will create an array(2 rows and 6 columns) filled with random numbers.np.random.random((2, 6))
Reshaping and Array IndexingReshaping is a method of transforming an array A to another array B with a different dimension that will have the same size as array A.Array indexing involves locating an element or elements of an array. This is illustrated in the code snippets below:
重塑和数组索引重塑是一种将数组A转换为另一个具有不同尺寸的数组B的方法,该数组将具有与数组A相同的大小。数组索引涉及查找数组中的一个或多个元素。 下面的代码段对此进行了说明:
import numpy as np
#Creating an array of 3 rows and 4 columnsarray_k = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])#The below allows array_K to be reshaped to another array L with both having same sizes of 12array_l = array_k.reshape(2, 6)#Negative indexing(Indexing from behind)
#The below will return 2print(array_k[0, -3])#The below will return 10print(array_k[2, 1])#Boolean Indexing method
#Using 'where'. The format is ==> np.where(condition, true value, false value)
#Using 'logical_and'. The format is ==> np.logical_and(condition1, condition2)
Array Math
数组数学
import numpy as np
#Creating an array of 3 rows and 4 columnsarray_k = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
array_t = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])#Returns sum of arrayarray_k.sum() #Returns sum of array across columnsarray_k.sum(axis=0) #Returns sum of array across rowsarray_k.sum(axis=1)#Returns array with cummulative sum of arrayarray_k.cumsum()
#Returns product of arrayarray_k.prod() #Returns array with cummulative product of arrayarray_k.cumprod() #Returns the average element of array array_k.mean() #Returns minimum value in array(Axis can be specified in parameter)array_k.min() #Returns maximum value in array(Axis can be specified in parameter)array_k.max() #Returns array of array_k in powers of array_tnp.power(array_k, array_t) #Creating an array of 3 rows and 3 columnsj = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]
k = np.array([[2, 2, 2], [3, 4, 5], [6, 7, 8]])
l = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
#Product of j,k,larray_JTimesKTimesL = np.array(j * k * l) #Returns the Range in the matrix(axis could be specified too)print(array_JTimesKTimesL.ptp())
BroadcastingBroadcasting allows us to use arrays of different shapes to perform operations. For smaller arrays to be compatible, it needs to have one of its dimension as “1” and the other dimension needs to match the larger array.
广播广播使我们可以使用不同形状的数组来执行操作。 为了使较小的阵列兼容,它需要将其尺寸之一设为“ 1”,而另一个尺寸则需要与较大的阵列匹配。
import numpy as npa = np.array([2, 4, 6]) #Has a shape of (1,3)
b = np.array([3]) #Has a shape of (1,1)#Hence a and b is compatiblec = a + b #Broadcasting takes placeprint(c) #Returns [5 7 9]