All You Need to Know About Numpy

Original article was published on Artificial Intelligence on Medium

All You Need to Know About Numpy

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. NumPy brings the computational power of languages like C and Fortran to Python, a language much easier to learn and use. With this power comes simplicity: a solution in NumPy is often clear and elegant. NumPy lies at the core of a rich ecosystem of data science libraries.

Why Numpy?

The standard Python data types are not very suited for mathematical operations. Suppose we have some list and we want to multiply that list with an integer, we will get the repeated element and floats are not even allowed. This is because Python lists are not designed as mathematical objects. Rather, they are purely a collection of items. In order to get a type of list that behaves like a mathematical array or matrix, we use Numpy.

Creating a Numpy Array (ndarrays)

A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

We can initialize NumPy arrays from nested Python lists, and access elements using square brackets. Numpy also provides many functions to create arrays:

The more important attributes of a ndarray object are:

  • ndarray.ndim the number of axes (dimensions) of the array.
  • ndarray.shape the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, the shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
  • ndarray.size the total number of elements of the array. This is equal to the product of the elements of shape.
  • ndarray.dtype an object describing the type of the elements in the array. One can create or specify dtypes using standard Python types. Additionally, NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.

Array Indexing

Numpy offers several ways to index into arrays.

Slicing: Similar to Python lists, numpy arrays can be sliced. Just like with lists, we might want to select certain values from an array since arrays may be multidimensional, you must specify a slice for each dimension of the array.

Boolean array indexing: Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition.

Arange and linspace

The essential difference between NumPy linspace and NumPy arange is that linspace enables you to control the precise end value, whereas arange gives you more direct control over the increments between values in the sequence.

Random Number Generator

Array Math

Basic mathematical functions operate elementwise on arrays and are available both as operator overloads and as functions in the numpy module.

Array Broadcasting

Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array.

Some Advanced Mathematics Operations