1.4. Working with NumPy#
import numpy as np
concatenate
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]]
stack
,
hstack
,
vstack
,
column_stack
# stack
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]]
# horizontal stack
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
# vertical stack
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
[[1 2 3]
[4 5 6]]
# column stack
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(np.column_stack((arr1, arr2)))
[[1 4]
[2 5]
[3 6]]
digitize
# create a masterscale with 10 rating classes
masterscale = np.linspace(0.1, 1, 10)
print(masterscale)
# create some ground truths and predictions
observed_labels = [1, 0, 0, 1, 0, 1]
predicted_labels = [0.1951, 0.01, 0.05, 0.7, 0.8, 0.9]
print(predicted_labels)
mapping = np.digitize(predicted_labels, masterscale, right=False)
print(mapping)
[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
[0.1951, 0.01, 0.05, 0.7, 0.8, 0.9]
[1 0 0 6 8 9]