Python Scientific computing _numpy_ function library

Source: Internet
Author: User

1. General functions and sorting

Common statistical functions:

Sum: sum (), mean: mean (), Standard deviation: STD (), Variance: Var (), Minimum: Min (), Max: Max (), difference between maximum and minimum: PTP (), Subscript for maximum value: Argmax (), subscript of the lowest value: Argmin (), Median: Median ()

All of the above functions can specify axis to operate along an axis, except for the mean () function, you can also use average (), and you can specify weights parameters to specify weights, calculate weighted averages, Argmax () and argmin () If you do not specify an axis parameter, Returns the flattened subscript;

Sort function: Sort (), argsort (); the sort () method of the array changes the group itself, while the sort () function does not; Sort by default Axis=-1, which is sorted along the last axis; sort () The function returns a new sorted array and Argsort () returns the sorted subscript array, such as:

Import NumPy as NP
A = Np.random.randint (0,10,size= (4,5))

Array ([[8, 2, 0, 3, 7],
[7, 0, 2, 7, 4],
[1, 8, 1, 1, 2],
[4, 6, 2, 1, 4]])

A.sort ()? #此时a数组会改变为排序后的数组;

A1 = Np.sort (a)? #此时a数组不变, the resulting A1 array is:

Array ([[0, 2, 3, 7, 8],
[0, 2, 4, 7, 7],
[1, 1, 1, 2, 8],
[1, 2, 4, 4, 6]])

# That's the result of sorting along the 1 axis

IDX = Np.argsort (a)?

Idx

Array ([[2, 1, 3, 4, 0],
[1, 2, 4, 0, 3],
[0, 2, 3, 4, 1],
[3, 2, 0, 4, 1]])

At this point, although idx.shape = (4,5), but not directly using A[IDX] to get the sorted array, because IDX is an array, so numpy will be the following:, equivalent to: A[IDX,:],IDX array of each value access to the 0 axis of an array, An error occurs when the value in the IDX exceeds the 0-axis length of a: Indexerror:index 4 is out of the bounds for axis 0 with size 4; So using IDX to access a sorted array requires a 0-axis subscript, through NP.OGR The ID object can be generated:

X,_ =?np.ogrid[:a.shape[0],:a.shape[1]]

The expression on the right generates a grid of array A, which is the subscript array for each axis, taking the first element to get an array of 0-axis subscript arrays:

Array ([[0],
[1],
[2],
[3]])

The sorted array can be correctly accessed through A[X,IDX]

2. Polynomial functions

A polynomial function is a function that contains only addition and multiplication, adding and multiplying operations on the power of a variable:

f (x) = A[n]*x^n + a[n-1]*x^ (n-1) + ... + a[2]*x^2 + a[1]*x + a[0]

A polynomial function is represented in numpy by the coefficients of each power of the variable x (from the order of high to the bottom):

A = Np.array ([1.0,0,-2,1])

p = np.poly1d (a)

P is a poly1d object, which can be called as a function, and returns the value of a polynomial, and is a Ufunc object that can be used as an array parameter to get an array of the corresponding polynomial values;

POLY1D objects can be arithmetic, corresponding to the arithmetic of the polynomial, when the division operation returns a tuple of two values, where the first value is the quotient polynomial (quotient) and the second value is the remainder polynomial (redundancy);

POLY1D objects can be integrated: Deriv () and differential: Integ () operation, get new polynomial;

You can use the Np.roots () function to root a polynomial:

R = Np.roots (p)

Array ([ -1.61803399,? 1.????,? 0.61803399])

Using the Np.poly () function, you can turn the root back to the coefficients of the polynomial:

Np.poly (R)

Array ([1.00000000e+00, -1.22124533e-15, -2.00000000e+00,
1.00000000E+00])

With the Np.polyfit () function, a set of data can be polynomial-fitted:

Import Matplotlib.pyplot as Plt
Plt.figure (figsize= (8,4))
For deg in [3,5,7]:
? ? A = Np.polyfit (x,y,deg)
? ? Error = Np.abs (Np.polyval (a,x)-y)
? ? Plt.plot (Error)
Plt.ylim (0,0.0001)
Plt.show ()

?

3. Segmented functions

Segmented operations with three functions: where (), select (), piecewise ()

The Where (condition,x,y) function is similar to the three-mesh operator, returns X when condition is true, otherwise returns Y, or, if the shape of x and Y is inconsistent, by broadcasting a uniform shape;

When the number of fragments is relatively long, the where () function is more complex to write, the Select (condlist, choicelist) function, through a list of Boolean arrays of length n, ChoiceList is a list of arrays of storage candidates of length n, Where all arrays are of length m;

Condlist can be visually understood as a condition of multiple segments, n is the number of segments, and ChoiceList is the value under each condition, with If-elif-else as:

If CONDLIST[0]:
? ? CHOICELIST[0]
Elif Condlist[1]:
? ? CHOICELIST[1]
Elif Condlist[2]:
? ? CHOICELIST[2]

such as

The piecewise (x, condlist, funclist) function is designed to avoid the need to use many arrays to store the segmented structure when the segment is relatively long, and the piecewise () function can calculate the result directly from the segmented list without needing an intermediate fragment array; and select ( ) parameter, Funclist is a list of functions, corresponding to the conditions in the condlist execution;

4. Statistical functions

The unique () function returns all the different values in the parameter array and sorts from small to large, equivalent to the Sort-u command in Linux, which has two optional parameters:

Return_index:true indicates that the subscript of the original array is also returned;

Return_inverse:true indicates that the array of subscripts used to reconstruct the original array is also returned;

The Bincount () function counts the number of occurrences of each element in an integer array (all elements in the parameter array must be non-negative), and the first element in the returned array represents the number of integer I occurrences in the parameter array.

Histogram (A, bins=10, Range=none, Normed=false, Weights=none) functions histogram statistics of one-dimensional arrays; The function returns two one-dimensional arrays: hist and bin_edges;

Histogram2d (), HISTOGRAMDD () Histogram statistics for two-dimensional and n-dimensional arrays

?


??

Python Scientific computing _numpy_ function library

Related Article

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.