forked from Nikhil22/netflix-style-recommender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonNumpyWarmUp.py
115 lines (59 loc) · 1.7 KB
/
PythonNumpyWarmUp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# coding: utf-8
# In[25]:
# Handy for matrix manipulations, likE dot product and tranpose
from numpy import *
# In[26]:
# Declare and initialize a 2d numpy array (just call it a matrix, for simplicity)
# This how we will be organizing our data. very simple, and easy to manipulate.
data = array([[1, 2, 3], [1, 2, 3]])
print data
# In[27]:
# Get dimensions of matrix
data.shape
# In[28]:
# Declare and initialize a matrix of zeros
zeros_matrix = zeros((1,2))
print zeros_matrix
# In[29]:
# Declare and initialize a matrix of ones
ones_matrix = ones((1,2))
print ones_matrix
# In[30]:
# Declare and initialize a matrix of random integers from 0-10
rand_matrix = random.randint(10, size = (10, 5))
print rand_matrix
# In[31]:
# Declare and initialize a column vector
col_vector = random.randint(10, size = (10, 1))
print col_vector
# In[32]:
# Access and print the first element of the column vector
print col_vector[0]
# In[33]:
# Change the first element of the column vector
col_vector[0] = 100
print col_vector
# In[34]:
# Access and print the first element of rand_matrix
print rand_matrix[0, 0]
# In[35]:
# Access and print the all rows of first column of rand_matrix
print rand_matrix[:, 0:1]
# In[36]:
# Access and print the all columns of first row of rand_matrix
print rand_matrix[0:1, :]
# In[37]:
# Access the 2nd, 3rd and 5th columns fo the first row rand_matrix
# Get the result in a 2d numpy array
cols = array([[1,2,3]])
print rand_matrix[0, cols]
# In[38]:
# Flatten a matrix
flattened = rand_matrix.T.flatten()
print flattened
# In[39]:
# Dot product
rand_matrix_2 = random.randint(10, size = (5,2))
dot_product = rand_matrix.dot(rand_matrix_2)
print dot_product
# In[ ]: