<jupyter_start><jupyter_text>Create "vectors" and "matrices" (really ND arrays)<jupyter_code>b = np.array([-13,9])            # 1D array construction
A = np.array([[4,-5], [-2,3]])   # 2D array contruction

b = np.ones(4)           # 1D array of ones
b = np.zeros(4)          # 1D array of zeros
b = np.random.randn(4)   # 1D array of random normal entries

A = np.ones((5,4))       # 2D array of all ones
A = np.zeros((5,4))      # 2D array of zeros
A = np.random.randn(5,4) # 2D array with random normal entries

I = np.eye(5)               # 2D identity matrix (2D array)
D = np.diag(np.random(5))   # 2D diagonal matrix (2D array)<jupyter_output><empty_output><jupyter_text>Select entries of A<jupyter_code>A[0,0]   # select single entry
A[0,:]   # select entire column
A[0:3,1] # slice indexing

# integer indexing
idx_int = np.array([0,1,2])
A[idx,3]

# boolean indexing
idx_bool = np.array([True, True, True, False, False])
A[idx,3]

# fancy indexing on two dimensions
idx_bool2 = np.array([True, False, True, True])
A[idx_bool, idx_bool2]     # not what you want
A[idx_bool,:][:,idx_bool2] # what you want<jupyter_output><empty_output><jupyter_text>Forming block arrays<jupyter_code>A = np.random.randn(5,4)
B = np.random.randn(2,4)
C = np.random.randn(5,2)
x = np.random.randn(x)
y = np.random.rand

np.vstack([A,B])       # A and B stacked vertically
np.hstack([A,C])       # A and C stacked horizontally
np.concatenate([x,y])  # concatenate 1D arrays<jupyter_output><empty_output><jupyter_text>Basic operations on arrays<jupyter_code>A = np.random.randn(5,4)
B = np.random.randn(5,4)
x = np.random.randn(4)
y = np.random.randn(5)

A+B          # matrix addition
A-B          # matrix subtraction

A*B          # ELEMENTWISE multiplication
A/B          # ELEMENTWISE division
A*x          # multiply columns by x
A*y[:,None]  # multiply rows by y (look this one up)

A.T          # transpose (just changes row/column ordering)
x.T          # does nothing (can't transpose 1D array)<jupyter_output><empty_output><jupyter_text>Matrix operators<jupyter_code>A = np.random.randn(5,4)
C = np.random.randn(4,3)
x = np.random.randn(4)
y = np.random.randn(5)
z = np.random.randn(4)

A.dot(C)       # matrix-matrix multiply (returns 2D array)
A.dot(x)       # matrix-vector multiply (returns 1D array)
x.dot(z)       # inner product (scalar)

A.T.dot(y)   # matrix-vector multiply
y.T.dot(A)   # same as above
y.dot(A)     # same as above
#A.dot(y)    # would throw error<jupyter_output><empty_output><jupyter_text>Solving linear systems<jupyter_code>b = np.array([-13,9])
A = np.array([[4,-5], [-2,3]])

np.linalg.inv(A)     # explicitly form inverse
np.linalg.solve(A, b) # return A^(-1)*b, more efficient and numerically stable<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14632379572618617, 'fraction_numerical': 0.027164070988772184, 'mean_word_length': 3.673434856175973, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '318711', 'license': 'MIT', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1072, 'n_words': 255, 'path': '2016/NumpyBasics.ipynb', 'repo_name': 'zeromtmu/practicaldatascience.github.io', 'size': '2761'}