Numpy statistical calculations compare the array, look at this is enough

Author: Wei River with TU Ming Zhang Xiupeng
For reprint please contact large data DT (ID: bigdatadt)

01 Numpy of statistical calculation method

NumPy built a lot of calculation methods, the most important statistical methods and explained as follows.

  • sum (): calculating matrix elements and; matrix calculation result is a one-dimensional array, it is necessary to specify rows or columns.
  • mean (): matrix elements calculated average value; matrix calculation result is a one-dimensional array, it is necessary to specify rows or columns.
  • max (): maximum value calculation of matrix elements; matrix calculation result is a one-dimensional array, it is necessary to specify rows or columns.
  • mean (): average calculating matrix elements.
  • median (): matrix elements calculated median.

Note that the values ​​used for these types of statistical methods must be int or float.

Array the following sample code:

vector = numpy.array([5, 10, 15, 20])
vector.sum()

The result is 50

Matrix following sample code:

matrix=
array([[ 5, 10, 15],
       [20, 10, 30],
       [35, 40, 45]])
matrix.sum(axis=1)
array([ 30,  60, 120])
matrix.sum(axis=0)
array([60, 60, 90])

As shown in the above example, axis = 1 and row are calculated, the results are presented in columns. axis = 0 is calculated and columns, results are presented in rows.

Extended Learning:

The official recommendation is a good introductory tutorial choice.

https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

02 Numpy in operation arg

argmax function is used to find a maximum value of the array index. Simply put, the index (position) corresponding to the maximum number is. Sample code is as follows:

ndex2 = np.argmax([1,2,6,3,2])        #返回的是2

argmin seeking function can be used in an array index minimum, usage and the like argmax. Sample code is as follows:

index2 = np.argmin([1,2,6,3,2]) #返回的是0

Let's explore the sort under Numpy matrix and how to use the index, sample code as follows:

import numpy as np
x = np.arange(15)
print(x)    # array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
np.random.shuffle(x)    #随机打乱
print(x)    # array([ 8, 13, 12,  3,  9,  2, 10,  0, 11,  5, 14,  7,  1,  4,  6])
sx = np.argsort(x)    #从小到大排序,返回索引值
print(sx)    # [ 7 12  5  3 13  9 14 11  0  4  6  8  2  1 10]

Here briefly explain, the first element of the vector x 7 represents the index address 0, and the second element represents the index address 12 x 1 vector, so the other elements.

03 FancyIndexing

To index a vector value is relatively easy, such as by x [0] to the value. However, if you want to take a few more complex, for example, need to return the third, fifth and eighth element, how should we do? Sample code is as follows:

import numpy as np
x = np.arange(15)
ind = [3,5,8]
print(x[ind]) #使用fancyindexing就可以解决这个问题

We can also form a new two-dimensional matrix from a one-dimensional vector, the following sample code:

import numpy as np
x = np.arange(15)
np.random.shuffle(x)
ind = np.array([[0,2],[1,3]])  #第一行需要取x向量中索引为0的元素,以及索引为2的元素,第二行需要取x向量中索引为1的元素以及索引为3的元素
print(x)
print(x[ind])

We look at the output it is easy to understand:

[ 3  2  7 12  9 13 11 14 10  5  4  1  6  8  0]
[[ 3  7]
 [ 2 12]]

For two-dimensional matrix, we use fancyindexing number is relatively easy to take, the following sample code:

import numpy as np
x = np.arange(16)
X = x.reshape(4,-1)
row = np.array([0,1,2])
col = np.array([1,2,3])
print(X[row,col])        #相当于取三个点,分别是(0,1),(1,2),(2,3)
print(X[1:3,col])        #相当于取第23行,以及需要的列

04 Numpy Array Comparisons

Numpy have a powerful function is to compare arrays or matrices, after the data comparison will produce a boolean value. Sample code is as follows:

import numpy as np
matrix = np.array([
 [5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
m = (matrix == 25)
print(m)

We see the output results are as follows:

[[False False False]
 [False  True False]
 [False False False]]

Next, look at a more complex example, the following sample code:

import numpy as np
matrix = np.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
 ])
second_column_25 =  (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])

The above code, print (second_column_25) is output [False, True False], first matrix [:, 1] represents all the rows, and the column index 1, i.e., [10,25,40], and finally 25 are compared, get is [False, True, False]. print (matrix [second_column_25,:]) it is representative of the line that returns a true value data, i.e. [20, 25, 30].

After comparison, we can np.count_nonzero (x <= 3) to calculate the number of elements less than or equal to 3, and 1 represents True, 0 representative of False. It can also np.any (x == 0), as long as there is an element of x is equal to 0 returns True. np.all (x> 0) all the elements is required before returning greater than 0 True. This can help us to determine whether the data in the x meet certain conditions.

About the author: Wei River with Master of Artificial Intelligence University of Edinburgh, Ali Baba Dharma hospital algorithm experts in computer vision, large data fields with more than 8 years of architecture and algorithm development experience.

Tu Ming, Alibaba data architect for big data, natural language processing, image recognition, Python, Java-related technologies have in-depth research, has accumulated a wealth of practical experience.

Zhangxiu Peng, graduated from Central South University, Alibaba technology development experts, engaged in the commercialization of cloud computing, big data, artificial intelligence and networking technology, for the first time the image recognition technology into industry in Alibaba, and promote product image recognition technology, platform.

This article Adapted from "deep learning and image recognition: Principles and Practice", authorized by the publisher released.

Here Insert Picture Description
Further Reading "deep learning and image recognition: Principles and Practice"
reprint please contact micro letter: DoctorData

EBook Buy Link: https://mall.csdn.net/v2/#/product/1637

Caution

This book has been in our VIP members' equity, you want to read this book for free can join our VIP members, oh, hundreds of free smooth read this e-book technology, members can not only enjoy the added value of million free books read permission, members also enjoy additional rights and interests, come to know me! https://mall.csdn.net/vip?type=v2

Released nine original articles · won praise 18 · views 3250

Guess you like

Origin blog.csdn.net/weixin_37649168/article/details/104214154