In this article, we’ll learn about Python NumPy, multidimensional Python Numpy array, the role of NumPy for machine learning, Python list vs NumPy, NumPy installing and importing, and finally how to create a Python NumPy array.

## What is Python NumPy?

- NumPy stands for Numeric Python.
- For Python, NumPy works as a scientific computing library.
- It has the feature of a multi-dimensional array and matrix through which it supports a large number of data. In order for us to build a machine learning model, for that we need a larger amount of data but that data will be in a format like TSV(tab-separated values), CSV(comma-separated values), etc. The machine learning model doesn’t accept it in that format so first, it should be converted in an array like 1D, 2D etc. By using this array, our ML model can execute operations.
- Calculations like Fourier transforms, linear algebra, and random number capabilities can be executed using Python NumPy array.

## Python Multidimensional Arrays

### 1. 1D NumPy Array

Let’s understand how to represent a one-dimensional Python array.

array([6,8,4])

Use a Python list inside the parenthesis of the array. The list can have single or multiple items.

### 2. 2D NumPy Array

Let’s now learn how to define a two-dimensional Python array.

array([ [5,6,7], [2,4,6], [2,3,6] ])

We can define the 2D array as a list inside a list as shown in the above example.

### 3. 3D NumPy Array

array([[ [5,6,7], [2,4,6], [2,8,6] ], [ [4,6,7], [2,4,2], [2,3,8] ], [ [7,6,7], [2,4,6], [2,9,2] ] ])

We can represent the 3D array as a Python list inside list inside list.

## Python NumPy vs Python List

We’ve seen that numPy also used a list so it means it works like Python lists but the Numpy library is written in Python and C programming language which gives numPy some advantages. Let’s see what they are:

Operations time is faster while using NumPy.

Data storage memory consumption is less.

Python NumPy is more convenient while performing operations using it as compared to Python lists.

Unlike Python lists, one continuous place is taken by NumPy to store arrays which makes them easily accessible for operations.

## Why choose NumPy for Machine Learning?

We use a number of images in machine learning to predict an output. For instance, we want to train our model to predict a specific image. For that, we need to give our machine-learning model a number of images.

Keep in mind that the ML model does not read images directly. Instead, it takes numbers so we can convert our image into number format like a greyscale image that will be converted to binary format and that format will be converted to Python numPy multidimensional array. Now by using this array, the Python ML model can perform its specified operations.

## CSV and TSV passed to ML Model

We can pass data to the ML model using CSV(comma-separated values) or TSV(tab-separated values). See below examples:

### CSV

ClientID Workout(times) Sleep(hours) Water(litres) BodyWeight(kg) 1, 3, 8, 4, 78 2, 3, 7, 3, 85 3, 3, 9, 5, 81 4, 2, 6, 2, 74

### TSV

ClientID Workout(times) Sleep(hours) Water(litres) BodyWeight(kg) 1 3 9 4 90 2 2 6 3 81 3 3 9 4 83 4 4 9 2 78

array([ [1,3,8,4], [2,3,7,3], [3,3,8,5], [4,2,6,2] ])

This is called a matrix in machine learning. It’s a 2D dimensional NumPy array which we have learned in the Python multi-dimensional arrays section.

array([ 78, 85, 81, 74 ])

This is called a vector in ML.

## Python NumPy Installation

In our previous article, we learned how to **install Anaconda navigation and Jupyter Notebook**. Some libraries have been installed using Anaconda in which numPy is also included if we are using Spyder or Jupyter Notebook. But if we are using PyCharm or other IDE then we need to install it manually.

For manual installation of Python NumPy, open the command prompt(cmd), then write the below line and press enter.

pip install numpy

**Important note: **Make sure you have an internet connection when installing NumPy.

## NumPy import

In order to use NumPy, we have to import it using the below line.

import numpy as np

We can use only **‘import numpy’** as well. If you need to make the name short then use the **‘as’** and after that, give it a short name as we’ll be using it in our code.

## Use NumPy Array

np.array([1,2,3,4])

This is a simple example of how we can use a Python NumPy array. In our next articles, we’ll explain its usage with multi-dimension arrays practically.

## Conclusion

In conclusion, we’ve learned what **Python NumPy array**(1D, 2D, 3D) is and how to install and import it. We’ve also gone through a simple example of how a Python NumPy array can be used. In our next articles, we’ll explain its implementation in more detail and through proper coding examples, so don’t forget to visit these articles as well.

Thank you for reading it.

Pingback: Python Multidimensional NumPy Array: Size, Type, Dimensions, Shape - Machine Learning PY