Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the common operation of Numpy library in python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what is the common operation of Numpy library in python, the content is very detailed, interested friends can refer to it, hope to be helpful to you.

Introduction of the numpy library:

Import numpy as np

1. Query the basic attributes of numpy objects

Lst = [[1, 2, 3], [4, 5, 6]] def numpy_type (): print (type (lst)) data = np.array (lst, dtype=np.float64) # array converts array to numpy array # bool,int,int8,int16,int32,int64,int128,uint8,uint32, # uint64,uint128,float16/32/64 Complex64/128 print (type (data)) # data type print (data.shape) # rows and columns print (data.ndim) # space dimension print (data.dtype) # element type print (data.itemsize) # bytes occupied by elements print (data.size) # elements

2. Common operations of arrays of numpy

Def numpy_array (): # output 2 rows and 4 columns of all 0 numpy array array print (np.zeros ([2,4])) # generally used for data initialization # output 3 rows and 5 columns of all 1 numpy array array print (np.ones ([3,5])) # Random number print (np.random.rand (2,4)) # generate 2 rows and 4 columns of random number matrix In this case, the default element size is 0 to 1 print (np.random.rand ()) # prints a random number, and the default element size is 0 to 1 print (np.random.rand (1Lo 10, 3)) # three parameters correspond to the x, y and z axes, respectively, representing the one-dimensional array print (np.random.randint (1) of three elements within the representation range of three different randint parameters of the three-dimensional matrix # and rand. 10, 3) print (np.random.randint (1,10)) # randint must pass a parameter range This means that print (np.random.randn ()) # returns a random number of standard normal distribution print (np.random.randn (2,4)) # returns two rows and four columns of random numbers print (np.random.choice ([10,2,3,1,5,6]) # randomly returns a print (np.random.beta (1)) from an iterable array (10100)) # randomly generate an one-dimensional array lst1 = np.array ([10,20,30,40]) lst2 = np.array ([4,3,2]) that conforms to the 100 elements published by beta in [1Magne 10] ) # Direct operation print (lst2 + lst1) print (lst2-lst1) print (lst2 / lst1) print (lst2 * lst1) print (lst2 * * lst1) # Square multiplied by print (np.dot (lst1.reshape ([2,2]), lst2.reshape ([2,2])) # add print (np.concatenate ((lst2, lst1), axis=0)) # 0 level append 1 Vertical append print (np.vstack ((lst2, lst1) # Vertical append print (np.hstack ((lst2, lst1) # horizontal append # split print (np.split (lst2, 2)) # split into 2 # copies of print (np.copy (lst2))

3. Common data operation methods of numpy

Def numpy_handle (): print (np.arange (1jue 11)) # generates an one-dimensional array of integers in [1jue 11). The default is to increment print by 1 (np.arange (1jue 11). Reshape ([2,5])) # reshape method can reconstruct the matrix into matrix print (np.arange (1j11). Reshape ([2)) with x rows and y columns (- 1])) # you can also use the missing value-1 to achieve the same effect data = np.arange (1,11). Reshape ([2 -1]) print (np.exp (data)) # exponential operation print (np.exp2 (data)) # squared operation print (np.sqrt (data)) # square operation print (np.sin (data)) # trigonometric function print (np.log (data)) # logarithmic operation print (data.max () Maximum print (data.min ()) # minimum

4. The understanding of axis in numpy

Def numpy_axis (): data = np.array ([1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16], [[17, 18, 19, 20], [20, 21, 22, 23]]) # axis infiltration from the outside in The higher the value, the more the number of layers infiltrated. The maximum is nMel 1 # axis=0, which means that 1 parses print (data.sum (axis=2)) # summation # where three layers of print (data.max (axis=1)) # get the maximum value print (data.min (axis=0)) # to get the minimum value

5. The calculation of linear algebra commonly used in numpy

Remember to introduce dependencies here:

From numpy.linalg import * # introduces the dependence of linear equations def numpy_line (): from numpy.linalg import * # introduces the dependence of linear equations print (np.eye (3)) # the unit matrix of class 3 lst3 = np.array ([[1,2], [3] 4]]) print (inv (lst3)) # inverse matrix print (lst3.transpose ()) # transpose matrix print (det (lst3)) # determinant print (eig (lst3)) # symptom values and sign vectors The first one represents the physical sign value, and the second represents the sign vector y = np.array ([[5.], [7.]]) Print (solve (lst3, y)) # to solve linear matrix equations what is the common operation of Numpy library in python is shared here, I hope the above content can be helpful to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report