<jupyter_start><jupyter_text>References: [http://cs231n.github.io/python-numpy-tutorial/numpy-arrays]<jupyter_code>import numpy as np
A = np.array([1,2,3])
A
A = np.array([[1,2,3],[4,5,6]])
A<jupyter_output><empty_output><jupyter_text>Can be reshaped in multiples of 6 because there are 6 elements.<jupyter_code>A.reshape(-1)
A.reshape(3,2)<jupyter_output><empty_output><jupyter_text>Some attributes<jupyter_code>A.dtype
A.size
A.itemsize # In Bytes
A.shape<jupyter_output><empty_output><jupyter_text>flatten() function<jupyter_code>A.flatten()<jupyter_output><empty_output><jupyter_text>zeros() function<jupyter_code>np.zeros(10)<jupyter_output><empty_output><jupyter_text>ones() function<jupyter_code>np.ones(10)<jupyter_output><empty_output><jupyter_text>arange(start,step,stop) or arange(start,stop)<jupyter_code>np.arange(1,10)
np.arange(1,10,2)
np.arange(1,10,-2)<jupyter_output><empty_output><jupyter_text>arange(start,stop) if start==stop we'll get empty array<jupyter_code>np.arange(10,10)<jupyter_output><empty_output><jupyter_text>sum() function<jupyter_code>abc = np.array([[1,2,3],[1,2,3]])
np.sum(abc)<jupyter_output><empty_output><jupyter_text>Normal multiplication and dot() product<jupyter_code>A = np.array([1,2,3])
B = np.array([2,2,2])
A * B
np.dot(A,B)<jupyter_output><empty_output><jupyter_text>linspace() function -> Return evenly spaced numbers over a specified interval.<jupyter_code>np.linspace(0, 5, 10) # Jana 5 tak ha per numbers 10 create krnay hain<jupyter_output><empty_output><jupyter_text>diag() function<jupyter_code>np.diag([1,2,3])
np.diag((1,2,3))<jupyter_output><empty_output><jupyter_text>linalg library of numpy (Scipy extension)<jupyter_code>matrix = np.array([[4.3, 8.9],[2.2, 3.4]])
print(matrix)
np.linalg.eig(matrix) # 1st vector is of eigen values and other vectors are eigen vectors
np.linalg.det(matrix)<jupyter_output><empty_output><jupyter_text>full() function<jupyter_code>np.full((2,2), 7)<jupyter_output><empty_output><jupyter_text>eye() function -> Identity Function<jupyter_code>np.eye(4)<jupyter_output><empty_output><jupyter_text>random.random() function<jupyter_code>np.random.random((2,2))
np.random.random([3,3])
np.random.rand((2,2)) # In rand() only integer can be passed
np.random.rand(2)
np.random.randn(2) # Normally distributed values<jupyter_output><empty_output><jupyter_text>Indexing<jupyter_code>a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
a
a[:2,1:3] # rows from 0 to 1 and columns from 1 to 2
a[0,1]
a[:,1]
a[1,:]<jupyter_output><empty_output><jupyter_text>Boolean Indexing<jupyter_code>a = np.array([[1,2], [3, 4], [5, 6]])
a
boolean = a>2
boolean
a[boolean]
a
a[a>2]<jupyter_output><empty_output><jupyter_text>Broadcasting<jupyter_code>x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)   # Create an empty matrix with the same shape as x

for i in range(4):
    y[i, :] = x[i, :] + v

print(y)<jupyter_output>[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]
{'fraction_non_alphanumeric': 0.1765886287625418, 'fraction_numerical': 0.050501672240802674, 'mean_word_length': 8.838815789473685, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 80, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '128173', 'license': 'MIT', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1299, 'n_words': 195, 'path': '1/Part 3.ipynb', 'repo_name': 'HussamCheema/numpy', 'size': '2990'}