Misc

import numpy as np
# Values from 5 to 20
a = np.arange(5, 20)
print(a)
[ 5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
type(a)
numpy.ndarray
# Revere an array
b = a[::-1]
b
array([19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5])
c = np.array([[2, 3], [5, 89]])
d = c[::-1]
print(c)
[[ 2  3]
 [ 5 89]]
print(d)
[[ 5 89]
 [ 2  3]]
e = np.eye(5)
print(e)
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
# Random
f = np.random.random((3, 1))
print(f)
[[0.57782685]
 [0.13940557]
 [0.01241673]]
g = np.ones((4, 4))
print(g)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
i = np.pad(g, pad_width=3, mode='constant')
print(i)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
j = np.pad(g, pad_width=1, mode='constant', constant_values=2)
print(j)
[[2. 2. 2. 2. 2. 2.]
 [2. 1. 1. 1. 1. 2.]
 [2. 1. 1. 1. 1. 2.]
 [2. 1. 1. 1. 1. 2.]
 [2. 1. 1. 1. 1. 2.]
 [2. 2. 2. 2. 2. 2.]]
k = np.random.random(10)
print(k)
[0.90819857 0.85455489 0.28167892 0.88489143 0.54545762 0.15966174
 0.29479419 0.80940438 0.78830577 0.56276551]
# Array Sorting
k.sort()
print(k)
[0.07001264 0.13921812 0.47192512 0.57821869 0.64253229 0.68100415
 0.68742275 0.69785331 0.8750189  0.94492613]