NumPy Common functions

Source: Internet
Author: User
Tags sorts

1 sum function sum ()

quadrature function Product ()

Weighted average number average ()

var () to calculate variance

Mean () expectations

STD () to find standard deviation

Note: in the sum () function, you can also sum a sequence similar to an array, such as a list ganso. When the array is multidimensional, he calculates the and of all the elements. If the dimension parameter axis is specified, the sum is carried along the specified axis. For two-bit arrays, if axis=0, the array is summed along the No. 0 axis, which sums each column to get a row matrix. If Axis = 1 represents a sum along the 1th axis, it is the sum of each row.

#-*-Coding:utf-8-*-"" "Spyder Editorthis is a temporary script file." " Import NumPy as Npfrom numpy import random as Nrx = Nr.randint (1,10, (2,3)) print xprint np.sum (x) print np.sum (x,axis = 0) pr int np.sum (X,axis = 1)

[[8 2 7] [3 2 4]]26[11  4 11][17  

STD and var calculate the standard deviation and variance of the sample respectively, the variance has two meanings: the difference of the folk prescription and the non-prescription, the concrete meaning is defined in the theory. The Var function has a parameter ddof, and when Ddof is 0 o'clock, the sample variance is computed, and when the ddof is 1 o'clock, the sample is calculated without a recipe difference and the default value is 0.

#-*-Coding:utf-8-*-"" "Spyder Editorthis is a temporary script file." " Import NumPy as Npfrom numpy import random as nr# generate a normal distribution sample A = Nr.normal (0,2.0,size = (10000,10)) # Calculated with a prescription difference V1 = Np.var (a,axis = 1) #计算无偏方差v2 = Np.var (A,axis = 1,ddof = 1) print V1.mean () print V2.mean ()

We can see that the expectation of poor folk remedies is smaller than the true variance.

2 min (), max () minimum and maximum, you can also specify the axis parameter

Minimum (), maximum () $ two min and two Max

PTP () difference between maximum and minimum values

Argmin () and Argmax () minimum subscript and Max subscript

Sort () array sorting

Argsort () calculates the subscript for array sorting

Lexsort () Multi-column sorting

Partition () fast calculation before K-bit

Argpatiton () Top K-position subscript

Median of median ()

Percentile () percentile

Searchsorted () Two-point search

#-*-Coding:utf-8-*-
"""
Spyder Editor

This is a temporary script file.
"""
Import NumPy as NP
From NumPy import random as NR

x = nr.randint (1,10,size = 10)
Print X
Max_pos = Np.argmax (x)
Min_pos = np.argmin (x)
# Subscript for output maximum and minimum values
Print Max_pos,min_pos


# sorting an array
x = Nr.randint (1,30,size = (3,4))
Print X
Print

# The default axis in sort is-1, which is to sort the final axis
Print Np.sort (x)
Print

#指定对0轴排序, which is to sort each column
Print Np.sort (x, axis = 0)
Print

# set Axis to None, at which point he will get a new array of flat after sort
Print Np.sort (x, axis = None)


# use Argsort () to return the array after sorting the subscript
#按照0轴排序
Print Np.argsort (x,axis = 0)
Print

#按照1轴排序
Print Np.argsort (x, axis = 1)

The following results are output:

[1 9 7 1 6 9 5 7 1 4]
1 0

[[8 2 25 26]
[5 16 25 9]
[19 28 18 11]]

[[2 8 25 26]
[5 9 16 25]
[11 18 19 28]]

[[5 2 18 9]
[8 16 25 11]
[19 28 25 26]]

[2 5 8 9 11 16 18 19 25 25 26 28]

[[1 0 2 1]
[0 1 0 2]
[2 2 1 0]]

[[1 0 2 3]
[0 3 1 2]
[3 2 0 1]]

The LEGSORTD parameter is an array of shape (k,n), or a sequence of K-length arrays of N. Legsort reverse sorts the subscript and needs attention when she sorts the primary key according to the last behavior in the array.

#-*-Coding:utf-8-*-"" "Spyder Editorthis is a temporary script file." " Import NumPy as Npfrom numpy import random as Nrnames = ["Liming", "Liuxiang", "Mike", "Xiaoming", "kangkang"]ages =  [23, 12,34,22,14] #ages为当前最后一行, with ages primary key idx = Np.lexsort ([names,ages]) Sorted_data = Np.array (Zip (names,ages)) Print Sorted_ Dataprint #二维数组排序默认以最后一行为主键x = Nr.randint (1,100,size= (4,5)) print xprint IDX = np.lexsort (x) print X[:,idx]

[' liming ' ['] ['] [' Liuxiang '] [' Mike '] [' xiaoming '] [' Kangkang ' 14 '] [[7] [7] [9] [46] [94] [] [+] [] [+] [] [+] [] [23] [93  ]  

NumPy Common functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.