Numpy library 03_ data operations

# Listing import numpy np # calculation as multiplication # ll = [1,2,3,4,5] # ll2 = ll * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] # print (ll2) # adding listing # ll3 = [1,2,3,4,5] # ll4 = ll3 + 10 # given, we can not write # print (ll4) #numpy addition array # nparr1 = np.arange (0,10) # print (nparr1) # [0 1 2 3 4 5 6 7 8 9] # print (nparr1 + 10) # [10 11 12 13 14 15 16 17 18 19] each element plus, you can add # print (nparr1 * 10) # [0 10 20 30 40 50 60 70 80 90] by each element can take # nparr2 = np.arange (0,10) .reshape (2,5) # print (nparr2) # print (nparr2 * 10) # print (nparr2 + 10) # can be above, its operations are carried directly into operation on each element of the generic function np example # # nparr3 = np.arange (- 10,3) # print (nparr3) # print (np.abs (nparr3)) # without changing the original data, # print (nparr3) # logical operation of the array, which to use three arrays # nparr4 = np.array ( [1,2,3,4,5]) # nparr5 = np.array ([6,7,8,9,10]) # nparr6 = np.array ([True, True, False, False, True]) # nparr7 = [(x if z else y) for x, y, z in zip (nparr4, nparr5, nparr6)] # print (nparr7) # [1, 2, 8, 9, 5] According to the type of changes to the list. # Print (type (nparr7)) # #np. where () function to achieve the above operation # nparr8 = np.where (nparr6, nparr4, nparr5) # print (nparr8) # [1 2 8 9 5] #where () function other uses, condition selection function # nparr9 = np.random.randn (5,5) # print (nparr9) # print ( "______________________") # nparr10 = np.where (nparr9> 0,1, -1) # which is the condition, the condition of the front straight, becomes 1, is false becomes -1, without changing the original data # print (nparr10) # print ( "______________________") # print (nparr9) # to continue to dig where the usage of multi-criteria selection, namely np.where nesting use # nparr11 = np.random.randint (10,40, size = (4,4)) # print (nparr11) # # claim greater than the number of standard 10 2, 20 3 is greater than the standard number, greater than the number of standard 30 4 # nparr12 = np.where (nparr11> = 30,4, np.where (nparr11> = 20,3,2)) # print (nparr12) # statistical operation nparr13 = np.array ([[1,2,3 , 4], [5,6,7,8], [9,10,11,12]]) # print (nparr13) # print (nparr13.sum ()) # find all elements # print (nparr13. sum (axis = 0)) #y # print array axis and consisting of (nparr13.mean ()) # # print averaging all elements (nparr13.mean (axis = 1)) # 0 = y-axis average, 1 = x-axis Average # print (nparr13.std ()) # # What is the standard deviation standard deviation "" " Standard deviation (Standard Deviation), Chinese environment and variance are often called, is a deviation from the mean square root of the arithmetic mean, expressed as σ. The standard deviation is the square root of the variance of the arithmetic. Standard deviation reflects the degree of dispersion of a data set. Mean same two sets of data, not necessarily the same standard deviation. "" "# Nparr14 = np.array ([[1,2,3,4], # [5,6,7,8], # [9,10,11,12]]) # print (nparr14.max (axis = 1)) # maximum max (), the minimum min (), can be added to the shaft axis, axis may be omitted # # print (nparr14.argmax ()) # maximum index # print (nparr14.cumsum ()) # [1,361,015,212,836,455,566 78] accumulated and, interestingly, the addition to obtain a next first few number # print (nparr14.cumsum (0)) #axis may be omitted, directly to # 0 or 1 print (nparr14.cumprod ()) # cumulative plot, and the same with the above cumulative sort # # nparr15 = np.array ([[1,8,3,4], # [5,6,7,4], # [ 9,1,11,2]]) # print (nparr15) # # nparr15.sort () # sorting, the original data is changed, the multi-dimensional x-axis default sort # nparr15.sort (0) # sorting, the original data is changed, multidimensional default sort x-axis, y-axis table row 0 # print (nparr15) # # set operation set operation of the one-dimensional array, provides a unique, i.e. to recalculation unique # nparr16 = np.array ([1,2,3 , 2,4,6,18,6,22,15,15,18]) # print (nparr16) # nparr17 = np.unique (nparr16) # without changing the original data, de-duplication, but also from small to large lined up sequence # print (nparr16) # print (nparr17) # nparr19 = np.array ([[1,8 , 3,4], # [5,6,7,4], # [9,1,11,2]]) # nparr20 = np.unique (nparr19) # multi-dimensional, then turned into a one-dimensional, but also to heavy. [1 2 3 4 5 6 7 8 9 11] # print (nparr20) "" "# set operations: unique () unique values, to re intersectld () public element unionld () and set inld () is present, return bool value setdiffld () difference setxorld collection () intersection negated "" "# linear Algebra: buy a book to learn

Guess you like

Origin www.cnblogs.com/yiyea/p/11441755.html