The NumPy library from Python is one of the three most commonly used libraries for numerical computing and data science, along with Pandas and matplotlib. The word NumPy is a short hand notation for Numerical Python. The NumPy library provides an n-dimensional object with different functionalities that can be used to perform a variety of data science, machine learning and scientific computing tasks.
This tutorial provides a brief introduction to the NumPy library. We’ll cover some of the most fundamental NumPy operations, focusing specifically on NumPy arrays. We’ll learn how to reshape and copy NumPy arrays, and how to perform arithmetic and matrix operations on them. Let’s get started.
Note: All the codes in this article are compiled with the Jupyter Notebook.
NumPy Installation
To install the NumPy library, you can use pip installer. The following command installs the NumPy library:
$ pip install numpy
Creating NumPy Arrays
A NumPy array is an n-dimensional object that can be used to store items of a specific data type. There are different ways to create a NumPy array. The simplest way is to pass a list of elements to the array()
method of the NumPy module. Look at the following example:
import numpy as np
nums = [10,20,30,40]
oned_array = np.array(nums)
The above script first imports the numpy
module and then creates a simple one-dimensional NumPy array with 4 elements. You can print the type of the oned_array
variable to actually see if it is a NumPy array or not, as shown below:
print(type(oned_array))
print(oned_array)
Output:
<class 'numpy.ndarray'>
[10 20 30 40]
If you’re familiar with Pandas, you’ll likely notice some similarities between NumPy arrays and Pandas DataFrames.
To create a two-dimensional array, you need to create a 2 dimensional list or a list of lists and pass it to the array()
method as shown below:
nums2 = [[10,20,30], [40,50,60], [70,80,90]]
twod_array = np.array(nums2)
print(type(twod_array))
print(twod_array)
Output:
<class 'numpy.ndarray'>
[[10 20 30]
[40 50 60]
[70 80 90]]
Finally, you can specify the type of elements that your numpy array is going to store. The type can be passed to the dtype
attribute of the array()
method. For instance, the following script creates a two dimensional NumPy array of all floats.
nums2 = [[10,20,30], [40,50,60], [70,80,90]]
twod_array = np.array(nums2, dtype = 'float')
print(type(twod_array))
print(twod_array)
Output:
<class 'numpy.ndarray'>
[[10. 20. 30.]
[40. 50. 60.]
[70. 80. 90.]]
The data types that you can use to create a NumPy array are:
- ‘float’
- ‘int’
- ‘bool’
- ‘str’
- ‘object’
We’ll be using the
Difference between a List and a NumPy array
It is important to understand the differences between a normal Python list and a NumPy array. Normal Python lists are extendable while you cannot add new elements to a NumPy array. Similarly, list operations are performed on the whole list while NumPy operations are broadcasted to all the items in the array. This might not make sense yet, so lets look at the following example.
nums2 = nums2 * 2
print(nums2)
In the script above we multiply the nums2
Python list (created above) by 2. In the output, you will see that all the items in the list will be duplicated rather than multiplying every element in the list by 2. Here is the output for the Python list.
Output:
[[10, 20, 30], [40, 50, 60], [70, 80, 90], [10, 20, 30], [40, 50, 60], [70, 80, 90]]
On the other hand, if you multiply a NumPy array by an integer, the individual items in the NumPy array are multiplied. Take a look:
twod_array = twod_array * 2
print(twod_array)
Output:
[[ 20 40 60]
[ 80 100 120]
[140 160 180]]
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Finding Shape and Dimensions of a NumPy Array
The shape of a NumPy array refers to the number of elements in each dimension. The shape and dimensions of a NumPy array can be calculated via the shape
and dimension
attributes.
Let’s create an array with 3 rows and 5 columns.
nums2 = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
twod_array = np.array(nums2, dtype = 'float')
print(type(twod_array))
print(twod_array)
Output:
<class 'numpy.ndarray'>
[[ 10. 20. 30. 40. 50.]
[ 40. 50. 60. 70. 80.]
[ 70. 80. 90. 100. 110.]]
Let’s now find the shape and dimensions of the twod_array
.
print('Array Shape: ', twod_array .shape)
print('Item types: ', twod_array .dtype)
print('Total Items: ', twod_array .size)
print('Array Dimensions: ', twod_array .ndim)
Output:
Array Shape: (3, 5)
Item types: float64
Total Items: 15
Array Dimensions: 2
Array Slicing
Array slicing refers to extracting subset of an array. Let’s create a two dimensional NumPy array:
nums2 = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
twod_array = np.array(nums2, dtype = 'float')
print(type(twod_array))
print(twod_array)
Output:
<class 'numpy.ndarray'>
[[ 10. 20. 30. 40. 50.]
[ 40. 50. 60. 70. 80.]
[ 70. 80. 90. 100. 110.]]
To slice a NumPy array, you have to use the color (:) operator. The following script returns the first two rows and first two columns of the twod_array
.
temp = twod_array[:2,:2]
print(temp)
The slice operator used in the above script specifies the script should start from the beginning and take all the rows less than 2 i.e. 0 and 1. Notice the row and column numbers in NumPy array start at 0th index. Next, the column slicing is specified after the comma. In the above script, we again tell the script to start from the 0th column and take all the columns less than 2.
Output:
[[10. 20.]
[40. 50.]]
In the next script, we select the rows starting from index 1 up to rows less than 3 (1, 2), and all the columns from the beginning to one less than column number 4 (0, 1, 2, 3).
temp = twod_array[1:3,:4]
print(temp)
Output:
[[ 40. 50. 60. 70.]
[ 70. 80. 90. 100.]]
Negative numbers have a meaning, too, when slicing a NumPy array. You can select the last row or column by specifying -1 as the index value. For instance, in the following script, we select the 2nd and 3rd row and then we select the very last column.
temp = twod_array[1:3,-1:]
print(temp)
Output:
[[ 80.]
[110.]]
Finally, you can also compare a NumPy Array with a number to get an array of all boolean values. For instance, the following script compares each element of the twod_array
with the integer 50. In the place of the elements that are greater than 50, True
is returned. For the elements smaller than 50, False
is returned as shown in the following example.
bool_array = twod_array> 50
print(bool_array)
Output:
[[False False False False False]
[False False True True True]
[ True True True True True]]
Finding Minimum, Maximum and Mean Values
To find the minimum, maximum and mean values in a NumPy array, the min()
, max()
, and mean()
functions are respectively used.
Here’s an example.
int("Mean of Array elements is: ", twod_array.mean())
print("Max value in the array is: ", twod_array.max())
print("Min value in the array is: ", twod_array.min())
Output:
Mean of Array elements is: 60.0
Max value in the array is: 110.0
Min value in the array is: 10.0
You can also find the minimum and maximum values in each row and column via amin()
function. To find the corresponding values in rows, you have to pass 1 as the value for the axis
value. To find minimum or maximum values for all columns, simply pass 0 as the value for the axis
attribute. Look at the following example.
print("Minimum Values in all columns: ", np.amin(twod_array, axis=0))
print("Minimum values in all rows: ", np.amin(twod_array, axis=1))
Output:
Minimum Values in all columns: [10. 20. 30. 40. 50.]
Minimum values in all rows: [10. 40. 70.]
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Making Copies of a NumPy Array
There are two ways to copy a NumPy array. You can make shallow copies or deep copies. When you use an equals operator to assign the value of an array on the right side of the equals operator to the variable on the left, a shallow copy is created. If you try to update the array values using the newly generated copy, the values in the original NumPy array are also updated. Look at the following example.
nums2 = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
twod_array = np.array(nums2, dtype = 'float')
new_array = twod_array
new_array[0,0] = 50
print(twod_array)
Output:
[[ 50. 20. 30. 40. 50.]
[ 40. 50. 60. 70. 80.]
[ 70. 80. 90. 100. 110.]]
In the example above, the
To make a deep copy where changing the values in the copied array has no affect on the original array, you need to call the copy()
method as shown in the following example.
nums2 = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
twod_array = np.array(nums2, dtype = 'float')
new_array = twod_array.copy()
new_array[0,0] = 50
print(twod_array)
Output:
[[ 10. 20. 30. 40. 50.]
[ 40. 50. 60. 70. 80.]
[ 70. 80. 90. 100. 110.]]
Notice item (0,0) retains it’s original value of 10 in the untouched
Reshaping a NumPy Array
You can reshape an existing NumPy array by calling the reshape()
method and passing it the new dimensions. It is important to mention that if you multiply the items in each dimension, the answer should be equal to the total number of items in a NumPy array. Let’s create a NumPy array with 3 rows and 5 columns.
nums2 = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
twod_array = np.array(nums2, dtype = 'float')
print(twod_array.shape)
Output:
(3, 5)
Here the array twod_array
has total of 15 columns. Let’s reshape the array to have 5 rows and 3 columns using the NumPy reshape
method.
twod_array = np.array(nums2, dtype = 'float')
temp = twod_array.reshape(5,3)
print(temp)
Output:
[[ 10. 20. 30.]
[ 40. 50. 40.]
[ 50. 60. 70.]
[ 80. 70. 80.]
[ 90. 100. 110.]]
Creating a Numpy Array with arange()
, zeros()
, ones()
Functions
In addition to passing a list as a parameter to the numpy()
function, you can also create NumPy arrays via the built-in arange()
, zeros()
, and ones()
methods.
Let’s first examine the different ways the arange()
method is used to create Numpy arrays.
# create numpy array from 0-9
print(np.arange(10))
# create numpy array from 1-9
print(np.arange(1, 10))
# create numpy array from 0 to 20(exclusive) with all even numbers
print(np.arange(0, 20, 2))
# create numpy array from 20 to 0 (exclusive) with a step of two in reverse
print(np.arange(20, 0, -2))
Output:
[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[ 0 2 4 6 8 10 12 14 16 18]
[20 18 16 14 12 10 8 6 4 2]
The zeros()
method creates a NumPy array of all zeros. Since you’re not populating values, you only have to pass array dimensions to the zeros method.
zeros_array = np.zeros((3, 4))
print(zeros_array)
Output:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Similarly, the ones()
method creates a NumPy array of all ones.
ones_array = np.ones((3, 4))
print(ones_array)
Output:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Arithmetic Operations
You can also perform different types of arithmetic operations on NumPy arrays. We’ll introduce some of the most common functions here.
Log Function
The log can be calculated via log()
function.
logs = np.log(np.arange(1,5))
print(logs)
Output:
[0. 0.69314718 1.09861229 1.38629436]
Exponentials (Euler’s Number)
The formula ex is passed each x
value from a NumPy array and solved via the exp()
function. The variable e is Eueler’s number, so e1 equals 2.71828183, as demonstrated in the example below:
exps = np.exp(np.arange(1,5))
print(exps)
Output:
[ 2.71828183 7.3890561 20.08553692 54.59815003]
Square Root
The sqrt()
function is used to find the square root of all the elements in a NumPy array as shown in the following example:
sqr = np.sqrt(np.arange(1,5))
print(sqr)
Output:
[1. 1.41421356 1.73205081 2. ]
Finding sins
The sin of all the elements in a NumPy array can be calculated via the sin()
function.
sins = np.sin(np.arange(1,5))
print(sins)
Output:
[1. 1.41421356 1.73205081 2. ]
You pass the sin function an angle in radians. To pass it an angle in degrees, you’ll need to use the NumPy deg2rad
function, like this:
sins = np.sin(np.deg2rad(90))
print(sins)
Output:
1.0
The deg2rad
function behaves the same as the radians
function, so you can use the two interchangeably.
Matrix Operations
Quick matrix operations are some of the most powerful features of the NumPy library. These functions are particularly useful when performing linear algebra and machine learning tasks on 2D arrays.
Matrix Dot Product
To find the dot product of two matrices in NumPy, the dot()
function can be used. It is important to mention that the number of columns in the matrix on the left side of the dot product must match the number of rows for the matrix on the right. Here is an example.
nums = [[10,20,30,40,50], [40,50,60,70,80], [70,80,90,100,110]]
m1 = np.array(nums, dtype = 'int')
nums2 = [[10,20], [40,50], [70,80], [90,100], [110,120]]
m2 = np.array(nums2, dtype = 'int')
print(m1.shape)
print(m2.shape)
result = np.dot(m1, m2)
print(result)
print(result.shape)
Output:
(3, 5)
(5, 2)
[[12100 13600]
[21700 24700]
[31300 35800]]
(3, 2)
Matrix Multiplication
In addition to a dot product, you can element-wise multiply two matrices via the multiply()
function.
result = np.multiply(m1,m1)
print(result)
Output:
[[ 100 400 900 1600 2500]
[ 1600 2500 3600 4900 6400]
[ 4900 6400 8100 10000 12100]]
Matrix Inverse
The inv()
function of the linalg
module is used to find the inverse of a matrix as shown below:
nums = [[10,20,30], [40,50,60], [70,80,90]]
m1 = np.array(nums, dtype = 'int')
result = np.linalg.inv(m1)
print(result)
Output:
[[ 1.97032484e+15 -3.94064967e+15 1.97032484e+15]
[-3.94064967e+15 7.88129935e+15 -3.94064967e+15]
[ 1.97032484e+15 -3.94064967e+15 1.97032484e+15]]
Matrix Determinant
Finally, to find the determinant of a matrix, you can use the det()
function of the linalg
module.
nums = [[10,20,30], [40,50,60], [70,80,90]]
m1 = np.array(nums, dtype = 'int')
result = np.linalg.det(m1)
print(result)
Output:
-1.5225915766287873e-13
Conclusion
NumPy is an extremely useful Python library for performing machine learning and scientific computing task. In this tutorial you saw some of the most basic NumPy operations. You studied how to create a NumPy array, how to find the shape and dimensions of a NumPy array, how to perform array slicing and how to find minimum, maximum and mean values from an array.
We also showed you how to use NumPy to make deep and shallow copies of a NumPy array, how to reshape a NumPy array, and how to use the arange()
, zeros()
and ones()
functions to quickly create specialized NumPy arrays. We rounded out the tutorial with some examples demonstrating how to perform arithmetic and matrix operations on NumPy arrays, which is useful for linear algebra and machine learning.
If you liked this NumPy tutorial, please subscribe using the form below. We’ll show you more Python tips and shortcuts to help you master this programming language.
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.