Numpy study notes 01 - installation and

1. Introduction to Numpy

numpyAdvantages:

Simple code: arrays and matrices are used as operation objects, and a large number of mathematical functions are supported

Efficient performance: numpy's data storage is better than Python's native List, and most of numpy's codes are implemented in C language, with more efficient performance

Basic library: numpy is the basic library of various python data science libraries, such as scipy, tensorflow, scikit-learn, etc.

Second, the installation of numpy

1. Use anaconda to install, comes with numpy installation package,

2. If you are using the native installation of python, use the pip statement to install

pip install numpy

3. Verify whether numpy is installed

Enter the python command line, enter: import numpy as np, if no error is reported, the installation is successful.

3. Performance test

1. Comparison of code simplicity

#python原生实现,平方和立方依次相加
def python_sum(n):
    a = [i**2 for i in range(n)]
    b = [i**3 for i in range(n)]
    c = []
    for i in range(n):
        c.append(a[i]+b[i])
    return c

python_sum(10)


# 使用numpy实现
def numpy_sum(n):
    a = np.arange(n)**2
    b = np.arange(n)**3
    return a+b

numpy_sum(10)

2. Performance comparison

%timeit python_sum(10*10000)
#77.1 ms ± 1.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


%timeit numpy_sum(10*10000)
#546 µs ± 12.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Note: The above code is implemented in the jupyter notebook environment

The comparison shows that using numpy to operate array-like data is more concise and faster

Guess you like

Origin blog.csdn.net/weixin_47930147/article/details/121073855