English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python NumPy Tutorial

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides multidimensional array objects, various derived objects (such as masked arrays and matrices), and various APIs for fast array operations, including mathematical, logical, shape operations, sorting, selection, input/output, discrete Fourier transform, basic linear algebra, basic statistical operations, and random simulation, etc.

The predecessor of NumPy, Numeric, was originally developed by Jim Hugunin and other collaborators.2005 In the year, Travis Oliphant combined the features of another similar program library Numarray in Numeric and developed NumPy with other extensions. NumPy is open source and is maintained and developed by many collaborators.

The core of the NumPy package is the ndarray object. It encapsulates n-dimensional arrays of the same data type as the native Python, and many operations are locally compiled and executed to ensure its excellent performance.

There are several important differences between NumPy arrays and native Python Arrays (arrays):

NumPy arrays have a fixed size when created, unlike Python's native array objects; changing the size of ndarray will create a new array and delete the original array.The elements of NumPy arrays must have the same data type, and therefore they have the same size in memory.NumPy arrays help with advanced mathematical operations and other types of operations on large amounts of data. Typically, these operations are more efficient and require less code than using Python's native arrays.More and more scientific and mathematical software packages based on Python use NumPy arrays, but they will convert the input arrays to NumPy arrays before processing them.

Before learning the NumPy tutorial, we need to have a basic understanding of Python, this website recommends using Python3.x version, if you are not familiar with Python, you can read ourPython Tutorial

Why use NumPy?

In Python, we have lists that satisfy array functions, but they are slow to process.NumPy aims to provide a faster alternative to traditional Python lists 50 times the array object.The array objects in NumPy are called ndarray, which provides many support functions to make it very easy to use ndarray.Arrays are very commonly used in data science because speed and resources are very important.Data science: a branch of computer science that studies how to store, use, and analyze data to extract information from it.

Why is NumPy faster than lists?

Unlike lists, NumPy arrays are stored in a continuous position in memory, so processes can access and manipulate them very efficiently.
This behavior is called the locality of reference in computer science.
This is the main reason why NumPy is faster than lists. It is also optimized to work with the latest CPU architectures.

NumPy Applications

NumPy is often used together with SciPy, this combination is widely used to replace MatLab, which helps us learn data science or machine learning through Python.SciPy is an open-source Python algorithm library and mathematical toolkit.SciPy includes modules such as optimization, linear algebra, integration, interpolation, special functions, fast Fourier transform, signal processing and image processing, solution of ordinary differential equations, and other common calculations used in science and engineering.Matplotlib is the visualization interface for the Python programming language and its numerical mathematics extension package NumPy. It provides a general-purpose graphical user interface toolkit.

Related Materials

NumPy Official Website:http://www.numpy.orgNumPy Source Code:https://github.com/numpy/numpySciPy Official Website:: https://www.scipy.orgSciPy Source Code:: https://github.com/scipy/scipyMatplotlib Source Code:: https://matplotlib.orgMatplotlib Source Code:: https://github.com/matplotlib/matplotlib

A Simple Numpy Example

# 1Installing Package
$ pip install numpy
# 2Entering the Python interactive interface
$ python -i
# 3Using Numpy
>>> from numpy import *
>>> eye(4)
# 4Output Result
array([1, 0., 0., 0.],
       [0., 1, 0., 0.],
       [0., 0., 1, 0.],
       [0., 0., 0., 1.]])