<jupyter_start><jupyter_text>Intro to NumpyThe following notebook uses a lot of materials (the whole theoretical part) from [the Numpy tutorial](https://nbviewer.jupyter.org/github/cgpotts/cs224u/blob/master/numpy_tutorial.ipynb) created by <NAME>, <NAME>, and <NAME> which is a part of the [Natual Language Understanding class](http://web.stanford.edu/class/cs224u/) from Stanford University.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Vectrors Initialization<jupyter_code>np.zeros(5)
np.ones(5)
# convert list to numpy array
np.array([1,2,3,4,5])
# convert numpy array to list
np.ones(5).tolist()
# one float => all floats
np.array([1.0,2,3,4,5])
# same as above
np.array([1,2,3,4,5], dtype='float')
# spaced values in interval
np.array([x for x in range(20) if x % 2 == 0])<jupyter_output><empty_output><jupyter_text>Just in case in you forgot the list comprehension in Python<jupyter_code># it returns a range object
range(20)
[x for x in range(20)]
[x for x in range(20) if x % 2 == 0]
np.array([x for x in range(20) if x % 2 == 0])
# same as above
np.arange(0,20,2)
# random floats in [0, 1)
np.random.random(10)
# random integers
np.random.randint(5, 15, size=10)<jupyter_output><empty_output><jupyter_text>Vector indexing<jupyter_code>x = np.array([10,20,30,40,50])
x[0]
# slice
x[0:2]
x[0:1000]
# last value
x[-1]
x[-0]
# last value as array
x[[-1]]
# last 3 values
x[-3:]
# pick indices
x[[0,2,4]]<jupyter_output><empty_output><jupyter_text>Vector assignmentBe careful when assigning arrays to new variables!<jupyter_code>#x2 = x # try this line instead
x2 = x.copy()
x2
x2[0] = 10

x2
x2[[1,2]] = 10

x2
x2[[3,4]] = [0, 1]

x2
# check if the original vector changed
x<jupyter_output><empty_output><jupyter_text>Vectorized operations<jupyter_code>x.sum()
x.mean()
x.max()
x.argmax()
np.log(x)
np.exp(x)
x + x  # Try also with *, -, /, etc.
x + 1<jupyter_output><empty_output><jupyter_text>Comparison with Python listsVectorizing your mathematical expressions can lead to __huge__ performance gains. The following example is meant to give you a sense for this. It compares applying `np.log` to each element of a list with 10 million values with the same operation done on a vector.<jupyter_code># log every value as list, one by one
def listlog(vals):
    return [np.log(y) for y in vals]
# get random vector
samp = np.random.random_sample(int(1e7))+1
samp
%time _ = np.log(samp)
%time _ = listlog(samp)<jupyter_output><empty_output><jupyter_text>MatricesThe matrix is the core object of machine learning implementations.   Matrix initialization<jupyter_code>np.array([[1,2,3], [4,5,6]])
np.array([[1,2,3], [4,5,6]], dtype='float')
np.zeros((3,5))
np.ones((3,5))
np.identity(3)
np.diag([1,2,3])<jupyter_output><empty_output><jupyter_text>Matrix indexing<jupyter_code>X = np.array([[1,2,3], [4,5,6]])
X
X[0]
X[0,0]
# get row
X[0, :]
# get column
X[ : , 0]
# get multiple columns
X[ : , [0,2]]<jupyter_output><empty_output><jupyter_text>Matrix assignment<jupyter_code># X2 = X # try this line instead
X2 = X.copy()

X2
X2[0,0] = 20

X2
X2[0] = 3

X2
X2[: , -1] = [5, 6]

X2
# check if original matrix changed
X<jupyter_output><empty_output><jupyter_text>Matrix reshaping<jupyter_code>z = np.arange(1, 7)

z
z.shape
Z = z.reshape(2,3)

Z
Z.shape
Z.reshape(6)
# same as above
Z.flatten()
# transpose
Z.T<jupyter_output><empty_output><jupyter_text>Numeric operations<jupyter_code>A = np.array(range(1,7), dtype='float').reshape(2,3)

A
B = np.array([1, 2, 3])<jupyter_output><empty_output><jupyter_text>You can read more about broadcasting [here](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html).<jupyter_code># not the same as A.dot(B)
A * B
A + B
A / B
# matrix multiplication
A.dot(B)
B.dot(A.T)
A.dot(A.T)
# outer product
# multiplying each element of first vector by each element of the second
np.outer(B, B)<jupyter_output><empty_output><jupyter_text>Exercises Task 1Initialize a vector of zeros of shape (5).You should get the following output:```array([0., 0., 0., 0., 0.])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 2Initialize a vector which contains a zero and negative odd numbers to -8 (inclusively). You should get the following output:```array([ 0, -2, -4, -6, -8])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 3Get the last 3 elements of the given vector.You should get the following output:```array([30, 40, 50])```<jupyter_code>x = np.array([10,20,30,40,50])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 4Find a mistake in the code.You should get the following output:```array([10, 30, 50])```<jupyter_code>x = np.array([10,20,30,40,50])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 5Modify the given vector so that you get the following output:```[  10    0   30 1000   50]```<jupyter_code>x = np.array([10,20,30,40,50])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 6Get the maximum number in the given list.You should get the following output:```50```<jupyter_code>x = np.array([10,20,30,40,50])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 7Initialize a matrix which looks like the following example.You should get the following output:```array([[5, 5, 5],       [1, 2, 3]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 8Initialize the same matrix as in the previous example but you data should be float numbers.You should get the following output:```array([[5., 5., 5.],       [1., 2., 3.]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 9Initialize a matrix of zeros which has 5 rows and 10 columns.You should get the following output:```array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 10Initialize an identity matrix with 5 rows.You should get the following output:```array([[1., 0., 0., 0., 0.],       [0., 1., 0., 0., 0.],       [0., 0., 1., 0., 0.],       [0., 0., 0., 1., 0.],       [0., 0., 0., 0., 1.]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 11Get the first column of the matrix X.You should get the following output:```array([1, 4, 7])```<jupyter_code>X = np.array([[1,2,3], [4,5,6], [7,8,9]])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 12Modify the matrix so that it looks like an example.You should get the following output:```[[ 1  2 10] [ 4  5 10] [ 7  8 10]]```<jupyter_code>X = np.array([[1,2,3], [4,5,6], [7,8,9]])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 13Get the values from first and the last columns of the second raw of matrix X.You should get the following output:```array([5, 8])```<jupyter_code>X = np.array([[1,2,3, 4], [5,6, 7, 8], [9, 10, 11, 12]])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 14Get the shape of vector x.You should get the following output:```(12,)```<jupyter_code>x = np.arange(1, 13)

# add your code here<jupyter_output><empty_output><jupyter_text>Task 15Create a matrix X from the vector x using reshaping.You should get the following output:```array([[ 1,  2],       [ 3,  4],       [ 5,  6],       [ 7,  8],       [ 9, 10],       [11, 12]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 16Flatten the matrix X from the previous task.You should get the following output:```array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 17Get a transponse of matrix X from Task 15.You should get the following output:```array([[ 1,  3,  5,  7,  9, 11],       [ 2,  4,  6,  8, 10, 12]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>Task 18Multiply each row of the matrix X by B.You should get the following output:```array([[ 1,  4],       [ 3,  8],       [ 5, 12],       [ 7, 16],       [ 9, 20],       [11, 24]])```<jupyter_code>B = np.array([1, 2])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 19Get a dot product of matrix X and B.You should get the following output:```array([[ 6,  9, 12],       [12, 19, 26],       [18, 29, 40],       [24, 39, 54],       [30, 49, 68],       [36, 59, 82]])```<jupyter_code>B = np.arange(6).reshape([2,3])

# add your code here<jupyter_output><empty_output><jupyter_text>Task 20Get a log for each number in matrix X.You should get the following output:```array([[0.        , 0.69314718],       [1.09861229, 1.38629436],       [1.60943791, 1.79175947],       [1.94591015, 2.07944154],       [2.19722458, 2.30258509],       [2.39789527, 2.48490665]])```<jupyter_code># add your code here<jupyter_output><empty_output><jupyter_text>You're done! Congratulations There is one bonus task which you could try to solve.  BonusCalculate Z and A:$$Z = XW+b$$$$A = tanh(z)$$You should get the following output for A:```array([[0.80265621, 0.91295317, 0.9033375 ],       [0.5570282 , 0.83529557, 0.6073829 ],       [0.51714154, 0.80740807, 0.63937959]])```<jupyter_code>X = [[0.74858744, 0.73163723], [0.39287332, 0.25377441], [0.6353768,  0.06158788]]
W = [[0.35263797, 0.13150443, 0.95494651], [0.73678507, 0.6127208,  0.93252179]]
b = [0.30299484, 0.9982467, 0.09294072]

Z = None # replace None with your code
A = None # replace None with your code

A<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup