# NumPy Introduction

## Prerequisites

A basic understanding of Python or any other programming languages.

For Python Basics Learn it from here -> Python Basics

## What is a Python NumPy?

'

## 3 Important reasons to learn NumPy

**1. Pandas is built on top of NumPy**

Pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series

**2. Scikit-learn package is built on top of NumPy**

**3. Replacement for MatLab - Paid software**

## Dimension Diagram

## How to create a numpy array?

### Single-dimensional (1D) Numpy Array

#Importing the NumPy library import numpy as np #1-Dimensional Array - Using List a=np.array([1,2,3,4,5]) print(a) Output: [1 2 3 4 5]

### Two-dimensional (2D) Numpy Array

#Importing the NumPy library import numpy as np # 2-Dimensional Array b = np.array([[1,2,3], [4,5,6]]) print(b) Output: [[1 2 3] [4 5 6]]

### Three-dimensional (3D) Numpy Array

#Importing the NumPy library import numpy as np # 3-Dimensional Array c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(c) Output: [[[1 2] [3 4]] [[5 6] [7 8]]]

## Array Attributes for 1D, 2D and 3D for above examples

1. ndim - It returns the number of array dimensions

#Code to display dimensions print(a.ndim) print(b.ndim) print(c.ndim) Output: 1 2 3

2. shape - It returns a tuple consisting of array dimensions. It can also be used to resize the array

#Code to display shape print(a.shape) print(b.shape) print(c.shape) Output: (5,) (2, 3) (2, 2, 2)

3. len - It returns length of the array. For ndimension it returns the size of the first dimension

#Code to display length print(len(a)) print(len(b)) print(len(c)) Output: 5 2 2

4. dtype - It return the data type of the array

#Code to display data type print(a.dtype) print(b.dtype) print(c.dtype) Output: int32 int32 int32

5. size - It returns the total number of elements inside the array

#Code to display total number of elements print(a.size) print(b.size) print(c.size) Output: 5 6 8

6. itemsize - It returns the length of each element of array in bytes.

#Code to display size of element in bytes print(a.itemsize) print(b.itemsize) print(c.itemsize) Output: 4 4 4

## Reason to use Python NumPy Array instead of List

### 1. High Performance (Fast)

# Python List - Cube of number %timeit cube = [i**3 for i in range(100)] Output: 50.8 µs ± 4.52 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

#NumPy code for Cube of number import numpy as np num = np.arange(100) %timeit num**3 Output: 1.58 µs ± 116 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

**Conclusion:** List took 50.8 ms whereas the numpy array took just 1.58ms. Hence, numpy array is faster than list.

### 2. Less Memory

# List Memory allocation import time import sys cube = [i**3 for i in range(100)] print("Memory Allocated in List =",sys.getsizeof(cube)*len(cube)) Output: Memory Allocated in List = 91200

# NumPy Memory allocation import numpy as np num = np.arange(100) cube = num**3 print("Memory Allocated in NumPy =",num.size*cube.itemsize) Output: Memory Allocated in NumPy = 400

**Conclusion:** The difference between the list and numpy is very high and this makes python numpy array as the preferred choice over list..