## What is NumPy?

NumPy stands for **Numerical Python** and is the core library for numeric and scientific computing.

Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open-source project.

## Operations using NumPy

- Mathematical and logical operations on arrays.
- Fourier transforms and routines for shape manipulation.
- Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.

NumPy β A Replacement for MatLab

NumPy is often used along with packages like SciPy (Scientific Python) and Matplotlib (plotting library). This combination is widely used as a replacement for MatLab, a popular platform for technical computing. However, Python alternative to MatLab is now seen as a more modern and complete programming language.

The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index.

Every item in a ndarray takes the same size as the block in the memory. Each element in ndarray is an object of the data-type object (called dtype).

## Installing NumPy library

```
$ pip install numpy
```

## Different Functions in NumPy Library

### Creating Single-Dimensional Array and Multi-Dimensional Array using NumPy

```
# Single Dimensional
1. import numpy as np
2. n1 = np.array([10,20,30,40])
3. n1
# Multi Dimensional
4. n2 = np.array([[10,20,30,40],[40,30,20,10]])
5. n2
```

### Initializing NumPy array with zeros

```
# Example 1
# Using np.zeros()
1. import numpy as np
# np.zeros((row,column))
2. n1 = np.zeros((1,2))
3. n1
# Example 2
4. n2 = np.zeros((5,5))
5. n2
```

### Initializing NumPy array with same number

```
# Example 1
# Using np.full()
1. import numpy as np
# np.full((row,column),Any number)
2. n1 = np.full((3,2),10)
3. n1
# Example 2
4. n2 = np.full((5,7),5)
5. n2
```

### Initializing NumPy array within a range

```
# Example 1
# Using np.arange()
1. import numpy as np
# np.arange(start_number,last_number)
2. n1 = np.arange(10,20)
3. n1
# Example 2
# np.arange(start_ number,last_number,Step)
# Step means printing number after certain gap number.
4. n2 = np.arange(10,50,5)
5. n2
```

### Initializing NumPy array with random numbers

```
# Generating specific Random number from starting index to ending index
# Example 1
# Using np.random.randint()
1. import numpy as np
# np.arange(From Start_Number,Till last_number, Array of numbers)
# Array of Numbers mean how much actual values you want in array.
2. n1 = np.random.randint(1,100,10)
3. n1
# Example 2
4. n2 = np.random.randint(10,50,8)
5. n2
```

### Checking the Shape of NumPy arrays

```
# Example 1
# Using np.shape()
1. import numpy as np
# np.array([array_1,array_2])
2. n1 = np.array([[1,2,3,4],[4,5,6,7]])
3. n1.shape
# n1.shape = (row, column)
4. n1.shape = (4,2)
5. n1.shape
```

### Joining NumPy Arrays

```
# Vertical Stack [Using vstack()]
# Stacking array elements vertically that is row wise.
# Example:
# array([[10,20,30],
# [40,40,60]])
1. import numpy as np
2. n1 = np.array([10,20,30])
3. n2 = np.array([40,50,60])
# np.vstack((array_1,array_2))
4. np.vstack((n1,n2))
# Horizontal Stack [Using hstack()]
# Stacking array elements horizontally that is column wise.
# Example:
# array([10,20,30,40,50,60])
# np.hstack((array_1,array_2))
5. np.hstack((n1,n2))
# Column Stack [Using column_stack()]
# Stacking elements column wise
# np.column_stack((n1,n2))
6. np.column_stack((n1,n2))
```

### NumPy Intersection & Difference

```
# Intersection using NumPy which means showing common elements from arrays.
1. import numpy as np
2. n1=np.array([10,20,30,40,50,60])
3. n2=np.array([50,60,70,80,90])
# np.intersect1d(array_1,array_2)
4. np.intersect1d(n1,n2)
# Difference using NumPy which means showing unique elements from arrays.
# np.setdiff1d(array_1,array_2) - Shows unique elements in array_1
5. np.setdiff1d(n1,n2)
# np.setdiff1d(array_2,array_1) - Shows unique elements in array_2
6. np.setdiff1d(n2,n1)
```

### NumPy Array Mathematics

```
# Addition of NumPy Arrays
1. import numpy as np
2. n1=np.array([10,20])
3. n2=np.array([30,40])
# Total of all elements present in array
4. np.sum([n1,n2])
# Adding elements row wise
5. np.sum([n1,n2],axis=0)
# Adding elements column wise
6. np.sum([n1,n2],axis=1)
# Basic Addition
7. n3=np.array([10,20,30])
# Adding 1 to each and every element of array
8. n3=n3+1
9. n3
# Basic Subtraction
# Subtracting 1 from each and every element of array
10. n3=n3-1
11. n3
# Basic Multiplication
# Multiple 2 with each and every element of array
12. n3=n3*2
13. n3
# Basic division
# Divide 2 by each and every element of array
14. n3=n3/2
15. n3
```

### NumPy Math Functions

```
# Mean
1. import numpy as np
2. n1=np.array([10,20,30,40,50,60])
3. np.mean(n1)
# Median
4. np.median(n1)
# Standard Deviation
5. np.std(n1)
```

### NumPy Save & Load

```
# Saving NumPy Array
1. import numpy as np
2. n1=np.array([10,20,30,40,50])
3. np.save('Your NumPy Array Name',n1)
# Loading NumPy Array
4. n2 = np.load('Previous Numpy Array Name')
5. n2
```

### Resources

So that's all for this Article Guysπ

## Top comments (0)