-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numpy_Chap_4.py
133 lines (83 loc) · 3.1 KB
/
Numpy_Chap_4.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Array-oriented programming with Arrays
# The objective is to calculate euclidian distance between all possible points in xs and ys
import numpy as np
points = np.arange(0, 1, 0.01)
xs, ys = np.meshgrid(points, points)
z = np.sqrt(xs ** 2 + ys ** 2)
import matplotlib.pyplot as plt
plt.imshow(z, cmap=plt.cm.gray)
plt.colorbar()
xarr = np.arange(1.1, 1.6, 0.1)
yarr = np.arange(2.1, 2.6, 0.1)
cond = np.array([True, False, True, True, False])
# the following 2 are the same, but I actually want something else
result = [(x if c else y) for x in xarr for y in yarr for c in cond]
for i in range(5):
for j in range(3):
for k in range(4):
print('i = {}, j = {}, k = {}\n'.format(i, j, k) + 20 * '-')
result = [(x if c else y) for x, y, c in zip(xarr, yarr, cond)]
# with np.where, things are much faster and concise.i
result_w = np.where(cond, xarr, yarr)
arr = np.random.randn(4, 4)
np.where(arr > 0, 2, -2)
# or we can combine scalars and arrays when using np.where
arr = np.random.randn(4, 4)
print(np.where(arr < 0, 2, arr))
# Mathematical and Statistical Methods
arr = np.arange(15).reshape(3, 5)
# lets compute the mean of each row.
# rows are axis = 0, but that will actually give us an array with means
# of each column.
print(arr.mean(axis=0))
# the trick is that when we tell numpy to calculate
# mean given axis=0, it looks at the array as combination of raws. if
# instead, we passed axis=1, it would look at it as a cominations of
# columns. The reason this is important is because numpy does everything
# element-wise. That is, if I need the mean of the raws and it functions
# element-wise, than I have to tell it too look at the array as a combi-
# nation of columns, which will enable it to operate element-wise upon
# columns and give me the desired outcome.
print(arr.mean(axis=1))
# Sorting
# sorting can be done in place with .sort() method.
arr = np.random.randn(6)
arr.sort()
# Multidimentional
arr = np.random.randn(5, 3)
# if I want to sort across rows, I would pass the axis = 1
arr.sort(axis=1)
# Again, the same logic works, as before. We need to keep in mind
# the elementwise philosophy of NumPy.
# These methods are mutating.
# The top level functions such as np.sort() are not mutating and
# returne a copy of the array
# Linear Algebra
from numpy.linalg import inv, qr
X = np.random.randn(5, 5)
mat = X.T.dot(X)
print(mat)
print(inv(mat))
print(mat.dot(inv(mat)))
# QR decomposition of a matrix
q, r = qr(mat)
# the function dieg does 2 things (it is actially in numpy, not in numpy.linalg)
# #1 if the argument is a 2 dimentional array (matix), than it returns a 1D array
# containing the diagonal
arr = np.arange(16).reshape(4, 4)
print('the diagonal is: ', np.diag(arr))
# #2 if the argument is a 1D, than it constructs a matrix, with the argument as the
# main diagonal, and the rest is zero.
new_arr = np.diag(np.array([1, 2, 3, 4]))
print(new_arr)
# Other comman functions of numpy.linalg are
# dot
# trace
# det
# eig
# inv
# pinv
# qr
# svd
# solve - solve the linare system Ax =b, where A is a square matrix
# lstsq - Compute the least square solution to Ax = b