Introduction to Numpy

Pritika Waghmare
5 min readMay 14, 2021
Numpy in Python

Here in this blog I will introduce the basis of Numpy. If you want to do machine learning then knowledge of NumPy is necessary. It one of the most widely used python library Numeric Python. It is the most useful library if you are dealing with numbers in python. NumPy guarantees great execution speed comparing it with python standard libraries. It comes with a great number of built-in function.

Installation of Numpy

First, let’s talk about its installation. NumPy is not part of a basic Python installation. We need to install it after the installation of python in our system. We can do it by pip using command pip install NumPy or it comes with conda as well.

We are done with the installation and now let’s jump right into NumPy. First, let start with the most important object in NumPy that is the ndarray or multi-dimensional array. Multi-dimensional array in similar words in an array of arrays which means array for example [1,2,3] is a one-dimensional array because it contains only one row where two-dimensional array as given below contain multiple rows as well as multiple columns.

[[1 2 3]

[4 5 6]

[7 8 9]]

Working with NumPy

Creating NumPy Arrays, Loading and Saving Files

NumPy works with Python objects called multi-dimensional arrays. Arrays are basically collections of values, and they have one or more dimensions. NumPy array data structure is also called ndarray, short for n-dimensional array. An array with one dimension is called a vector and an array with two dimensions is called a matrix. Datasets are usually built as matrices and it is much easier to open those with NumPy instead of working with list of lists, for example.

Turning a list to a NumPy array is pretty simple:

numpy_array = np.array(list)

And printing/displaying the array will look like this:

array([[ 7.4 , 0.7 , 0. , …, 0.56 , 9.4 , 5. ],
[ 7.8 , 0.88 , 0. , …, 0.68 , 9.8 , 5. ],
[ 7.8 , 0.76 , 0.04 , …, 0.65 , 9.8 , 5. ],
…,
[ 6.3 , 0.51 , 0.13 , …, 0.75 , 11. , 6. ],
[ 5.9 , 0.645, 0.12 , …, 0.71 , 10.2 , 5. ],
[ 6. , 0.31 , 0.47 , …, 0.66 , 11. , 6. ]])

Working and Inspecting Arrays

Now that you have your array loaded, you can check its size (number of elements) by typing array.size and its shape (the dimensions — rows and columns) by typing array.shape. You can use array.dtype to get the data types of the array (floats, integers etc — see more in the NumPy documentation) and if you need to convert the datatype you can use the array.astype(dtype) command. If you need to convert a NumPy array to a Python list, there is a command for that too: array.tolist().

Indexing and Slicing

Indexing and slicing NumPy arrays works very similarly to working with Python lists: array[5] will return the element in the 5th index, and array[2,5] will return the element in index[2][5]. You can also select the first five elements, for example, by using a colon (:). array[0:5] will return the first five elements (index 0–4) and array[0:5,4] will return the first five elements in column 4. You can use array[:2] to get elements from the beginning until index 2 (not including index 2) or array[2:] to return from the 2nd index until the end of the array. array[:,1] will return the elements at index 1 on all rows.

Assigning values to a NumPy array is, again, very similar to doing so in Python lists: array[1]=4 will assign the value 4 to the element on index 1; you can do it to multiple values: array[1,5]=10 or use slicing when assigning values: array[:,10]=10 will change the entire 11th column to the value 10.

Sorting and Reshaping

array.sort() can be used to sort your NumPy array — you can pass different arguments inside the brackets to define what you want to sort on (by using the argument ‘order=string/list of strings’, for example. See more examples in the documentation). array.sort(axis=0) will sort specific axis of the array — rows or columns. two_d_arr.flatten() will flatten a 2 dimensional array to a 1 dimensional array. array.T will transpose an array — meaning columns will become rows and vice versa. array.reshape(x,y) would reshape your array to the size you set with x and y. array.resize((x,y)) will change the array shape to x and y and fill new values with zeros.

Combining and Splitting

You can use np.concatenate((array1,array2),axis=0) to combine two NumPy arrays — this will add array 2 as rows to the end of array 1 while np.concatenate((array1,array2),axis=1) will add array 2 as columns to the end of array 1. np.split(array,2) will spilt the array into two sub-arrays and np.hsplit(array,5) will split the array horizontally on the 5th index.

Adding and Removing Elements

There are, of course, commands to add and remove elements from NumPy arrays:

· np.append(array,values) will append values to end of array.

· np.insert(array, 3, values)will insert values into array before index 3

· np.delete(array, 4, axis=0)will delete row on index 4 of array

· np.delete(array, 5, axis=1) will delete column on index 5 of array

Descriptive Statistics

You can use NumPy methods to get descriptive statistics on NumPy arrays:

· np.mean(array,axis=0) will return mean along specific axis (0 or 1)

· array.sum() will return the sum of the array

· array.min()will return the minimum value of the array

· array.max(axis=0)will return the maximum value of specific axis

· np.var(array)will return the variance of the array

· np.std(array,axis=1)will return the standard deviation of specific axis

· array.corrcoef()will return the correlation coefficient of the array

· numpy.median(array) will return the median of the array elements

Doing Math with NumPy

Any tutorial to NumPy would not be complete without the numerical and mathematical operations you can do with NumPy! Let’s go over them:

np.add(array ,1) will add 1 to each element in the array and np.add(array1,array2) will add array 2 to array 1. The same is true to np.subtract(), np.multiply(), np.divide() and np.power() — all these commands would work in exactly the same way as described above.

You can also get NumPy to return different values from the array, like:

· np.sqrt(array) will return the square root of each element in the array

· np.sin(array) will return the sine of each element in the array

· np.log(array) will return the natural log of each element in the array

· np.abs(arr) will return the absolute value of each element in the array

· np.array_equal(arr1,arr2) will return True if the arrays have the same elements and shape

It is possible to round different values in array: np.ceil(array) will round up to the nearest integer, np.floor(array) will round down to the nearest integer and np.round(array) will round to the nearest integer.

This is just the tip of the iceberg when it comes to what you can do with NumPy! I do hope that this blog post did help you see the possibilities and how powerful NumPy can be when working on data with Python.

--

--