Numpy library is introduced
NumPy is a powerful Python library for performing computations on multidimensional arrays. The word NumPy comes from two words, Numerical and Python. NumPy provides a large number of library functions and operations to help programmers easily do numerical calculations. It is widely used in data analysis and machine learning. He has the following characteristics:
- Numpy has built-in parallel computing capability. When a system has multiple cores, numpy will automatically perform parallel computation.
- Numpy is written in C with an internal unlocked global interpreter (GIL). Numpy can operate on arrays faster than pure Python code.
- There is a powerful N-dimensional Array object Array (something like a list).
- Practical linear algebra, Fourier transform and random number generation functions.
All in all, it is a very efficient package for handling numerical operations.
Installation:
PIP install numpy can be used to install numpy.
Tutorial Address:
- Website:
https://docs.scipy.org/doc/numpy/user/quickstart.html
. - Chinese document:
https://www.numpy.org.cn/user_guide/quickstart_tutorial/index.html
.
Numpy arrays vs. Python lists:
Let’s say we want to square each element in a Numpy array and Python list. The code is as follows:
# Python lists
t1 = time.time()
a = []
for x in range(100000):
a.append(x**2)
t2 = time.time()
t = t2 - t1
print(t)
Copy the code
The time taken is about 0.07180. Using numpy’s array is much faster:
t3 = time.time()
b = np.arange(100000) * *2
t4 = time.time()
print(t4-t3)
Copy the code