Numpy is one of the most commonly used packages for scientific computing in Python. It provides a multidimensional array object, as well as variations such as masks and matrices, which can be used for various math operations. Numpy is compatible with, and used by many other popular Python packages, including pandas and matplotlib.
Why is numpy so popular? Quite simply, because it’s faster than regular Python arrays, which lack numpy’s optimized and pre-compiled C code that does all the heavy lifting. Another reason is that numpy arrays and operations are vectorized, which means they lack explicit looping or indexing in the code. This makes the code not only more readable, but also more similar to standard mathematical notation.
The following example illustrates the vectorization difference between standard Python and numpy.
For two arrays A and B of the same size, if we wanted to do a vector multiplication in Python:
c = [] for i in range(len(a)): c.append(a[i]*b[i])
In numpy, this can simply be done with the following line of code:
c = a*b
Numpy makes many mathematical operations used widely in scientific computing fast and easy to use, such as:
- Vector-Vector multiplication
- Matrix-Matrix and Matrix-Vector multiplication
- Element-wise operations on vectors and matrices (i.e., adding, subtracting, multiplying, and dividing by a number )
- Element-wise or array-wise comparisons
- Applying functions element-wise to a vector/matrix ( like pow, log, and exp)
- A whole lot of Linear Algebra operations can be found in NumPy.linalg
- Reduction, statistics, and much more
The following parts will provide you with step-by-step instructions on how to work with NumPy, including:
How to import NumPy in Python

Before you can import numpy, you first need to install it. There are two ways to install numpy:
- Install the binary (pre-compiled) version using pip
- Compile it from source code, and then install it
The simplest way to install numpy is to use the pip package manager to download the binary version from the Python Package Index (PyPI.org) and install it on your system using the following command:
pip install numpy
Afterward, you can check if Numpy is properly installed by starting Python and running the following lines of codes.
import numpy as np
print(np.__version__)
If everything is properly installed, you should see an output similar to this:
'1.15.1'
If your company requires that all packages be built from source, you’ll need to manually compile the Numpy library, which means you’ll need to install the following on your local system:
- A C compiler
- A FORTRAN 77 compiler
- Cython
- A packaging solution for your target OS
Compiling from source can get quite complex, what with environment setup, scripts and patches, not to mention resolving any dependency conflicts or errors that may occur.
How to convert pandas to NumPy

Pandas and numpy work very well together. It is quite easy to transform a pandas dataframe into a numpy array. Simply using the to_numpy() function provided by Pandas will do the trick.
If we wanted to turn an entire dataframe into a numpy array, we can simply use the following line of code:
df.to_numpy()
This will return us a numpy 2D array of the same size as our dataframe (df), but with the column names discarded.
If we wanted to convert just the first two columns in a dataframe into a numpy array, we can use to_numpy() as follows:
df[["column1","column2"]].to_numpy()
If we wanted to convert just a few cells of data in a dataframe into a numpy array, we can simply slice the dataframe and convert the output using to_numpy():
df.iloc[[0,1,2],[0,1,2]].to_numpy()
The above code converts the first three columns and first three rows to a numpy array.
How to build a NumPy array

There are many ways of creating a Numpy array. Let’s start with the simplest one: an array of zero dimensions (0d), which contains a single element.
arr = np.array(4)
Here we use the np.array function to initialize our array with a single argument (4). The result is an array that contains just one number: 4. That’s simple enough, but not very useful.
We can create a regular 1-dimensional (1d) array by giving the np.array function a list as an argument.
arr = np.array([1,2,3,4])
Or
arr = np.array((1, 2, 3, 4))
Both of which give us the following array:
[1 2 3 4]
Note that the second example uses a tuple as an argument, which also works.
We can create a numpy array of any dimension we want simply by giving it an argument of that dimension (2D, 3D, etc). For example, to create a 2-dimensional array (2D) with:
arr = np.array([[1,2],[3,4]])
Or
arr = np.array(((1,2),(3,4)))
Both of which give us the following 2D array:
[[1 2]
[3 4]]
Another functionality of np.array function allows us to create any kind of numpy array with any dimension without specifically providing that dimensioned array as an argument. For example, we can create a 5-dimensional Numpy Array from just a regular 1d array.
arr = np.array([1, 2, 3, 4], ndmin=2)
print(arr.ndim)
print(arr.shape)
Here the print statement will print 2 as the dimension, and our array will be a 2-dimensional array with only the given list. The print statement printing the shape will print (1,4) as the rest of the array is empty.
The output looks like this:
2
(1, 4)
There are some intrinsic functions numpy provides for easy array creation, as well. Let’s take a look at the “zeros” and “ones” functions:
arr = np.zeros((2,2))
arr = np.ones((2,2))
The above code snippet will create two different arrays:
- The first array will contain only zeros in a 2×2 array
[[0 0]
[0 0]]
- The second array will contain only ones in a 2×2 array
[[1 1]
[1 1]]
As you can see from the code the argument given as a tuple will define the size.
For use in linear algebra, numpy provides the following function:
arr = np.eye(3)
The above code snippet will create the following array:
[[1 0 0]
[0 1 0]
[0 0 1]]
Finally, let’s look at the arange() function:
arr = np.arange(10)
The above code snippet will print generate a 1D array of elements from 0 to 9:
[0 1 2 3 4 5 6 7 8 9]
How to turn a NumPy array into a list?

It is easy to transform a numpy array into a Python list using the numpy tolist() function or the list() function. Let’s take a look at the list() function first:
a = np.arange(4)
print(type(list(b)[0]))
print(type(list(b)))
Which will print the following.
<class 'numpy.int64'>
<class 'list'>
As you can see, the numpy array was transformed into a Python list, but the elements are still numpy built-in integer values. This is because using the list() function doesn’t recursively transform all elements. As a result, you can only use the list() function on one dimensional numpy arrays.
Now let’s look at the tolist() function. Using our example above, we can apply the tolist() function as follows:
a = np.arange(4)
print(type(b.tolist()[0]))
print(type(b.tolist()))
The tolist() function can be used for any dimension numpy array of any shape:
a = np.arange(4).reshape(2,2)
a.tolist()
The above code snippet will create a 1D numpy array with digits from 0 to 3, reshape it to be a 2 by 2 array, and then print it out as a Python list.