27.numpy 3
import numpy as np
## Join
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr = np.concatenate((arr1, arr2))
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2))
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([5,6])
arr = np.concatenate((arr1, arr2))
print(arr)
(rows, columns) ---> (2,3) --> (0,1) --> axis = 0 --> rows axis = 1 --> columns
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2), axis =1)
print(arr)
# stack
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr = np.stack((arr1, arr2))
print(arr)
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr = np.stack((arr1, arr2), axis =1)
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr1, arr2))
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2), axis =1)
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2), axis =0)
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr2, arr1), axis =0)
print(arr)
arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr2, arr1), axis =0)
print(arr)
arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.hstack((arr1, arr2))
print(arr)
arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.vstack((arr1, arr2))
print(arr)
arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.stack((arr1, arr2))
print(arr)
arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.concatenate((arr1, arr2))
print(arr)
## Splitting
arr = np.array([1,2,3,4,5,6])
newarr = np.array_split(arr, 2)
print(newarr)
arr = np.array([1,2,3,4,5,6])
newarr = np.array_split(arr, 4)
print(newarr)
arr2 = np.array([[9,8],[7,6],[5,4],[3,2]])
newarr = np.array_split(arr2, 5)
print(newarr)
arr2 = np.array([[9,8],[7,6],[5,4],[3,2]])
newarr = np.array_split(arr2, 4, axis=1)
print(newarr)
arr3 = np.array([[9,8,7],[5,4,3]])
newarr = np.array_split(arr3, 4, axis=1)
print(newarr)
## Searching
arr = np.array([1,2,3,4,5,6])
new = np.where(arr == 4)
print(new)
arr = np.array([1,2,3,4,4,6])
new = np.where(arr == 4)
print(new)
arr = np.array([[1,2],[3,4]])
new = np.where(arr == 4)
print(new)
#(1,1)
arr = np.array([1,2,3,4])
new = np.searchsorted(arr, 3)
print(new)
arr = np.array([1,2,3,4])
new = np.searchsorted(arr, 6)
print(new)
arr = np.array([1,4,7,9])
new = np.searchsorted(arr, 6)
print(new)
arr = np.array([1,4,7,9])
new = np.searchsorted(arr, [6,3,8])
print(new)
arr = np.array([1,4,7,9])
new = np.searchsorted(arr, 7, side='right')
print(new)
arr = np.array([1, 4, 7, 9])
x = np.searchsorted(arr, 5, side='right')
print(x)
arr = np.array([6, 7, 8, 9])
x = np.searchsorted(arr, 7, side='right')
print(x)
# Sorting
arr = np.array([6, 10, 8, 12])
x = np.sort(arr)
print(x)
arr = np.array([[6, 10, 8, 12],[2,1,6,4]])
x = np.sort(arr)
print(x)
arr = np.array(["A","E","B"])
x = np.sort(arr)
print(x)
arr = np.array([True, False, True])
x = np.sort(arr)
print(x)
## Filtering
import numpy as np
arr = np.array([1,2,3,4,5,6])
x =[True, False, True, False, True, False]
newarr = arr[x]
print(newarr)
arr = np.array([1,2,3,4,5,6])
x = (arr%2 == 0)
newarr = arr[x]
print(newarr)
arr = np.array([[1,2,3],[4,5,6]])
x = (arr%2 == 0)
newarr = arr[x]
print(newarr)
arr = np.array([20,10,30,40,50])
x = (arr > 20)
newarr = arr[x]
print(newarr)
arr = np.array([[[20,10]],[[30,40]],[[50,5]]])
x = (arr > 20)
newarr = arr[x]
print(newarr)
arr = np.array([1,2,3,4,5,6])
x = []
for item in arr:
if item %2 == 0:
x.append(True)
else:
x.append(False)
newarr = arr[x]
print(newarr)
## Random
np.random.randint(0,10)
from numpy import random
random.randint(0,10)
random.rand() # 0 to 1
random.randn()
random.randint(0,10, size=(2,3))
random.randint(0,100, size=(2,3,5))
random.rand(2,3)
arr = np.array([3,6,7,1,34,56])
x = random.choice(arr)
print(x)
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)