# Understanding Numpy for Computer Vision

# What is Numpy Routine for computing complex array?

NumPy (Numerical Python) is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Numpy provides the powerful data structure known as n-d array and function to manipulate that n-d array .This data structure is used by other library to represent complex data such as images .

# Creating Array

# 1-d Array

import numpy as np

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"


x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)                         # Prints "int64"

1
2
3
4
5
6
7
8
9
10
11
12
13

# n-d array

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"

1
2
3
4

# Predefined Function

a = np.zeros((2,2))   # Create an array of all zeros
print(a)              # Prints "[[ 0.  0.]
       
b = np.ones((1,2))    # Create an array of all ones
print(b)              # Prints "[[ 1.  1.]]"

c = np.full((2,2), 7)  # Create a constant array
print(c)               # Prints "[[ 7.  7.]
                       #          [ 7.  7.]]"

d = np.eye(2)         # Create a 2x2 identity matrix
print(d)              # Prints "[[ 1.  0.]
                      #          [ 0.  1.]]"

e = np.random.random((2,2))  # Create an array filled with random values
print(e)                     # Might print "[[ 0.91940167  0.08143941]
                             #               [ 0.68744134  0.87236687]]"

x = np.arrange(6)
print(x)					# [0 1 2 3 4 5]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# Accessing Array

Slicing : Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
#  [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print(a[0, 1])   # Prints "2"
b[0, 0] = 77     # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])   # Prints "77"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Manipulation

# Dimensional

  1. reshape
  2. ravel
  3. flatten

# Joining

  1. stack
  2. dstack : depth stack
  3. hstack : horizontal stack on left side
  4. vstack : vertical stack on top of each other

# Spliting

  1. split

# Transforming

  1. flip
  2. fliplr : Plain flip with axis with -1
  3. flipud : Plain flip with axis with 0
  4. role : It perform shifting operation
  5. rot90 : rotate anticlok wise in 90 degree

# Bitwise

  1. bitwise_and
  2. bitwise_or
  3. bitwise_xor
  4. bitwise_not

# Statistical

  1. median
  2. average
  3. std
  4. var
  5. histogram