Numpy

 

Numerical Python

  1. Why Numpy ?
  2. Numpy N-Dimensional Array Operations
    1. Array Structure
    2. Create an Array
    3. Index an Array
    4. Sort an Array
    5. Array Broadcasting
  3. Numpy Matrices Creation/Operations
  4. Numpy Vectors Creation/Operations
  5. Numpy File Read/Write
  6. Numpy Statistical Functions
Why Numpy ?
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Python programming language has four collection data types :

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.

Drawbacks:

  • Python native collection data types are good for storing small amounts of one-dimensional data.
  • But, can’t use directly with arithmetical operators (+, -, *, /, …).
  • Need efficient arrays with arithmetic and better multidimensional tools.

Numpy Features :

  • a powerful N-dimensional array object
  • advanced array slicing methods (to select array elements)
  • convenient array reshaping methods

and it even contains 3 libraries with numerical routines:

  • basic linear algebra functions
  • basic Fourier transforms
  • sophisticated random number capabilities

we cannot multiply two lists directly we will have to do by iterating element by element. Please see below python list example(without NumPy)

# Python program to demonstrate a need of NumPy 

list1 = [1, 2, 3, 4 ,5, 6]
list2 = [10, 9, 8, 7, 6, 5] 

# Multiplying both lists directly would give an error.
print(list1*list2)

The above problem can be easily solved using NumPy library.

# Python program to demonstrate the use of NumPy arrays
import numpy as np 

list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 9, 8, 7, 6, 5] 

# Convert list1 into a NumPy array
a1 = np.array(list1) 

# Convert list2 into a NumPy array
a2 = np.array(list2) 

print(a1*a2)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Array Structure

An array is basically nothing but pointers. It’s a combination of a memory address, a data type, a shape and strides:

  • The data pointer indicates the memory address of the first byte in the array,
  • The data type or dtype pointer describes the kind of elements that are contained within the array,
  • The shape indicates the shape of the array, and
  • The strides are the number of bytes that should be skipped in memory to go to the next element. If your strides are (10,1), you need to proceed one byte to get to the next column and 10 bytes to locate the next row.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Create an Array

To make a numpy array, you can just use the np.array() function.

All you need to do is pass a list to it and optionally, you can also specify the data type of the data.

Download a Python Numpy Array Creation/Manipulation Example

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Index an Array

Basic Slicing and indexing

Advanced indexing

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Array Broadcasting

https://machinelearningmastery.com/broadcasting-with-numpy-arrays/

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Draw a Plot using NumPy (Matplotlib)

Download a Python Numpy Plot Graph Generation Example

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Image processing using NumPy

Download a Python Numpy Image Processing Example