NumPy

The fundamental package for scientific computing with Python

NumPy Official Website: https://numpy.org/



Descriptions for NumPy

  • Creator: Travis Oliphant(2005)
  • License: Open source
  • Expansion: Numerical Python
  • Purpose: Array Manipulator
  • Domains:Linear algebra, Fourier transform, Matrices

NumPy or Lists ?

  • NumPy arrays are stored at one continuous place in memory
  • NumPy is faster than lists and also it is optimized to work with latest CPU architectures.
---------------------------------------------------------------------------------------------------------------------------------------

# Checking NumPy Version

print(np.__version__)   

# Sample Array Creation

import numpy
a = numpy.array([111, 222, 333, 444, 555])
print(a)

# Python alias are an alternate name  (np)

import numpy as np
a = np.array([111, 222, 333, 444, 555])
print(a)

# 1-Dimesion to n-Dimension Array

a1 = np.array(42)                                                                     #0-D Arrays
a2 = np.array([1, 2, 3, 4, 5,6,7,8,9])                                                   #1-D Arrays
a3 = np.array([[1, 2, 3], [4, 5, 6]])                                         #2-D Arrays
a4 = np.array([ [[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]] ]) #3-D Arrays
a5 = np.array([1, 2, 3, 4], ndmin=5)                                     #n-D Arrays
print(a1,a2,a3,a4,a5)

# Access Elements or Indexing

print(a1)
print(a2[3])
print (a3[1,1])
print(a4[0, 2, 0])

# Negative Indexing

print(a3[0, -1])

# Shape of an Array

print(a1.shape,a2.shape,a3.shape,a4.shape,a5.shape)

# Reshape From 1-D to 2-D

na2 = a2.reshape(4, 3)
print(na2)

# Reshape From 1-D to 3-D

nna2 = a2.reshape(2, 3, 2)
print(nna2)

# Shuffling Arrays

random.shuffle(a2)
print(a2)

# Generating Permutation of Arrays

print(random.permutation(a2))

------------------------------------------------------------------------------------------------