Newer
Older
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
{
"cells": [
{
"cell_type": "markdown",
"id": "conditional-vermont",
"metadata": {},
"source": [
"#### `==` vs `is`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "serial-chamber",
"metadata": {},
"outputs": [],
"source": [
"a = 1.0\n",
"b = 1.0\n",
"print(a == b)\n",
"print(a is b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "nominated-completion",
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 3]\n",
"b = [1, 2, 3]\n",
"print(a == b)\n",
"print(a is b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "vietnamese-needle",
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 3]\n",
"b = a\n",
"print(a == b)\n",
"print(a is b)\n",
"b[0] = 42\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"id": "random-dispute",
"metadata": {},
"source": [
"#### numpy copy vs view"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "higher-contamination",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "continental-panel",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10)\n",
"b = a[5] #copy numeric type\n",
"b = 42\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bronze-heather",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10)\n",
"b = a #alias\n",
"b[5] = 42\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "rational-thursday",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10)\n",
"b = a[5:10] #view\n",
"b[0] = 42\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "arranged-hello",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10)\n",
"b = np.array(a[5:10]) #copy array view\n",
"b[0] = 42\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "finite-copyright",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10)\n",
"b = a[[5, 6, 7, 8, 9]] #copy by advanced indexing\n",
"b[0] = 42\n",
"a"
]
},
{
"cell_type": "markdown",
"id": "endangered-throw",
"metadata": {},
"source": [
"### Give an equivalent python function for the following mathematical expressions\n",
"Vector and matrix variables are represented by numpy arrays.\n",
"Run the assert cells to check your solution."
]
},
{
"cell_type": "markdown",
"id": "dependent-crime",
"metadata": {},
"source": [
"$\\qquad \\mathbf{x}, \\mathbf{y} \\in \\mathbb{R}^N$\n",
"$\\qquad 0 \\leq n \\lt N$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "forbidden-ireland",
"metadata": {},
"outputs": [],
"source": [
" s = 0\n",
" for n in range(x.shape[0]):\n",
" s += x[n] * y[n]\n",
" return s"
]
},
{
"cell_type": "markdown",
"id": "sixth-works",
"metadata": {},
"source": [
"Which is equivalent to the dot product:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "needed-haiti",
"metadata": {},
"outputs": [],
"source": [
"# solution using the numpy dot product (more efficient and concise)\n",
"def f(x, y):\n",
" return x @ y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "referenced-siemens",
"metadata": {},
"outputs": [],
"source": [
"assert f(np.array([1, 2]), np.array([3, 4])) == 11"
]
},
{
"cell_type": "markdown",
"id": "rising-quantum",
"metadata": {},
"source": [
"$\\qquad \\mathbf{X} \\in \\mathbb{R}^{MxN}$\n",
"$\\quad \\mathbf{y} \\in \\mathbb{R}^{N}$\n",
"$\\quad \\mathbf{g}(\\mathbf{X}, \\mathbf{y}) \\in \\mathbb{R}^{M}$\n",
"$\\qquad 0 \\leq m \\lt M \\quad 0 \\leq n \\lt N$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "racial-retail",
"metadata": {},
"outputs": [],
"source": [
" s = np.zeros(X.shape[0])\n",
" for m in range(X.shape[0]):\n",
" for n in range(X.shape[1]):\n",
" s[m] += X[m, n] * y[n]\n",
" return s"
]
},
{
"cell_type": "markdown",
"id": "increasing-brief",
"metadata": {},
"source": [
"Which is again equivalent to the dot product:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "reliable-confusion",
"metadata": {},
"outputs": [],
"source": [
"# solution using the numpy dot product (more efficient and concise)\n",
"def g(X, y):\n",
" return X @ y"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "impressive-policy",
"metadata": {},
"outputs": [],
"source": [
"X = np.array([[0, 2, 4],\n",
" [1, 3, 5]])\n",
"assert (g(X, np.array([1, 2, 3])) == np.array([16, 22])).all()"
]
},
{
"cell_type": "markdown",
"id": "thrown-dimension",
"metadata": {},
"source": [
"$\\qquad 0 \\leq m \\lt M$\n",
"$\\qquad 0 \\leq n \\lt N$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "qualified-polish",
"metadata": {},
"outputs": [],
"source": [
"# solution using for loops\n",
"def phi(x, N):\n",
" phi = np.zeros((x.shape[0], N))\n",
" for m in range(phi.shape[0]):\n",
" for n in range(phi.shape[1]):\n",
" phi[m, n] = x[m]**n\n",
" return phi"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "monthly-lingerie",
"metadata": {},
"outputs": [],
"source": [
"# solution using numpy broadcasting (more efficient and concise) \n",
" return np.power(x[:, np.newaxis], np.arange(N))"
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "religious-quarter",
"metadata": {},
"outputs": [],
"source": [
"assert (phi(np.array([1, 2, 3]), 3) == np.array([[1, 1, 1],\n",
" [1, 2, 4],\n",
" [1, 3, 9]])).all()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}