DEV Community

Cover image for NumPy Arrays
Tanveer Shahriar Arnob
Tanveer Shahriar Arnob

Posted on

NumPy Arrays

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely. This module is mainly used for Data Science and Machine Learning.


We can install NumPy using pip [pip is a python package manager]. Go to your terminal and type:

pip install numpy
Enter fullscreen mode Exit fullscreen mode

Using NumPy

To use the built in function from NumPy we have to import it first.

import numpy
Enter fullscreen mode Exit fullscreen mode

But after that if we want to use any function we have to write numpy every time to use that. That's why it is convention to import numpy as np.

import numpy as np
Enter fullscreen mode Exit fullscreen mode

NumPy Arrays

NumPy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).

Creating Numpy Arrays

We can create an array by directly converting a list or list of lists:

NumPy Array

NumPy Matrix

NumPy Array Data Type

There are also some built in functions to generate arrays.


numpy.arange([start, ]stop, [step, ])
arange is like the range function of python. It returns evenly spaced values within a given interval. Here start and step is optional if we need it but stop is mandatory.

arange can be called with a varying number of positional arguments:

arange(stop): Values are generated within the half-open interval [0, stop) (in other words, the interval including start but excluding stop).

arange(start, stop): Values are generated within the half-open interval [start, stop).

arange(start, stop, step) Values are generated within the half-open interval [start, stop), with spacing between values given by step.

NumPy arange


numpy.zeros(shape, dtype=float)
It generates a array of given shape and type, filled with zeroes. Here shape can be a int which will generate 1D array or a tuple of ints which will generate matrices. Instead of tuple we can use list of ints also. And dtype is by default float but we can change it into int.

NumPy zeros


numpy.ones(shape, dtype=float)
It is same as the zeros function but instead of zeroes all of the elements are one.

NumPy ones


numpy.linspace(start, stop, num=50)
Returns num evenly spaced samples, calculated over the interval [start, stop]. And num is by default 50.

NumPy linspace


Return a 2-D array with ones on the diagonal and zeros elsewhere. Simply it generates a nxn identity matrix.

NumPy eye


NumPy also has lots of ways to create arrays with random numbers.


numpy.random.rand(d0, d1, ..., dn)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

NumPy Random rand


numpy.random.randn(d0, d1, ..., dn)
Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform. Also rand cannot generate negative values but randn can.

NumPy Random randn


random.randint(low, high=None, size=None)
Return random integers from low (inclusive) to high (exclusive). If we don't mention size then it will return one value. For size we can pass an int or a tuple of ints or a list of ints.

NumPy Random randint

Array Attributes and Methods


Returns an array containing the same data with a new shape. But we have to declare the shape such that we have the proper amount of elements. Suppose we have a array of 25 elements. We can transform it into a 5x5 matrix but we can not reshape into a 3x4 matrix as it can not contain 25 element.

NumPy reshape


It will return the max value of the array.

NumPy max


It will return the index of the max value of the array.

NumPy argmax


It will return the min value of the array.

NumPy min


It will return the index of the min value of the array.

NumPy argmin


Shape is an attribute that arrays have which returns the shape of the array.

NumPy shape


You can also grab the data type of the object in the array.

NumPy dtype

That's it for today. Feel free to ask any question in the comment.

Top comments (0)