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)
[1 2 3 4 5 6]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2))
print(arr)
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([5,6])
arr = np.concatenate((arr1, arr2))
print(arr)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-9a0eef0fd46b> in <module>
      1 arr1 = np.array([[1,2], [3,4]])
      2 arr2 = np.array([5,6])
----> 3 arr = np.concatenate((arr1, arr2))
      4 print(arr)

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

(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)
[[1 2 5 6]
 [3 4 7 8]]

# stack
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr = np.stack((arr1, arr2))
print(arr)
[[1 2 3]
 [4 5 6]]

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr = np.stack((arr1, arr2), axis =1)
print(arr)
[[1 4]
 [2 5]
 [3 6]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr1, arr2))
print(arr)
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2), axis =1)
print(arr)
[[1 2 5 6]
 [3 4 7 8]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr1, arr2), axis =0)
print(arr)
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.concatenate((arr2, arr1), axis =0)
print(arr)
[[5 6]
 [7 8]
 [1 2]
 [3 4]]

arr1 = np.array([[1,2], [3,4]])
arr2 = np.array([[5,6], [7,8]])
arr = np.stack((arr2, arr1), axis =0)
print(arr)
[[[5 6]
  [7 8]]

 [[1 2]
  [3 4]]]

arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.hstack((arr1, arr2))
print(arr)
[[11 12 15 16]
 [13 14 17 18]]

arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.vstack((arr1, arr2))
print(arr)
[[11 12]
 [13 14]
 [15 16]
 [17 18]]

arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.stack((arr1, arr2))
print(arr)
[[[11 12]
  [13 14]]

 [[15 16]
  [17 18]]]

arr1 = np.array([[11,12], [13,14]])
arr2 = np.array([[15,16], [17,18]])
arr = np.concatenate((arr1, arr2))
print(arr)
[[11 12]
 [13 14]
 [15 16]
 [17 18]]

## Splitting
arr = np.array([1,2,3,4,5,6])
newarr = np.array_split(arr, 2)
print(newarr)
[array([1, 2, 3]), array([4, 5, 6])]

arr = np.array([1,2,3,4,5,6])
newarr = np.array_split(arr, 4)
print(newarr)
[array([1, 2]), array([3, 4]), array([5]), array([6])]

arr2 = np.array([[9,8],[7,6],[5,4],[3,2]])
newarr = np.array_split(arr2, 5)
print(newarr)
[array([[9, 8]]), array([[7, 6]]), array([[5, 4]]), array([[3, 2]]), array([], shape=(0, 2), dtype=int64)]

arr2 = np.array([[9,8],[7,6],[5,4],[3,2]])
newarr = np.array_split(arr2, 4, axis=1)
print(newarr)
[array([[9],
       [7],
       [5],
       [3]]), array([[8],
       [6],
       [4],
       [2]]), array([], shape=(4, 0), dtype=int64), array([], shape=(4, 0), dtype=int64)]

arr3 = np.array([[9,8,7],[5,4,3]])
newarr = np.array_split(arr3, 4, axis=1)
print(newarr)
[array([[9],
       [5]]), array([[8],
       [4]]), array([[7],
       [3]]), array([], shape=(2, 0), dtype=int64)]

## Searching
arr = np.array([1,2,3,4,5,6])
new = np.where(arr == 4)
print(new)
(array([3]),)

arr = np.array([1,2,3,4,4,6])
new = np.where(arr == 4)
print(new)
(array([3, 4]),)

arr = np.array([[1,2],[3,4]])
new = np.where(arr == 4)
print(new)
#(1,1)
(array([1]), array([1]))

arr = np.array([1,2,3,4])
new = np.searchsorted(arr, 3)
print(new)
2

arr = np.array([1,2,3,4])
new = np.searchsorted(arr, 6)
print(new)
4

arr = np.array([1,4,7,9])
new = np.searchsorted(arr, 6)
print(new)
2

arr = np.array([1,4,7,9])
new = np.searchsorted(arr, [6,3,8])
print(new)
[2 1 3]

arr = np.array([1,4,7,9])
new = np.searchsorted(arr, 7, side='right')
print(new)
3

arr = np.array([1, 4, 7, 9])
x = np.searchsorted(arr, 5, side='right')
print(x)
2

arr = np.array([6, 7, 8, 9])
x = np.searchsorted(arr, 7, side='right')
print(x)
2

# Sorting
arr = np.array([6, 10, 8, 12])
x = np.sort(arr)
print(x)
[ 6  8 10 12]

arr = np.array([[6, 10, 8, 12],[2,1,6,4]])
x = np.sort(arr)
print(x)
[[ 6  8 10 12]
 [ 1  2  4  6]]

arr = np.array(["A","E","B"])
x = np.sort(arr)
print(x)
['A' 'B' 'E']

arr = np.array([True, False, True])
x = np.sort(arr)
print(x)
[False  True  True]

## 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)
[1 3 5]

arr = np.array([1,2,3,4,5,6])
x = (arr%2 == 0)

newarr = arr[x]
print(newarr)
[2 4 6]

arr = np.array([[1,2,3],[4,5,6]])
x = (arr%2 == 0)

newarr = arr[x]
print(newarr)
[2 4 6]

arr = np.array([20,10,30,40,50])
x = (arr > 20)

newarr = arr[x]
print(newarr)
[30 40 50]

arr = np.array([[[20,10]],[[30,40]],[[50,5]]])
x = (arr > 20)

newarr = arr[x]
print(newarr)
[30 40 50]

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)
[2 4 6]

## Random
np.random.randint(0,10)
4
from numpy import random
random.randint(0,10)
2
random.rand() # 0 to 1
0.3093733080482275
random.randn()
-0.5892962348314702
random.randint(0,10, size=(2,3))
array([[3, 8, 4],
       [5, 6, 4]])
random.randint(0,100, size=(2,3,5))
array([[[55,  4, 54, 11, 27],
        [98, 65, 17, 17, 78],
        [45, 39, 44, 28,  4]],

       [[18, 15, 92,  8, 67],
        [78, 81, 40,  4, 83],
        [17,  7, 33, 90,  3]]])
random.rand(2,3)
array([[0.03999663, 0.84650002, 0.87984641],
       [0.37996655, 0.64266493, 0.17751475]])
arr = np.array([3,6,7,1,34,56])
x = random.choice(arr)
print(x)
1

x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)
[[9 7 5 5 7]
 [9 3 3 9 9]
 [9 3 5 9 9]]