Python numpy – 01 – A quick reference


  1. “numpy” is the fundamental package for scientific computing in Python.
  2. Python library for creating N-Dimensional array
  3. Great support for Liner-Algebra, Statistics Distribution, Trignometery
  4. Aabhar : Jose Portilla (Head of Data Science at Pierian Training) @Udemy
  5. Creating an array in 3 ways, accessing via index, some major methods
  6. Broadcasting and how to prevent
  7. 1D np.array([1,2]) 2D np.array([ [1,2], [3,4] ]) 3D np.array([ [ [1,2], [3,4] ] ])
import numpy as np
# ####  Create numpy array using  
# A) Transforming standard python list
myList = [1,2,3]
myArray = np.array(myList)
print(type(myArray), "---", myArray)
# RES -> <class 'numpy.ndarray'> --- [1 2 3]

# B) Using Built-in functions
print(np.arange(0,11,2))   # o to 10 in steps of 2
# RES -> [ 0  2  4  6  8 10]
print(np.linspace(0,10,3)) # Evenly distributed 3 number bwn 0 to 10
# RES -> [ 0.  5. 10.]
print(np.zeros((1,5))) # 1D array with 5 zeroes, nD possible
# RES -> [[0. 0. 0. 0. 0.]]
print(np.ones(5)) #1D array with 5 ones, only 1D possible
# RES -> [1. 1. 1. 1. 1.]
print(np.eye(3))  #3x3 matrix with diagonal elements as 1
# RES -> [[1. 0. 0.]
#         [0. 1. 0.]
#         [0. 0. 1.]]

# C) By Generating random data
print(np.random.rand(2,3)) # 2x3 matrix with every number bwn o to 1
# RES -> [[0.08564555 0.5295177  0.82151728]
#         [0.83742842 0.96277555 0.84243966]]


print(np.random.randint(5,10,3)) # 3 integer no. bwn 5 to 10 
# RES -> [6 6 8]
print(np.random.randint(5,55, (2,5)))  # any 12 integers between 5 to 55 in 2x5 format
# RES -> [[35 46 49 16 14]
#         [53 43 16 51 23]]

#randn : "standard normal" distribution with mean = 0 and SD = 1
print(np.random.randn(1,5))
# RES -> [[ 0.95835337 -1.70240379 -0.9072812  -0.51675364  1.25098128]]

#############  FEW MAJOR METHODS #############
x = np.arange(5,11)
print(x)
# RES -> [ 5  6  7  8  9 10]
print(x.max(), x.min(), x.argmax(), x.argmin() )  #max and min with their position value
# RES -> 10 5 5 0

#1 D array with 6 elements to it
print("X shape : " , x.shape)   # shape is property where as max() is method
# RES -> X shape :  (6,)
#2 D array, with 3 elements for each 1D array
y = x.reshape(2,3)
print(y)
# RES -> [[ 5  6  7]
#         [ 8  9 10]]

print("Y Shape : ", y.shape)
# RES -> Y Shape :  (2, 3)

#############  INDEXING & SLICING #############
arr = np.arange(1,11)
print(arr)
# RES -> [ 1  2  3  4  5  6  7  8  9 10]
print(arr[5])
#  RES -> 6
print(arr[5:9])  #5th to 9th indexed value
# RES -> [6 7 8 9]
print(arr[5:]) # 5th till end
# RES -> [ 6  7  8  9 10]
print(arr[:5]) # From start till 5th
# RES -> [1 2 3 4 5]

#############  BROADCASTING #############
myArr = np.arange(11,66,11)
print(myArr)
# RES -> [11 22 33 44 55]
myArr[0:3] = 99 # Value is broadcasted to 1st 3 elements
print(myArr)
# RES -> [99 99 99 44 55]

myArr2 = myArr  # This myArr2 is basically a pointer
myArr2[0] = 0   # Modifying via pointer and hence original array will change
print(myArr)
# RES -> [ 0 99 99 44 55]

# To prevent, use copy() method
myArr3 = myArr.copy()
myArr3[0] = -1
print(myArr)
# RES -> [ 0 99 99 44 55]

########### BROADCASTING - Advantage of bool array #####

x = np.arange(1,7)
print(x)
#RES -> [1 2 3 4 5 6]
boolX = x>3
print(boolX)
# RES -> [False False False  True  True  True]
print(x[boolX])   # returns only those values > 3
# RES -> [4 5 6]

###########  2D array selection and indexing ###########
# Array of 1D arrays
arr2d = np.array([ [5,10,15], [20,25,30], [35,40,45]])
print(arr2d)
# RES -> [[ 5 10 15]
#         [20 25 30]
#         [35 40 45]]
print(arr2d[0]) # 1 row
# RES -> [ 5 10 15]
print(arr2d[0,2]) # 1st row last element - 15
# RES -> 15
print(arr2d[:2])  #Rows upto 2 excluding 2 means leave the 3rd row
# RES -> [[ 5 10 15]
#         [20 25 30]]
print(arr2d[:2, 1]) # Now form the extracted, 1st colum from all rows
# RES -> [10 25]
print(arr2d[:2, 1:]) # Now form the extracted, 1st and all the rest colum from all rows
# RES -> [[10 15]
#         [25 30]]
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s