Detailed explanation of Numpy scientific computing library

Numpy is an open source Python scientific computing library. It is the basic library of Python scientific computing library. Many other famous scientific computing libraries such as Pandas and Scikit-learn use some functions of Numpy library. 

1. Basic usage of Numpy

1. Numpy array object

A multidimensional array in Numpy is called an ndarray, which is the most common array object in Numpy. ndarray objects generally contain two parts:

  • ndarray data itself;
  • metadata describing the data;

Advantages of Numpy arrays:

  • Numpy arrays are usually composed of elements of the same kind, that is, the data items in the array are of the same type. This has the advantage that since the array elements are known to be of the same type, the size of the space required to store data can be quickly determined;
  • Numpy arrays can use vectorized operations to process the entire array, which is faster; while Python lists usually need to use loop statements to traverse the list, and the operating efficiency is relatively poor;
  • Numpy uses the optimized C API, and the operation speed is faster;

Regarding vectorization and scalarization operations, the difference can be seen by comparing the following reference examples.

Use python's list to perform loop traversal operations:

def pySum():
    a = list(range(10000))
    b = list(range(10000))
    c = []
    for i in range(len(a)):
        c.append(a[i]**2 + b[i]**2)

    return c
%timeit pySum()

Out[]:
10 loops, best of 3: 

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/130419069