<jupyter_start><jupyter_text>Load in NumPy<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>The Basics<jupyter_code>a = np.array([1,2,3], dtype='int32')
print(a)
b = np.array([[9.0,8.0,7.0],[6.0,5.0,4.0]])
print(b)
# Get Dimension
a.ndim
# Get Shape
b.shape
# Get Type
a.dtype
# Get Size
a.itemsize
# Get total size
a.nbytes
# Get number of elements
a.size<jupyter_output><empty_output><jupyter_text>Accessing/Changing specific elements, rows, columns, etc<jupyter_code>a = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
print(a)
# Get a specific element [r, c]
a[1, 5]
# Get a specific row 
a[0, :]
# Get a specific column
a[:, 2]
# Getting a little more fancy [startindex:endindex:stepsize]
a[0, 1:-1:2]
a[1,5] = 20

a[:,2] = [1,2]
print(a)<jupyter_output>[[ 1  2  5  4  5  6  7]
 [ 8  9  5 11 12 20 14]]
[[ 1  2  1  4  5  6  7]
 [ 8  9  2 11 12 20 14]]<jupyter_text>*3-d example<jupyter_code>b = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print(b)
# Get specific element (work outside in)
b[0,1,1]
b<jupyter_output><empty_output><jupyter_text>Initializing Different Types of Arrays<jupyter_code># All 0s matrix
np.zeros((2,3))
# All 1s matrix
np.ones((4,2,2), dtype='int32')
# Any other number
np.full((2,2), 99)
# Any other number (full_like)
np.full_like(a, 4)
# Random decimal numbers
np.random.rand(4,2)
# Random Integer values
np.random.randint(-4,8, size=(3,3))
# The identity matrix
np.identity(5)
# Repeat an array
arr = np.array([[1,2,3]])
r1 = np.repeat(arr,3, axis=0)
print(r1)
output = np.ones((5,5))
print(output)

z = np.zeros((3,3))
z[1,1] = 9
print(z)

output[1:-1,1:-1] = z
print(output)<jupyter_output>[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]
[[0. 0. 0.]
 [0. 9. 0.]
 [0. 0. 0.]]
[[1. 1. 1. 1. 1.]
 [1. 0. 0. 0. 1.]
 [1. 0. 9. 0. 1.]
 [1. 0. 0. 0. 1.]
 [1. 1. 1. 1. 1.]]<jupyter_text>Be careful when copying arrays!!!<jupyter_code>a = np.array([1,2,3])
b = a.copy()
b[0] = 100

print(a)<jupyter_output>[1 2 3]<jupyter_text>Mathematics<jupyter_code>a = np.array([1,2,3,4])
print(a)
a + 2
a - 2
a * 2
a / 2
b = np.array([1,0,1,0])
a + b
a ** 2
# Take the sin
np.cos(a)


# For a lot more (https://docs.scipy.org/doc/numpy/reference/routines.math.html)<jupyter_output><empty_output><jupyter_text>Linear Algebra<jupyter_code>a = np.ones((2,3))
print(a)

b = np.full((3,2), 2)
print(b)

np.matmul(a,b)
# Find the determinant
c = np.identity(3)
np.linalg.det(c)<jupyter_output><empty_output><jupyter_text>Statistics<jupyter_code>stats = np.array([[1,2,3],[4,5,6]])
stats
np.min(stats)
np.max(stats, axis=1)
np.sum(stats, axis=0)<jupyter_output><empty_output><jupyter_text>Reorganizing Arrays<jupyter_code># Vertically stacking vectors
v1 = np.array([1,2,3,4])
v2 = np.array([5,6,7,8])

np.vstack([v1,v2,v1,v2])
# Horizontal  stack
h1 = np.ones((2,4))
h2 = np.zeros((2,2))

np.hstack((h1,h2))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21229817931982137, 'fraction_numerical': 0.08794228787358296, 'mean_word_length': 4.546666666666667, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '42992', 'license': 'MIT', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1410, 'n_words': 301, 'path': 'B2 - NLP/Madhav_NLP_Numpy.ipynb', 'repo_name': 'Shreyansh-Gupta/Open-contributions', 'size': '2911'}