import numpy as np
# Initalizing 1-D array
a1 = np.array([1, 2, 3])
print(a1)
# Initalizing 2-D array
a2 = np.array([[1,3,5], [2, 3, 4]])
print(a2)
# Initalizing an array full of 0's
a3 = np.zeros(shape=(3, 2))
print(a3)
# Initalizing an array full of 1's
a4 = np.ones(shape=(3, 2))
print(a4)
# Initalizing the identity matrix
a5 = np.eye(5)
print(a5)
# Getting the dimensions of an array
print(a5.ndim)
# Getting the dimensions of an array
b = np.array([1,2,3])
print(b.ndim)
# Getting the shape of an array
# First value is # of rows.
# Second value is # of columns.
print(a5.shape)
# Getting the shape of an array
a6 = np.array([1,2,3])
print(a6)
print(a6.shape)
# Getting the shape of an array
a7 = np.array([[1], [2], [3]])
print(a7)
print(a7.shape)
# Getting the number of elements of an array
a8 = np.array([[1, 4, 3], [2, 5, 3], [3, 9, 6]])
print(a8)
print(a8.size)
# Reshaping an array
a9 = np.array([[1, 4, 3], [2, 5, 3], [3, 9, 6], [-1, 2, 4]])
print(a9.reshape(2, 6))
# vstack & hstack
a10 = np.array([1, 2, 3])
a11 = np.array([4, 5, 6])
print(np.vstack((a10, a11)))
print(np.hstack((a10, a11)))
# vstack & hstack
print(np.vstack(([[1, 2, 3], [2, 3, 4]], [[6, 7, 8], [5, 2, 1]])))
print(np.hstack(([[1, 2, 3], [2, 3, 4]], [[6, 7, 8], [5, 2, 1]])))
a = np.array([1, 2, 3])
print(a[0], a[1], a[2])
a = np.array([[1, 2, 3]])
print(a[0], a[0][0], a[0][1], a[0][2])
print(a[0], a[0, 0], a[0, 1], a[0, 2]) # Another way of indexing
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a[0], a[0][0], a[0][1], a[0][2])
print(a[0], a[0, 0], a[0, 1], a[0, 2]) # Another way of indexing
print(a[1], a[1][0], a[1][1], a[1][2])
print(a[1], a[1, 0], a[1, 1], a[1, 2]) # Another way of indexing
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a[:, 0]) # Gets the first column
print(a[0, :]) # Gets the first row
a = np.array([[1, 2, 3], [4, 5, 6]])
# [0:1] Gets the first row
# 2 gets the second element
print(a[0:1, 2])
# [0:2] Gets both rows
# 2 gets the second element of each row
print(a[0:2, 2])
# [0:1] Gets the second row
# 0 gets the first element
print(a[1:2, 0])
'''
When you directly copy a numpy array, if you make any changes to either copy, it will also be reflected in the other one
'''
print("Example 1")
a = np.array([1,2,3])
b = a
print("Before changes a: ", a)
print("Before changes b: ", b)
b[0] = 3
print("After changes a: ", a) # Now is [3, 2, 3]
print("After changes b: ", b)
print()
print("Example 2")
a = np.array([1,2,3])
b = a
print("Before changes a: ", a)
print("Before changes b: ", b)
a[0] = 5
print("After changes a: ", a)
print("After changes b: ", b) # Now is [5, 2, 3]
'''
Use the copy function. Changes made in one won't affect the other
'''
print("Example 1")
a = np.array([1,2,3])
b = a.copy()
print("Before changes a: ", a)
print("Before changes b: ", b)
b[0] = 3
print("After changes a: ", a) # Still [1,2,3]
print("After changes b: ", b) # Changed to [3, 2, 3]
print()
print("Example 2")
a = np.array([1,2,3])
b = a.copy()
print("Before changes a: ", a)
print("Before changes b: ", b)
a[0] = 5
print("After changes a: ", a) # Changed to [5, 2, 3]
print("After changes b: ", b) # Still [1,2,3]
# Scalar Math
a = np.array([1, 2, 3])
print(a + 1)
print(a - 1)
print(a * 10)
print(a / 2)
print(a ** 2)
a = np.array([1, 2, 3])
b = np.array([5, 9, 4])
# Adding 2 matrices
print(a + b)
# Subtracting 2 matrices
print(a - b)
# Element-wise multiplying 2 matrices
print(a * b)
# Element-wise dividing 2 matrices
print(a / b)
# Element-wise power
print(a ** b)
a = np.array([1, 2, 3])
b = np.array([5, 9, 4])
# Getting the transpose of a
print(a.T)
# Matrix Multiply Method 1
print(np.matmul(a, b.T))
# Matrix Multiply Method 2
print(a @ b.T)
c = np.array([[1, 2], [3, 4]])
# Getting an inverse
print(np.linalg.inv(c))