-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpylib.py
More file actions
221 lines (195 loc) · 5.53 KB
/
numpylib.py
File metadata and controls
221 lines (195 loc) · 5.53 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Samples of numpy
import numpy as np
print(np.__version__)
a = np.array([1, 2, 3])
print("Prints the array", a)
print("Shape of the array:", a.shape)
print("Type of element:", a.dtype)
print("Bit size of element:", a.itemsize)
print("Dimension:", a.ndim)
print("Total elements:", a.size)
print("First element:", a[0])
a[0] = 10
print("First element (updated):", a[0])
b = a * np.array([2, 0, 2])
print("Multiply arrays (same dimension):", b)
print("Differents with native lists")
plist = [1, 2, 3]
array = np.array(plist)
print("Python List:", plist)
print("NP array:", array)
print("Appending elements")
plist.append(4)
array = np.append(array, 4)
print("Python List:", plist)
print("NP array:", array)
print("+ operator")
plist = [1, 2, 3]
array = np.array(plist)
plist = plist + [4]
array = array + [4]
print("Python List:", plist)
print("NP array:", array)
print("* operator")
plist = [1, 2, 3]
array = np.array(plist)
plist = plist * 4
array = array * 4
print("Python List:", plist)
print("NP array:", array)
print("+ operator")
plist = [1, 2, 3]
array = np.array(plist)
plist = plist + [4]
array = array + [4]
print("Python List:", plist)
print("NP array:", array)
print("Apply sqrt to each element")
array = np.array([1, 2, 3, 4])
print("NP array:", array)
array = np.sqrt(array)
print("NP array:", array)
print("DOT PRODUCT")
l1 = [1, 2, 3]
l2 = [4, 5, 6]
dotpy = 0
for i in range(len(l1)):
dotpy += l1[i] * l2[i]
print("In python:", dotpy)
np1 = np.array(l1)
np2 = np.array(l2)
dotnp = np.dot(np1, np2)
print("In NP (dot func):", dotnp)
dotnp = np1 * np2
dotnp = np.sum(dotnp)
print("In NP (using *):", dotnp)
dotnp = np1 @ np2
print("In NP (using @):", dotnp)
print("Multidimentional arrays:")
ma = np.array([[1, 2], [3, 4], [5, 6]])
print(ma, ma.shape)
print("first element: [row=0,col=0]", ma[0, 0])
print("slices")
print("All rows column 0", ma[:, 0])
print("All columns row 0", ma[0, :])
print("Transpose:", ma, ma.T)
bool_idx = ma > 2
print("Evaluate bool index(test elements)", bool_idx)
print("Getting elements with bool index:", ma[bool_idx])
ss = np.where(ma > 2, ma, -1)
print("Getting same size array with bool index(put -1 if false):", ss)
ma = np.array([10, 19, 30, 40, 61])
print(ma)
ima = [0, 2, 4]
print("getting elements using python list", ma[ima])
evens = np.argwhere(ma % 2 == 0).flatten()
print("Getting evens:", ma[evens])
ma = np.arange(1, 7)
print(ma)
mab = ma.reshape((2, 3))
print("Reshape in 2 rows and 3 cols:", mab)
print("Reshape using newaxis:")
mab = ma[np.newaxis, :]
print(mab, mab.shape)
mab = ma[:, np.newaxis]
print(mab, mab.shape)
print("Concatenation")
ma = np.array([[1, 2], [3, 4]])
print(ma)
mb = np.array([[5, 6], [7, 8]])
print(mb)
mc = np.concatenate((ma, mb))
print(mc)
mc = np.concatenate((ma, mb), axis=None)
print(mc)
mc = np.concatenate((ma, mb), axis=1)
print(mc)
mc = np.concatenate((ma, mb.T), axis=1)
print(mc)
print("hstack, vstack:")
ma = np.array([1, 2, 3])
mb = np.array([4, 5, 6])
print(np.hstack((ma, mb)))
print(np.vstack((ma, mb)))
print("BROADCASTING ( arrays of different shapes )")
ma = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
mb = np.array([1, 0, -1])
print(ma, "+", mb)
mc = ma + mb
print(mc)
print("Some DS common functions:")
ma = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9], [11, 12, 13, 14, 15, 16, 17, 18, 19]])
print(ma)
print("Calculates total sum:", ma.sum())
print("Calculates sum columns:", ma.sum(axis=0))
print("Calculates sum rows:", ma.sum(axis=1))
print("Calculates total mean:", ma.mean())
print("Calculates mean per col:", ma.mean(axis=0))
print("Calculates mean per row:", ma.mean(axis=1))
print("Calculates variants:", ma.var())
print("Calculates standard deviation:", ma.std())
print("Minumum:", ma.min())
print("Minumum row:", ma.min(axis=0))
print("Minumum cols:", ma.min(axis=1))
print("Maximum:", ma.max())
print("Maximum row:", ma.max(axis=0))
print("Maximum col:", ma.max(axis=1))
print("COPYING")
ma = np.array([1, 2, 3])
print(ma)
print("Using = (reference like pointers)")
mb = ma
mb[0] = 42
print(mb)
print(ma)
print("Using copy to duplicate")
ma = np.array([1, 2, 3])
mb = ma.copy()
print(mb)
print(ma)
print("Generating arrays")
mz = np.zeros((3, 3))
print("ZEROS (3,3):", mz)
mo = np.ones((3, 3))
print("ONES (3,3):", mo)
mc = np.full((3, 3), 8.0)
print("Custom (3,3):", mc)
print("IDENTITY MAT:", np.eye(3))
print("Arange:", np.arange(10))
print("With step:", np.arange(0, 10, 2))
print("Using linsapce to get a N elements equally spaced:", np.linspace(0, 10, 5))
print("RANDOM NUMBERS")
r = np.random.random((3, 2))
print("Between 0-1", r)
r = np.random.randn(3, 2)
print("Normal/Gaussian (mean=0, var=1)", r)
r = np.random.randint(3, 10, size=(2, 2))
print("Random integers with shape", r)
r = np.random.choice(5, size=10)
print("Random one dim array of size 10 with numbers between 0 and 5", r)
r = np.random.choice([-3, -6, 7], size=5)
print("Random one dim array of size 10 with numbers in list:", r)
print("Compare values")
a = np.array([[1, 2], [3, 4]])
b = np.array([[1, 2], [3, 4]])
print("Using == might cause errors:", a == b)
print("Correct is using allclose:", np.allclose(a, b))
print("LINEAR ALGEBRA MODULE")
a = np.array([[1, 2], [3, 4]])
eigenvalues, eigenvectors = np.linalg.eig(a)
print("EigenValues:", eigenvalues, "EigenVectors:", eigenvectors)
print("Solving x + y = 2200 and 1.5x + 4y = 5050")
print("Using inv and dot")
A = np.array([
[1, 1],
[1.5, 4.0]
])
b = np.array([2200, 5050])
x = np.linalg.inv(A).dot(b)
print(x)
print("Second way using solve:")
x = np.linalg.solve(A, b)
print(x)
print("Loading CSV")
data = np.loadtxt('sample.csv', delimiter=",", dtype=int)
print(data)