<jupyter_start><jupyter_text>NumPy Tutorial__By <NAME>__<jupyter_code># pip install numpy
import numpy as np<jupyter_output><empty_output><jupyter_text>The Basics<jupyter_code>a = [1, 2, 3]
print(a)
print(type(a))
a = np.array(a)
print(a)
print(type(a))
# We can also create arrays of different dimensions in numpy
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
print(type(b))
# we can see the dimension of any numpy array using the .shape command
print(b.shape)
a = np.array([1, 2, 3])
print(a.shape)<jupyter_output>(3,)<jupyter_text>Creating arrays using `Numpy.random` package<jupyter_code>np.random.seed(10)
r = np.random.randint(100, size=(4, 10))
print(r)
r2 = np.random.rand(3, 4)
print(r2)<jupyter_output>[[0.44524562 0.04426012 0.24749552 0.46513333]
 [0.88291734 0.1809206  0.35617448 0.3049914 ]
 [0.72609875 0.26811569 0.34519581 0.35372182]]<jupyter_text>Accessing Specific Elements<jupyter_code>r = np.random.randint(10, size=(3, 4))
print(r)
# accessing first row
print(r[0, :])
# accessing last row
print(r[-1, :])
# accessing specific row
row = 2
print(r[row - 1, ])
# accessing the first column
print(r[:, 0])
# accessing the first column and reshaping it into a column vector
c1 = r[:, 0]
print(c1)
c1 = c1.reshape((3, 1))
print(c1)
# accessing the last column
c2 = r[:, -1]
print(c2)
# reshaping the last column into column vector
c2 = c2.reshape((3, 1))
print(c2)
# retreiving specific rows
np.random.seed(10)
r = np.random.randint(low=0, high=100, size=(5, 6))
print(r)
print(r[[0, 1], :])
# printing selected rows
print(r[[0, 3], ])
# changing order of rows
print(r[[3, 0, 4], ])
# getting specific columns
print(r[:, [4, 2]])
# selecting a specific element (row, column)
print(r[0, 0])
row, column = 2, 3
print(r[row, column])<jupyter_output>88<jupyter_text>Getting elements using start, stop, step size (getting crazy 🙃)<jupyter_code>print(r)
print(r[::2, :])
print(r[::-1, ])
print(r[::-1, ::-1])
print(r[1:4:2, 0:5:3])
# 1, 3  |  0, 3<jupyter_output>[[29  0]
 [72 51]]<jupyter_text>Changing the Value of the matrices<jupyter_code>print(r)
#changing the value of a single element
r[0, 0] = 100
print(r)
r[0, :]
# changing the value of an entire row
r[0, :] = np.random.randint(low=-100, high=0, size=(1, 6))
print(r)
r[:, 2]
# changing the value of an entire column 
r[:, 2] = np.random.randint(low=-200, high=-100, size=(5,))
print(r)
# changing the value of some particular elements
r[1, [0, 2]] = 0
print(r)
k = r.copy()
k
# broadcasting
k[0, :] = 100
k
k[:, 0] = -100
k
k[1:4:2, [0, 4]] = -50
k
k[0:3:2, [3, 4]]<jupyter_output><empty_output><jupyter_text>Different Initialization Methods<jupyter_code># Creating the zeros Matrix
z = np.zeros((3, 3))
print(z)
z = np.zeros((3, 4))
print(z)
# creating matrix with data type defined as int
z = np.zeros((3, 4), dtype=np.int)
print(z)
print(z.dtype)
# creating all 1's matrix
o = np.ones((2, 4))
print(o)
o = np.ones((2, 4), dtype=np.int)
print(o)
# creating a matrix with some arbitrary number
f = np.full(shape=(3, 4), fill_value=-98)
print(f)
print(f.dtype)
f = np.full(shape=(1, 2), fill_value=2.0)
print(f)
print(f.dtype)
# The Identity Matrix
np.identity(3)
np.identity(5, dtype=np.int)
np.eye(3)<jupyter_output><empty_output><jupyter_text>Creating copy of Arrays<jupyter_code>a = np.array([1, 2, 3])
b = a
b[0] = 100
print(a)
a = np.array([1, 2, 3])
b = a.copy()
b[0] = 100
print(a)<jupyter_output>[1 2 3]<jupyter_text>Broadcasting in NumPy Arrays<jupyter_code>a = [1, 2, 3]
print(type(a))
print(a + 1)
a = np.array([1, 2, 3])
print(a + 1)
np.random.randint(low=0, high=100, size=(4, 5)) - 50<jupyter_output><empty_output><jupyter_text>Basic Mathematicall Operations<jupyter_code># Multilication with scalar
np.random.seed(10)
A = np.random.randint(0, 100, size=(3, 4))
print(A)
A * 0
4 * A
# Division with scalar
A / 2
A // 3
A / np.array([1, 2, 3, 4])
B = np.random.randint(1, 5, size=(3, 4))
B
A
A / B
A / np.random.randint(1, 5, size=(3, 4))<jupyter_output><empty_output><jupyter_text>Matrix Operations<jupyter_code>type(A)
A = np.random.randint(1, 20, size=(2, 3))
print(A)
B = np.random.randint(1, 20, size=(2, 3))
print(B)
A + B
A + 2 * B
A - B<jupyter_output><empty_output><jupyter_text>Element wise Operations<jupyter_code>print(A)
# element wise power operator
A ** 2
np.sin(A)
np.cos(A)
np.tan(A)
np.exp(A)<jupyter_output><empty_output><jupyter_text>Linear Algebra<jupyter_code># Taking the matrix dot products between 2 matrices
A = np.array([[1, 2]])
B = np.array([[5, 10, 0], [-9, 0, 3]])
print(A)
print(B)
A.shape
B.shape
R = A.dot(B)
print(R)
R.shape<jupyter_output><empty_output><jupyter_text>Important$dim A$ = ($n_1$, $m_1$)$dim B$ = ($n_2$, $m_2$)if $m_1 = n_2$$$(n_1, m_2)$$<jupyter_code>print(A.shape)
print(B.shape)
print(R.shape)
# Determinant of a Square Matrix
A = np.random.randint(-100, 100, size=(4, 4))
print(A)
print(np.linalg.det(A))
I = np.identity(4, dtype=np.int)
print(I)
print(np.linalg.det(I))
# Find the Trace of a Square Matrix
print(A)
print(np.trace(A))
print(np.trace(I))
# calculating the inverse of a matrix
np.linalg.inv(I)
A_inv = np.linalg.inv(A)
print(A_inv)
K = np.arange(9).reshape(3, 3)
K
np.linalg.det(K)
np.linalg.inv(K)
print(A.dot(A_inv))
print(A_inv.dot(A))
J = A.dot(A_inv)
print(J)
mask = np.array(J > 10 ** -10, dtype=np.int)
mask
mask2 = np.array([
    [1, 1, 1, 1],
    [0, 0, 0, 0],
    [0.5, 0.5, 0.5, 0.5],
    [0, 0, 0, 0]
])
mask2
A * mask2
mask * J
# thresholding
mask_3 = np.array(A > 10, dtype=np.int)
mask_3
A * mask_3
# finding the transpose of a matrix
B = np.arange(6).reshape(2, 3)
print(B)
print(B.shape)
print(B.T)
print(B.T.shape)
J = np.array([[1, 2,3, 4, 5]])
J
J.shape
J.T<jupyter_output><empty_output><jupyter_text>Statistics<jupyter_code>print(A)
# finding minimum in the matrix
np.min(A)
# finding minimum in every column vector
np.min(A, axis=0)
# finding the mimimum in every row vector
np.min(A, axis=1)
# finding maximum in entire matrix
print(np.max(A))
# finding maximum in every column vector 
np.max(A, axis=0)
# finding the maximum in every row vector
np.max(A, axis=1)
# sum of every element in the matrix
np.sum(A)
# sum of elements in every column vector
np.sum(A, axis=0)
# sum of elements in every row vector
np.sum(A, axis=1)
A.sum(axis=1)<jupyter_output><empty_output><jupyter_text>Boolean Operations on Arrays<jupyter_code>A = np.arange(35).reshape(5, 7)
print(A)
# equality check
A == 4
# comparison operator
A < 10
A >= 23
# converting a boolean array to integer array
(A < 20).astype(np.int)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17873059781773476, 'fraction_numerical': 0.0653142769325342, 'mean_word_length': 5.233716475095785, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 64, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '203340', 'license': 'MIT', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2824, 'n_words': 714, 'path': 'notebooks/numpy-tutorial/numpy-tutorial.ipynb', 'repo_name': 'anishLearnsToCode/ml-workshop-wac-2', 'size': '6515'}