numpy (reprint) learning numpy Python data analysis of the study

Data analysis of learning Python numpy

Python numpy module, which is a powerful processing module array, and the core of the module but also in other data analysis module (e.g., pandas and SciPy) a.

This will take the following five aspects to introduce content numpy module:

1) create an array of

2) properties and functions related arrays

3) to obtain an array element - the general index, sliced, and fancy index index Boolean

4) Statistics function and linear algebra

5) for generating random numbers

 

Creating arrays

numpy use array () function to create an array, Array's first argument must be a sequence, either tuples can also be a list.

Create a one-dimensional array

Function creates a one-dimensional ordered array can use numpy in arange (), which is an extended version of the built-in function range.

In [1]: import numpy as np

In [2]: ls1 = range(10)

In [3]: list(ls1)

Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [4]: type(ls1)

Out[4]: range

 

In [5]: ls2 = np.arange(10)

In [6]: list(ls2)

Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [7]: type(ls2)

Out[7]: numpy.ndarray

By arange generated sequence is not a simple list type, but rather a one-dimensional array.

 

If the law is not a one-dimensional array of ordered elements, but human input, we need to array () function creates.

In [8]: arr1 = np.array((1,20,13,28,22))

In [9]: arr1

Out[9]: array([ 1, 20, 13, 28, 22])

In [10]: type(arr1)

Out[10]: numpy.ndarray

The above is a tuple of one-dimensional array consisting of a sequence.

 

In [11]: arr2 = np.array([1,1,2,3,5,8,13,21])    

In [12]: arr2

Out[12]: array([ 1,  1,  2,  3,  5,  8, 13, 21])

In [13]: type(arr2)

Out[13]: numpy.ndarray

The above is a list of one-dimensional array consisting of a sequence.

 

Create a two-dimensional array

Create a two-dimensional array, in fact, that the list set list or set of tuples tuple.

In [14]: arr3 = np.array(((1,1,2,3),(5,8,13,21),(34,55,89,144)))

In [15]: arr3

Out[15]:

array([[  1,   1,   2,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144]])

Used above tuple tuple set mode.

 

In [16]: arr4 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

In [17]: arr4

Out[17]:

array([[ 1,  2,  3,  4],

[ 5,  6,  7,  8],

[ 9, 10, 11, 12]])

Use the above list set list approach.

For high-dimensional array of data analysis used less in the future, where about creating high-dimensional array not go into details, set construction method is still the way.

 

Described above are set manually one-dimensional, two-dimensional or higher-dimensional array, numpy also provides several special array, they are:

In [18]: np.ones (3) # Returns the one-dimensional array elements are all 1

Out[18]: array([ 1.,  1.,  1.])

 

In [19]: np.ones ([3,4]) # Returns all the elements of a two-dimensional array of 3 × 4

Out[19]:

array([[ 1.,  1.,  1.,  1.],

[ 1.,  1.,  1.,  1.],

[ 1.,  1.,  1.,  1.]])

 

In [20]: np.zeros (3) # Returns the one-dimensional array elements are all 0

Out[20]: array([ 0.,  0.,  0.])

 

In [21]: np.zeros ([3,4]) # 0 returns all the elements of a two-dimensional array of 3 × 4

Out[21]:

array([[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.]])

 

In [22]: np.empty (3) # returns an empty one-dimensional array

Out[22]: array([ 0.,  0.,  0.])

 

In [23]: np.empty ([3,4]) # Returns the empty two-dimensional array of 3 × 4

Out[23]:

array([[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.]])

 

Properties and functions related to arrays

When constructing an array good, we take a look at the operation and what the properties and function of the array itself:

In [24]: arr3

Out[24]:

array([[  1,   1,   2,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144]])

 

In [25]: the number of rows and columns of the array of all of arr3.shape #shape

Out[25]: (3, 4)

 

In [26]: arr3.dtype #dtype method returns an array of data types

Out[26]: dtype('int32')

 

In [27]: a = arr3.ravel () # ravel by a method of straightening array (multidimensional array is reduced to one-dimensional arrays)

In [28]: a

Out[28]: array([  1,   1,   2,   3,   5,   8,  13,  21,  34,  55,  89, 144])

 

In [29]: b = arr3.flatten () # by an array method flatten straightened

In [30]: b

Out[30]: array([  1,   1,   2,   3,   5,   8,  13,  21,  34,  55,  89, 144])

The difference is that ravel method generates an array of original view , no memory space occupied, but the change will affect the view of the change of the original array . The flatten method returns a true value , change the value of the change does not affect the original array.

By the following examples may be able to understand:

In [31]: b[:3] = 0

In [32]: arr3

Out[32]:

array([[  1,   1,   2,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144]])

By changing the value of b, there is no change in the original array.

 

In [33]: a[:3] = 0

In [34]: arr3

Out[34]:

array([[  0,   0,   0,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144]])

After a change in value, it will lead to follow changes in the original array.

 

 

In [35]: arr4

Out[35]:

array([[ 1,  2,  3,  4],

[ 5,  6,  7,  8],

[ 9, 10, 11, 12]])

 

In [36]: arr4.ndim # Returns the number of dimensions of the array

Out[36]: 2

 

In [37]: arr4.size # Returns the number of array elements

Out[37]: 12

 

In [38]: arr4.T # results returned array transpose

Out[38]:

array([[ 1,  5,  9],

[ 2,  6, 10],

[ 3,  7, 11],

[ 4,  8, 12]])

If the data type is an array of a plurality of words , real method returns the real part of the complex, imag method returns the imaginary part of the complex.

 

After introduce an array of methods, then we look at the array itself has what functions operational:

In [39]: len (arr4) # Returns an array of the number of rows

Out[39]: 3

 

In [40]: arr3

Out[40]:

array([[  0,   0,   0,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144]])

 

In [41]: arr4

Out[41]:

array([[ 1,  2,  3,  4],

[ 5,  6,  7,  8],

[ 9, 10, 11, 12]])

 

In [42]: np.hstack((arr3,arr4))

Out[42]:

array([[  0,   0,   0,   3,   1,   2,   3,   4],

[  5,   8,  13,  21,   5,   6,   7,   8],

[ 34,  55,  89, 144,   9,  10,  11,  12]])

Transverse splicing same number of rows and arr4 arr3 two arrays, but must satisfy two arrays.

 

In [43]: np.vstack((arr3,arr4))  

Out[43]:

array([[  0,   0,   0,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144],

[  1,   2,   3,   4],

[  5,   6,   7,   8],

[  9,  10,  11,  12]])

Longitudinal splice arr3 arr4 and two arrays, but must meet the same number of columns of the two arrays.

 

In [44]: np.column_stack ((arr3, arr4)) # having the same function and effect hstack

Out[44]:

array([[  0,   0,   0,   3,   1,   2,   3,   4],

[  5,   8,  13,  21,   5,   6,   7,   8],

[ 34,  55,  89, 144,   9,  10,  11,  12]])

 

In [45]: np.row_stack ((arr3, arr4)) # having the same function and effect vstack

Out[45]:

array([[  0,   0,   0,   3],

[  5,   8,  13,  21],

[ 34,  55,  89, 144],

[  1,   2,   3,   4],

[  5,   6,   7,   8],

[  9,  10,  11,  12]])

 

the RESHAPE () function and a resize () function to re-set the number of columns and rows of the array:

 

In [46]: arr5 = np.array(np.arange(24))

In [47]: arr5 # This is a one-dimensional array

Out[47]:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23])

 

In [48]: a = arr5.reshape(4,6)  

In [49]: a

Out[49]:

array([[ 0,  1,  2,  3,  4,  5],

[ 6,  7,  8,  9, 10, 11],

[12, 13, 14, 15, 16, 17],

[18, 19, 20, 21, 22, 23]])

By reshape function of the one-dimensional array to a two-dimensional array, is an array of 4 rows and 6 columns.

 

In [50]: a.resize(6,4)  

In [51]: a

Out[51]:

array([[ 0,  1,  2,  3],

[ 4,  5,  6,  7],

[ 8,  9, 10, 11],

[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]])

By resize function will directly change the shape of the original array.

 

Array conversion: ToList array into a list, asType () array data cast type , the following are examples of two functions:

 

In [53]: b = a.tolist()

In [54]: b

Out[54]:

[[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]]

In [55]: type(b)

Out[55]: list

 

In [56]: c = a.astype(float)

In [57]: c

Out[57]:

array([[  0.,   1.,   2.,   3.],

[  4.,   5.,   6.,   7.],

[  8.,   9.,  10.,  11.],

[ 12.,  13.,  14.,  15.],

[ 16.,  17.,  18.,  19.],

[ 20.,  21.,  22.,  23.]])

 

In [58]: a.dtype

Out[58]: dtype('int32')

In [59]: c.dtype

Out[59]: dtype('float64')

 

 

Gets an array of elements

Gets the array element by indexing and slicing the way, one-dimensional array of elements to obtain a list, tuple way to obtain the same:

In [60]: arr7 = np.array(np.arange(10))

In [61]: ARR7

Out[61]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [62]: arr7 [3] # obtain the fourth element

Out[62]: 3

 

In [63]: arr7 [: 3] # get the first 3 elements

Out[63]: array([0, 1, 2])

 

In [64]: arr7 [3:] # get all the elements after the first four elements i.e.

Out[64]: array([3, 4, 5, 6, 7, 8, 9])

 

In [65]: arr7 [-2:] # 2 acquired at the end of element

Out[65]: array([8, 9])

 

In [66]: arr7 [:: 2] # 1 starting from the first element, all the elements in steps of obtaining 2

Out[66]: array([0, 2, 4, 6, 8])

 

Acquiring two-dimensional array of elements:

In [67]: arr8 = np.array(np.arange(12)).reshape(3,4)

In [68]: arr8

Out[68]:

array([[ 0,  1,  2,  3],

[ 4,  5,  6,  7],

[ 8,  9, 10, 11]])

 

In [69]: arr8 [1] # Returns the row of the array 2

Out[69]: array([4, 5, 6, 7])

 

In [70]: arr8 [: 2] # 2 returns an array of rows before

Out[70]:

array([[0, 1, 2, 3],

[4, 5, 6, 7]])

 

In [71]: arr8 [[0,2]] # Returns the specified row 1 and row 3

Out[71]:

array([[ 0,  1,  2,  3],

[ 8,  9, 10, 11]])

 

In [72]: arr8 [:, 0] # 1 returns an array of first column

Out[72]: array([0, 4, 8])

 

After returning the two array #: - In [73]:: arr8 [2,]

Out[73]:

array([[ 2,  3],

[ 6,  7],

[10, 11]])

 

In [74]: arr8 [:, [0,2]] # Returns an array of the first and third column

Out[74]:

array([[ 0,  2],

[ 4,  6],

[ 8, 10]])

 

In [75]: arr8 [1,2] # Returns an array of three rows corresponding to the second element

Out[75]: 6

 

 

Boolean index , that index values are True and False, to note that the index must enter a Boolean array of objects.

In [76]: log = np.array([True,False,False,True,True,False])

In [77]: arr9 = np.array(np.arange(24)).reshape(6,4)

In [78]: arr9

Out[78]:

array([[ 0,  1,  2,  3],

[ 4,  5,  6,  7],

[ 8,  9, 10, 11],

[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]])

 

In [79]: arr9 [log] # Returns all corresponding rows to True

Out[79]:

array([[ 0,  1,  2,  3],

[12, 13, 14, 15],

[16, 17, 18, 19]])

 

In [80]: arr9 [-log] # filter out all the row corresponding to False neg

Out[80]:

array([[ 4,  5,  6,  7],

[ 8,  9, 10, 11],

[20, 21, 22, 23]])

 

For one scene, a one-dimensional array represents a region, a two-dimensional array representation observations, how to select the observation target area?

In [81]: area = np.array(['A','B','A','C','A','B','D'])

In [82]: area

Out[82]:

array(['A', 'B', 'A', 'C', 'A', 'B', 'D'],

dtype='<U1')

 

In [83]: observes = np.array(np.arange(21)).reshape(7,3)

In [84]: observes

Out[84]:

array([[ 0,  1,  2],

[ 3,  4,  5],

[ 6,  7,  8],

[ 9, 10, 11],

[12, 13, 14],

[15, 16, 17],

[18, 19, 20]])

 

In [85]: observes[area == 'A']

Out[85]:

array([[ 0,  1,  2],

[ 6,  7,  8],

[12, 13, 14]])

A return to observe all areas.

 

In [86]: observes [(area == 'A') | (area == 'D')] # condition value needed & (and), | (or) ends with parentheses

Out[86]:

array([[ 0,  1,  2],

[ 6,  7,  8],

[12, 13, 14],

[18, 19, 20]])

Returns all observation regions A and D regions.

 

Of course, the Boolean index can also be mixed with ordinary index or slice:

In [87]: observes[area == 'A'][:,[0,2]]  

Out[87]:

array([[ 0,  2],

[ 6,  8],

[12, 14]])

A return all rows of the regions, and acquire only the first column and the third column data.

 

Fancy index : in fact, the array element of the array as the index of the original extract

In [88]: arr10 = np.arange(1,29).reshape(7,4)

In [89]: arr10

Out[89]:

array([[ 1,  2,  3,  4],

[ 5,  6,  7,  8],

[ 9, 10, 11, 12],

[13, 14, 15, 16],

[17, 18, 19, 20],

[21, 22, 23, 24],

[25, 26, 27, 28]])

 

In [90]: arr10 [[4,1,3,5]] # Returns the specified row in the specified order

Out[90]:

array([[17, 18, 19, 20],

[ 5,  6,  7,  8],

[13, 14, 15, 16],

[21, 22, 23, 24]])

 

In [91]: arr10 [[4,1,5]] [:, [0,2,3]] # Returns the specified row and column

Out[91]:

array([[17, 19, 20],

[ 5,  7,  8],

[21, 23, 24]])

 

In [92]: arr10[[4,1,5],[0,2,3]]

Out[92]: array([17,  7, 24])

 Please note! This is the result of the above distinct return, the return of the above is a two-dimensional array, this command returns a one-dimensional array.

 

If you want to use a relatively simple way to return the specified row in a column of two-dimensional array, you can use ix_ () function

In [93]: arr10[np.ix_([4,1,5],[0,2,3])]

Out[93]:

array([[17, 19, 20],

[ 5,  7,  8],

[21, 23, 24]])

This arr10 [[4,1,5]] and [:, [0,2]] returns the result is the same.

Statistical functions and linear algebra operations

Statistical operation common aggregate functions are: minimum, maximum, median, mean, variance, standard deviation. First, let's take a look at the array element level of computing :

In [94]: arr11 = 5-np.arange(1,13).reshape(4,3)

In [95]: arr12 = np.random.randint(1,10,size = 12).reshape(4,3)

In [96]: arr11

Out[96]:

array([[ 4,  3,  2],

[ 1,  0, -1],

[-2, -3, -4],

[-5, -6, -7]])

 

In [97]: arr12

Out[97]:

array([[1, 3, 7],

[7, 3, 7],

[3, 7, 4],

[6, 1, 2]])

 

In [98]: arr11 ** 2 # calculates the square of each element

Out[98]:

array([[16,  9,  4],

[ 1,  0,  1],

[ 4,  9, 16],

[25, 36, 49]])

 

In [99]: np.sqrt (arr11) # calculating the square root of each element

Out[99]:

array([[ 2.        ,  1.73205081,  1.41421356],

[1., 0.,] and

[In, in, in],

[In, in, in]])

Due to the negative square root does not make sense, it returns NaN .

 

In [100]: np.exp (arr11) # calculated index value of each element

Out[100]:

array([[  5.45981500e+01,   2.00855369e+01,   7.38905610e+00],

[  2.71828183e+00,   1.00000000e+00,   3.67879441e-01],

[1.35335283e-01, 4.97870684e-02, 1.83156389e-02],

[  6.73794700e-03,   2.47875218e-03,   9.11881966e-04]])

 

In [101]: np.log (arr12) # is calculated for each element of the natural logarithm

Out[101]:

array([[ 0.        ,  1.09861229,  1.94591015],

[ 1.94591015,  1.09861229,  1.94591015],

[ 1.09861229,  1.94591015,  1.38629436],

[ 1.79175947,  0.        ,  0.69314718]])

 

In [102]: np.abs (arr11) # calculates the absolute value of each element

Out[102]:

array([[4, 3, 2],

[1, 0, 1],

[2, 3, 4],

[5, 6, 7]])

 

The same shape as the array elements between the operation of:

In [103]: arr11 + arr12 # plus

Out[103]:

array([[ 5,  6,  9],

[ 8,  3,  6],

[ 1,  4,  0],

[ 1, -5, -5]])

 

In [104]: arr11 - arr12 # Save

Out[104]:

array([[  3,   0,  -5],

[ -6,  -3,  -8],

[ -5, -10,  -8],

[-11,  -7,  -9]])

 

In [105]: arr11 * arr12 # multiplication

Out[105]:

array([[  4,   9,  14],

[  7,   0,  -7],

[ -6, -21, -16],

[-30,  -6, -14]])

 

In [106]: arr11 / arr12 # inter

Out[106]:

array([[ 4.        ,  1.        ,  0.28571429],

[ 0.14285714,  0.        , -0.14285714],

[-0.66666667, -0.42857143, -1.        ],

[-0.83333333, -6.        , -3.5       ]])

 

In [107]: arr11 // arr12 # divisible

Out[107]:

array([[ 4,  1,  0],

[ 0,  0, -1],

[-1, -1, -1],

[-1, -6, -4]], dtype=int32)

In [108]: arr11% arr12 # Remainder

Out[108]:

array([[0, 0, 2],

[1, 0, 6],

[1, 4, 0],

[1, 0, 1]], dtype=int32)

 

Next we look at the statistical operation function:

In [109]: np.sum (arr11) # calculate all elements and

Out[109]: -18

 

In [110]: np.sum (arr11, axis = 0) # of each column summation

Out[110]: array([ -2,  -6, -10])

 

In [111]: np.sum (arr11, axis = 1) # summation for each row

Out[111]: array([  9,   0,  -9, -18])

 

In [112]: np.cumsum (arr11) # demand and accumulated for each element (top to bottom, left to right order of elements)

Out[112]: array([  4,   7,   9,  10,  10,   9,   7,   4,   0,  -5, -11, -18], dtype=int32)

 

In [113]: np.cumsum (arr11, axis = 0) # calculates the cumulative and each column, and returns the two-dimensional array

Out[113]:

array([[  4,   3,   2],

[  5,   3,   1],

[  3,   0,  -3],

[ -2,  -6, -10]], dtype=int32)

 

In [114]: np.cumprod (arr11, axis = 1) # calculates the cumulative product of each row, and returns the two-dimensional array

Out[114]:

array([[   4,   12,   24],

[   1,    0,    0],

[  -2,    6,  -24],

[  -5,   30, -210]], dtype=int32)

 

In [115]: np.min (arr11) # calculates a minimum value of all elements

Out[115]: -7

 

In [116]: np.max (arr11, axis = 0) # of calculating the maximum value of each column

Out[116]: array([4, 3, 2])

 

In [117]: np.mean (arr11) # calculate the mean of all the elements

Out[117]: -1.5

 

Mean np.mean (arr11, axis = 1) # is calculated for each row: In [118]

Out[118]: array([ 3.,  0., -3., -6.])

 

In [119]: np.median (arr11) # calculates the median of all the elements

Out[119]: -1.5

 

In [120]: np.median (arr11, axis = 0) # median calculation for each column

Out[120]: array([-0.5, -1.5, -2.5])

 

In [121]: np.var (arr12) # calculating the variance of all the elements

Out[121]: 5.354166666666667

 

In [122]: np.std (arr12, axis = 1) # standard deviation calculated for each row

Out[122]: array([ 2.49443826,  1.88561808,  1.69967317,  2.1602469 ])

 

Statistical functions in operation numpy is very flexible, either calculate statistical values of all elements can also be calculated statistical indicators specified row or column. There are other common functions , such as the sign function sign, ceil (> = the smallest integer x), floor (<= largest integer x), MODF (the integer part and fractional part of the floating point number are stored in two separate array), cos, arccos, sin, arcsin, tan, arctan like.

 

I am very excited to be a function of the WHERE () , which is similar to the Excel function if, for flexible transformation:

 

In [123]: arr11

Out[123]:

array([[ 4,  3,  2],

[ 1,  0, -1],

[-2, -3, -4],

[-5, -6, -7]])

 

In [124]: np.where(arr11 < 0, 'negtive','positive')

Out[124]:

array([['positive', 'positive', 'positive'],

['positive', 'positive', 'negtive'],

['negtive', 'negtive', 'negtive'],

['negtive', 'negtive', 'negtive']],

dtype='<U8')

Of course, it np.where also be nested , complex algorithms.

 

 

Other functions

UNIQUE (x) : the only element of x, and returns the results in order

INTERSECT (x, y) : x and y is calculated public element, i.e., the intersection

union1d (x, y) : x and y is calculated and set

setdiff1d (x, y) : x and difference calculation of y, i.e., the elements in x, y is not

setxor1d (X, Y) : a set of symmetric difference calculation, i.e., in the presence of an array, but does not exist in both arrays

in1d (x, y) : x is determined whether the element contained in y,

 

 

Linear algebra

Similarly numpu languages also with R, can easily be calculated linear algebra, such as determinant, reverse, track, characterized in roots, feature vectors and the like. Note, however that is not a function related to linear algebra in numpy, but rather the sub-linalg numpy embodiment of the.

In [125]: arr13 = np.array([[1,2,3,5],[2,4,1,6],[1,1,4,3],[2,5,4,1]])

In [126]: arr13

Out[126]:

array([[1, 2, 3, 5],

[2, 4, 1, 6],

[1, 1, 4, 3],

[2, 5, 4, 1]])

 

In [127]: np.linalg.det (arr13) # Returns the matrix determinant

Out[127]: 51.000000000000021

 

In [128]: np.linalg.inv (arr13) # Returns the inverse square

Out[128]:

array([[-2.23529412,  1.05882353,  1.70588235, -0.29411765],

[ 0.68627451, -0.25490196, -0.7254902 ,  0.2745098 ],

[ 0.19607843, -0.21568627,  0.07843137,  0.07843137],

[ 0.25490196,  0.01960784, -0.09803922, -0.09803922]])

 

In [129]: np.trace (arr13 ) # Returns the square traces (sum of diagonal elements), attention is not solved trace subroutine linalg

Out[129]: 10

 

In [130]: np.linalg.eig (arr13) # returned tuple features and eigenvectors composition

Out[130]:

(array([ 11.35035004,  -3.99231852,  -0.3732631 ,   3.01523159]),

array([[-0.4754174 , -0.48095078, -0.95004728,  0.19967185],

[-0.60676806, -0.42159999,  0.28426325, -0.67482638],

[-0.36135292, -0.16859677,  0.08708826,  0.70663129],

[-0.52462832,  0.75000995,  0.09497472, -0.07357122]]))

 

In [131]: np.linalg.qr (arr13) # Returns the QR decomposition of matrix

Out[131]:

(array([[-0.31622777, -0.07254763, -0.35574573, -0.87645982],

[-0.63245553, -0.14509525,  0.75789308, -0.06741999],

[-0.31622777, -0.79802388, -0.38668014,  0.33709993],

[-0.63245553,  0.580381  , -0.38668014,  0.33709993]]),

array([[-3.16227766, -6.64078309, -5.37587202, -6.95701085],

[ 0.        ,  1.37840488, -1.23330963, -3.04700025],

[ 0.        ,  0.        , -3.40278524,  1.22190924],

[ 0.        ,  0.        ,  0.        , -3.4384193 ]]))

 

In [132]: np.linalg.svd (arr13) # Returns the singular value decomposition of a square matrix

Out[132]:

(array([[-0.50908395,  0.27580803,  0.35260559, -0.73514132],

[-0.59475561,  0.4936665 , -0.53555663,  0.34020325],

[-0.39377551, -0.10084917,  0.70979004,  0.57529852],

[-0.48170545, -0.81856751, -0.29162732, -0.11340459]]),

array([ 11.82715609,   4.35052602,   3.17710166,   0.31197297]),

array([[-0.25836994, -0.52417446, -0.47551003, -0.65755329],

[-0.10914615, -0.38326507, -0.54167613,  0.74012294],

[-0.18632462, -0.68784764,  0.69085326,  0.12194478],

[ 0.94160248, -0.32436807, -0.05655931, -0.07050652]]))

 

In [133]: np.dot (arr13, arr13) # square matrix multiplication operation is really

Out[133]:

array([[18, 38, 37, 31],

[23, 51, 38, 43],

[13, 25, 32, 26],

[18, 33, 31, 53]])

 

In [134]:arr14 = np.array([[1,-2,1],[0,2,-8],[-4,5,9]])

In [135]: vector = np.array([0,8,-9])

In [136]: np.linalg.solve(arr14,vector)

Out[136]: array([ 29.,  16.,   3.])

 

 

Random Number Generator

Statistics often mentioned distribution data, such as normal, exponential, chi-square distribution, binomial, Poisson distribution, the following talk about the distribution of the random number generation.

Normal distribution histogram

In [137]: import matplotlib # drawing means for

In [138]: np.random.seed (1234) # Set random seed

In [139]: N = 10000 # randomly generated amount of sample

In [140]: randnorm = np.random.normal (size = N) # generates a normal random number

In [141]: counts, bins, path = matplotlib.pylab.hist (randnorm, bins = np.sqrt (N), normed = True, color = 'blue') # histogrammed

Group and the above frequency from the histogram counts and stored in bins.

 

And [142]: sigma = 1; a = 0

In [143]: norm_dist = (1 / np.sqrt (2 * sigma * np.pi)) * np.exp (- ((bins-mu) ** 2) / 2) # normal density function

In [144]: matplotlib.pylab.plot (bins, norm_dist, color = 'red') # normal density function plotted in FIG.



Using the binomial distribution gambling

Meanwhile abandoned nine coins, if less than five face-up, then lost $ 8, or win $ 8. If the hands of a 1000 yuan as gambling money, gambling ask after 10,000 times might be what happens then?

In [146]: np.random.seed(1234)

In [147]: binomial = np.random.binomial (9,0.5,10000) # binomial random number generated

In [148]: money = np.zeros (10000) # 10000 to generate a list of gambling

In [149]: money [0] = 1000 # 1,000 yuan first gambling

In [150]: for i in range(1,10000):

     ...:     if binomial[i] < 5:

     ...:         money[i] = money[i-1] - 8  

# If less than 5 positive, then based on the gambling money on a lost 8 yuan

     ...:     else:

     ...:         money[i] = money[i-1] + 8  

# If at least five positive, then on the basis of a gambling money on to win eight yuan

In [151]: matplotlib.pylab.plot(np.arange(10000), money)



Use random integer implement random walk

After walking 10,000 steps a drunk in the original position will be in place? If he walked every step is random, that is, the next step may be 1 or may be -1.

In [152]: np.random.seed (1234) # random seed set

In [153]: position = 0 # sets the initial position

In [154]: walk = [] # Create an empty list

In [155]: steps = 10000 # 10000 assuming the next walking step

In [156]: for i in np.arange(steps):

     ...: step = every step 1 if np.random.randint (0,2) else -1 # are random

     ...: position = position + step # cumulative sum for each step

     ...: walk.append (position) # determine the location where each step

In [157]: matplotlib.pylab.plot (np.arange (10000), walk) # draw random walk FIG.



The above code can also be written as (where stated before binding function, cumsum function):

In [158]: np.random.seed(1234)

In [159]: step = np.where(np.random.randint(0,2,10000)>0,1,-1)

In [160]: position = np.cumsum(step)

In [161]: matplotlib.pylab.plot(np.arange(10000), position)

 


Avoid the for loop, you can achieve the same effect.

Reprinted from: https: //www.cnblogs.com/nxld/p/6058572.html

Guess you like

Origin www.cnblogs.com/bighammerdata/p/11684926.html