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

How to use the allclose of Python NumPy

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to use the allclose of Python NumPy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Numpy array and operation

The extension library numpy is an important extension library for Python to support scientific computing, and it is one of the necessary extension libraries in the field of data analysis and scientific computing, such as scipy, pandas, sklearn and so on. It provides powerful N-dimensional arrays and their related operations, complex broadcast functions, C _ big C + and Fortran code integration tools, linear algebra, Fourier transform and random number generation and other functions.

Create an array

An array is a contiguous memory space used to store a number of data, with elements of the same type, such as floating-point numbers. Array operation is an important basis for learning data analysis and machine learning related algorithms. When we deal with the actual data, we always use a large number of array operations or matrix operations, some of which are read directly through files, some are generated according to the actual needs, and of course some data are collected in real time.

Import numpy as npnp.array ([1,2,3,4,5]) #-> array ([1,2,3,4,5]) np.array (range (5)) #-> array ([0,1,2,3,4]) np.array ([1,2,3], [4,5,6]) #-> array ([1,2,3, # [4,5,6]) np.arange (5) #-> array ([0,1,2,3]) 4]) np.arange (1, 10, 2) #-> array ([1, 3, 5, 7, 9]) np.linspace (0, 10, 11) #-> array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.]) np.linspace (0, 10, 11, endpoint=False) #-> array (0. , 0.90909091, 1.81818182, 2.72727273, 3.63636364 1.00000000e+000 # 4.54545455, 5.45454545, 6.36363636, 7.27272727, 8.18181818 # 9.09090909]) np.logspace (0100,10) #-> array ([1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469eZO3 # 2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250eZ077 # 7.74263683e+088, 1.00000000e+100]) np.logspace (1,6,5) Base=2) #-> array ([2., 4.75682846, 11.3137085, 26.90868529, 64. ]) np.zeros (3) #-> array ([0.,0.,0.]) np.ones (3) #-> array ([1.1,1.1.]) np.zeros ((3,3)) #-> array ([[0.,0.,0.], # [0.0.0.0.0.], # [0.0.0.0.0.]) np.ones ((3,3)) #-> array ([[1.1,1. 1.], # [1, 1, 1.], # [1, 1.]) np.identity (3) #-> array ([[1, 0, 0.], # [0, 1.], # [0, 0, 1.]) np.empty ((3, 3)) #-> array ([[1, 0, 0.], # [0, 1. 0.], # [0.10492407, 0.17699537, 1.]) np.hamming (20) #-> array ([0.08, 0.10492407, 0.17699537, 0.28840385, 0.42707668) # 0.5779865, 0.7247799, 0.85154952, 0.94455793, 0.9937262, # 0.9937262, 0.94455793, 0.85154952,0.7247799,0.5779865, # 0.42707668,0.28840385, 0.17699537, 0.10492407 0.08]) np.blackman (20) #-> array ([- 1.38777878e-17, 1.02226199e-02, 4.50685843e-02, 1.14390287eMurray 01 7.52034438e-01 # 9.03492728e-01, 9.88846031e-01, 9.88846031e-01, 9.03492728eMuy01 7.52034438e-01, 5.66665187e-01 3.82380768e-01, 2.26899356eWhile 01 1.14390287e-01 # 1.14390287e-01, 4.50685843e-02, 1.02226199e-02,-1.38777878e-17]) np.kaiser (12, 5) #-> array ([0.03671089, 0.16199525, 0.36683806, 0.61609304, 0.84458838 # 0.98167828, 0.98167828, 0.84458838, 0.61609304, 0.36683806 # 0.16199525,0.03671089]) np.random.randint 5) #-> array ([6, 43, 33, 11, 5]) np.random.randint (0,50, (3)) #-> array ([[45, 30, 17, 31, 25], # [11, 32, 47, 48, 5], # [22, 29, 3, 9, 28]) np.random.rand (10) #-> array ([0.53459796, 0.59163821, 0.11611952, 0.68199147, 0.03725451) # 0.57498382, 0.15140171, 0.33914725, 0.90706282, 0.68518446]) np.random.standard_normal (5) #-> array ([- 0.62939386,-0.16023864, 1.67463293,-0.44869975, 0.97008488]) np.random.standard_normal (size= (3,4,2)) #-> array ([[- 0.7924237,-2.02222271], # [- 0.7360387,-1.88832641] # [- 0.43188511,-0.40672139], # [2.03058394, 1.007505], # # [[0.35664297, 1.9308035], # [0.56456596,-1.02357394], # [1.45042549,-0.59816538], # [- 0.00659242, 0.15439743]], # [- 1.31088702,-0.167339] # [0.44439704, 0.00819551], # [- 2.39637084,-0.07890167], # [0.53474018, 1.18425122]]) np.diag ([1,2,3,4]) #-> array ([[1,0,0,0], # [0,2,0,0], # [0,0,3,0], # [0,0,0,4])

The NumPy function allclose () is used to match two arrays with a Boolean output, which is within the error range of 1e-05 by default. Returns False if the items in the two arrays are not equal within tolerance. This is a good way to check whether each element of the two arrays is similar.

Allclose () numpy.allclose (a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)

Where the parameters are:

An is an array of numpy

B is the numpy array

Rtol is the relative maximum allowable error coefficient. If the default value is 1.0e-5, the allowable error is rtol * abs (b).

Atol is the difference between the sum of arrays an and b to compare the absolute values.

Whether equal_nan treats the missing values as the same. The default is False.

Here is a specific example:

Enter:

Array1 = np.array ([0.12, 0.17, 0.24, 0.29) array2 = np.array (0.13, 0.19, 0.26, 0.31)

Enter:

Np.allclose (array1,array2,0.1)

Output:

False

The reason is rtol=0.1, so (0.19-0.17) > (0.19 * 0.1)

Change from 0.17 to 0.18 in array1

Enter:

Array3 = np.array ([0.12, 0.18, 0.24, 0.29])

Enter:

Np.allclose (array3,array2,0.1)

Output:

True

This is the end of the content of "how to use the allclose of Python NumPy". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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