Getting Started with NumPy

NumPy (Numerical Python) is a Python library used for scientific computing. It can also be used as an efficient multi-dimensional container for data.

How to use NumPy

To use any library in Python as in most programming languages, we must first import it. To import NumPy you should have installed Python 3.6 and above. To import “numpy” as any other Python modules we use the keyword “import” as follows:

import numpy as np

We make a reference to the library as “np” instead of “numpy.” so that we don’t have to use the word “numpy” everywhere in our code, i.e., creating an array: np.array([1, 2, 3]).

In this tutorial, we will be using this method, although we could just import it as import numpy and the example for this would be numpy.array([1,2,3]) when creating an array.

If you already know Python and you have used Python lists, the comparison and differences between the two will make you understand NumPy easily and better.

NumPy vs. Lists

NumPy and Lists are similar to each other in the sense that they can both store data, be indexed, and be iterated. However, NumPy:

  • uses less memory,
  • is faster, and more
  • convenient than Lists.

Also, we cannot perform calculations (add, subtract, multiply, divide and exponentiation) on Python Lists but we can on NumPy Arrays.

Examples

In the following examples, In means INPUT and Out is the OUTPUT after running the code snippet.

Example to show the difference between Lists and NumPy:

Another example:

NumPy Operations with Examples
1. Finding the size of an array: the size tool returns how many values are in the array.

2. How to get the shape of an array: the shape tool returns the dimensions of the array in the format (rows, columns). Note: an array can be referred to as a matrix.

3. Reshaping an array: the reshape tool allows us to change the original dimension of an array. In this example, we changed the array dimensions from (2, 4) to (4, 2).

4. Getting a specific value from an array: returns the value at a specific position called on. It follows the format [rows, columns] with indexes starting at 0.

5. Extracting multiple values from an array: [0, 0:4] returns values from the first row and values from columns with indexes 0 to 3.

6. Reversing the order of an array: “ ::-1 ” in the rows position reverses the order of the rows whereas “ ::-1 ” in the columns position reverses the order of values in the columns.

7. Getting the max, min and sum of an array.

8. Finding the mean, median, variance, and standard deviation of an array.

9. Doing an operation on a row and column: “ axis = 0 ” performs an operation on each y-axis within the array. “ axis = 1 ” performs an operation on each x-axis within the array. “ axis = None ” performs an operation on all values in the array, it is default.

10. Joining arrays together to create one array: the concatenate tool joins arrays together. The final array can be reshaped by performing operations on specific axes.

11. Printing an array filled with zeros and ones: to clarify the zeros and ones tools returns an array filled with zeros or ones. The default type is float therefore changing dtype to reflect an integer will print integer values.

12. Printing identity matrices: in the first example, the identity tool returns a square matrix with the dimensions 3 by 3. In the second example, the eye tool prints an 8 by 7 matrix with the first upper diagonal shifted one over denoted with k = 1.

13. Multiplying matrices: the dot tool returns the dot products of two arrays. The cross tool returns the cross product of two arrays.

Related Posts