python numpy

1.numpy.random.randn

numpy.random.randn(d0, d1, ..., dn)

d0, d1, …, dn: should be positive integers, indicating dimensions.

Returns a value if there are no arguments, or (d0, d1, …, dn) values ​​if there are arguments, all randomly sampled from a standard normal distribution.

For random samples from N (\ mu, \ sigma ^ 2), use:

sigma np.random.randn(...) mu

 

2.numpy.random.rand

numpy.random.rand(d0, d1, ..., dn)

d0, d1, ..., dn : The dimensions of the returned array, all should be positive.

Creates an array of the given type, filling it in a uniformly distributed random sample [0,  1)

 

3.numpy.reshape

Gives a new shape to an array without changing its data.

>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])

4.shape function

The shape function is a function in numpy.core.fromnumeric, and its function is to view the dimension of a matrix or array.

>>> e = eye(3)  
>>> e  
array([[ 1.,  0.,  0.],  
       [ 0.,  1.,  0.],  
       [ 0.,  0.,  1.]])  
>>> e.shape  
(3, 3)  

Create a 4×2 matrix c, c.shape[1] is the length of the first dimension, and c.shape[0] is the length of the second dimension.

>>> c = array([[1,1],[1,2],[1,3],[1,4]])  
>>> c.shape  
(4, 2)  
>>> c.shape[0]  
4  
>>> c.shape[1]  
2  

 

5.numpy.zeros

用法:zeros(shape, dtype=float, order='C')

Returns: returns a 0-filled array of the given shape and type;

Parameters: shape: shape

            dtype: data type, optional parameter, default numpy.float64

            dtype type: t, bit field, such as t4 represents 4 bits

                                 b, boolean, true or false

                                 i, integer, such as i8 (64-bit)

                                u, unsigned integer, u8 (64 bits)

                                f, float, f8 (64-bit)

                               c, floating-point negative number,

                                o, object,

                               s, a, string, s24

                               u, unicode, u24

            order: optional parameter, c represents similar to c language, row priority; F represents column priority

example:

np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
s = (2,2)
np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])

 

6.argsort ()

1. First define an array data

import numpy as np
x=np.array([1,4,3,-1,6,9])

2. Now we can see what the specific function of the argsort() function is:

x.argsort ()

The output is defined as y=array([3,0,2,1,4,5]).

We found that the argsort() function arranges the elements in x from small to large , extracts its corresponding index (index), and then outputs it to y .

3. Since I encountered a form similar to np.argsort()[num] in the program , I couldn't understand it, so I went to the python environment and tried it myself:

ps: The absolute value of num here is less than or equal to the number of elements in x

When num>=0, np.argsort()[num] can be understood as y[num];

When num<0, np.argsort()[num] is to output the elements of the array y in reverse , for example, np.argsort()[-1] is to output the index corresponding to the maximum value in x, np.argsort()[ -2] That is, the index corresponding to the second largest value in x is output, and so on. .

Intuitive experiments can only see the effect, the following is the verification I did with the above example:

This is the output when num is negative .

This is the output when num>=0.

 

7.dot()

The dot() function is matrix multiplication, and * means element-by-element multiplication

 

8.random.choice()

You can randomly select content from an int number or a 1-dimensional array, and put the selection result into an n-dimensional array and return it.

numpy.random.choice(a, size=None, replace=True, p=None)

a : 1-D array-like or int If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)

size : int or tuple of ints, optional

replace : boolean, optional Whether the sample is with or without replacement

p : 1-D array-like, optional The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

9.numpy.flatnonzero():

This function takes a matrix and returns the position (index) of the non-zero elements in the flattened matrix

This is the usage given by the official document. It is very formal. Input a matrix and return the position of non-zero elements in it.

>>> x = np.arange(-2, 3)
>>> x
array([-2, -1,  0,  1,  2])
>>> np.flatnonzero(x)
array([0, 1, 3, 4])

Used to return the position of a particular element:

The judgment d==3 of the vector elements returns a matrix composed of 0/1 with the same length as the vector, and then the function is called, and the returned position is the position corresponding to the element to be found.

d = np.array([1,2,3,4,4,3,5,3,6])
haa = np.flatnangwi (d == 3 )
print haa

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024230&siteId=291194637