<jupyter_start><jupyter_text>Get the most common values in an array<jupyter_code>a = np.array([20, 23, 21, 19, 23, 21, 27])
c = np.bincount(a)
c
np.argmax(c)<jupyter_output><empty_output><jupyter_text>Get the index array sorted by value<jupyter_code>a = np.array([[1, 4, 5, 7, 3, 2 ,2], [1, 4, 5, 7, 3, 2 ,1]])
y = np.array([1, 4, 5, 7, 3, 2 ,1])
s = np.argsort(a[1])[0:3]
s
y.shape, y.dtype
b = [ a[1, i] for i in s ]
b
# if y is an python array, error
y[s]<jupyter_output><empty_output><jupyter_text>L2 Norm<jupyter_code>x = np.random.randn(10, 4, 3, 2)
y = np.random.randn(10, 4, 3, 2)
x.shape, y.shape
np.sqrt(np.sum((x-y)**2))
np.linalg.norm(x-y)
squared_sum = np.sum((x-y)**2, axis=1)
squared_sum.shape
norm1 = np.sqrt(np.sum((x-y)**2, axis=1))
norm1.shape, norm1.dtype
norm2 = np.linalg.norm(x-y, axis=1)
norm2.shape, norm2.dtype
np.sqrt(np.sum(norm1- norm2)**2)
difference = np.linalg.norm(norm1 - norm2)
difference
squared_sum = np.zeros((3, 2))
for i in range(4):
    squared_sum += (x[0, i] - y[0, i])**2
np.sqrt(squared_sum)<jupyter_output><empty_output><jupyter_text>L2 Norm using broadcastUse this formula to compute the L2 norm for matrix \\(x\\) and \\(y\\): $$||x-y||^2 = ||x||^2 -2||xy||+||y||^2$$<jupyter_code>X = np.random.rand(5,4,3,2)
Y = np.random.rand(6,4,3,2)

# flat the array
X = np.reshape(X, (X.shape[0], -1))
Y = np.reshape(Y, (Y.shape[0], -1))

X.shape, Y.shape
num_X = X.shape[0]
num_Y = Y.shape[0]
num_X, num_Y
dists = np.zeros((num_X, num_Y))
dists.shape
sum_Y = np.sum(Y ** 2, axis=1)
sum_Y.shape
sum_YY = sum_Y.reshape(1, num_Y)
sum_YY.shape
dists += sum_YY
dists.shape
sum_X = np.sum(X ** 2, axis=1)
sum_X.shape
sum_XX = sum_X.reshape(num_X, 1)
sum_XX.shape
dists += sum_XX
dists.shape
dot_XY = 2 * np.dot(X, Y.T)
dot_XY.shape
dists -= dot_XY
dists.shape
dists = np.sqrt(dists)
dists<jupyter_output><empty_output><jupyter_text>Split an array and stack them partially<jupyter_code>x = np.random.rand(10, 2)
y = np.array_split(x, 3, axis=0)
x.shape
y[0].shape, y[1].shape, y[2].shape
x_0_2 = np.vstack((y[0], y[2]))
x_0_2.shape
x_0_2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20162059103908483, 'fraction_numerical': 0.055290753098188754, 'mean_word_length': 4.879551820728292, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '99608', 'license': 'Apache-2.0', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1018, 'n_words': 215, 'path': 'cs231n/numpy_test.ipynb', 'repo_name': 'Rockung/sma', 'size': '2098'}