Home  >  Article  >  Backend Development  >  Tips and tricks for viewing numpy versions

Tips and tricks for viewing numpy versions

王林
王林Original
2024-01-19 10:53:051155browse

Tips and tricks for viewing numpy versions

numpy is a very commonly used mathematics library in Python. It is widely used in the field of scientific computing and supports a large number of numerical calculations, linear algebra, random number generation, Fourier transform and other functions. When using numpy for mathematical calculations, it is often necessary to determine the numpy version and its characteristics, and make different optimization and algorithm selections for different versions of numpy. This article will introduce tips and tricks for checking numpy version, and how to better use numpy by detecting numpy version information.

1. How to view the numpy version

There are many built-in functions and properties in numpy that can be used to obtain numpy version information. The following will introduce several commonly used methods to check the numpy version.

  1. Use numpy.version attribute

There is a version attribute in numpy, which can be used to obtain detailed information of the current numpy version, including version number and Git commit hash value , compiler information, etc. The code example is as follows:

import numpy as np
print(np.version.version)

The output result is as follows:

1.20.1
  1. Use numpy.__version__ attribute

In addition to the version attribute, numpy also A __version__ attribute is provided, whose default value is a string representation of the current numpy version. This attribute is also one of the common ways to determine version information in numpy. The code example is as follows:

import numpy as np
print(np.__version__)

The output result is the same as the previous example:

1.20.1
  1. Use numpy.show_config function

If you need to view more detailed numpy compilation and build information, you can use the numpy.show_config function. This function will display the various compilers, linkers, and libraries used by numpy when building, including the C compiler, CBLAS library, LAPACK library, etc. Its code example is as follows:

import numpy as np
np.show_config()

The output result is as follows:

blas_mkl_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']

blis_info:
    NOT AVAILABLE

openblas_info:
    NOT AVAILABLE

lapack_mkl_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']

lapack_opt_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']

lapack_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']

mkl_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']

blas_opt_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/lib/intel64']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:/Program Files (x86)/Intel/oneAPI/mkl/2021.1.1/include']
...(输出结果省略)

Through the above three methods, you can check the specific version and compilation information of numpy, and find out whether the version of numpy is suitable for different applications. The numpy version corresponding to the project, as well as the selection of appropriate numpy algorithms and methods, are of great significance.

2. Application of numpy version information

After clarifying the numpy version information, when using numpy, you can select appropriate algorithms and methods for different versions to achieve the optimal Optimization effect and performance improvement. For example, in numpy versions 1.20 and above, higher-level functions can be used to automatically handle NaN values ​​to avoid exceptions when the program is running. At the same time, some efficient optimization algorithms are used, and the performance has also been greatly improved. In lower versions of numpy, you may need to manually handle NaN values ​​and exceptions, and use some simple algorithms to improve the stability and performance of the program.

The following is a simple example showing how to use numpy version information to select the optimal algorithm.

Suppose we need to calculate the product of a 10000×10000 matrix. We can calculate this task in two ways. One method is to use the numpy.dot() function, which calculates the dot product of two matrices by calling the dgemm subroutine in the BLAS library. It also supports multi-threading and vectorization calculations, and the calculation speed is very fast. Another method is to use the numpy.multiply() function to multiply the two matrices element by element, and then sum the results to obtain the dot product. The implementation of this method is relatively simple, but the performance is poor.

The following code compares the calculation time of the two algorithms:

import numpy as np
import time

A = np.random.rand(10000, 10000)
B = np.random.rand(10000, 10000)

# 方法1:使用numpy.dot函数
start_time = time.time()
C = np.dot(A, B)
end_time = time.time()
print("方法1计算时间:", end_time - start_time)

# 方法2:使用numpy.multiply函数
start_time = time.time()
C = np.multiply(A, B).sum()
end_time = time.time()
print("方法2计算时间:", end_time - start_time)

The output results are as follows:

方法1计算时间: 3.94059681892395
方法2计算时间: 9.166156768798828

As you can see, the calculation speed of using numpy.dot() It is almost 2.5 times faster than using numpy.multiply(). From this, it can be concluded that when the numpy version is compatible, the numpy.dot() algorithm should be preferred for better performance and shorter calculation time.

Conclusion

This article introduces several methods for viewing numpy versions, and also introduces the application of different algorithms and methods for different numpy versions. In actual numpy development, it is very necessary to understand the characteristics and performance of the numpy version and master the numpy version viewing skills, which can lay a solid foundation for better numpy application and development.

The above is the detailed content of Tips and tricks for viewing numpy versions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn