You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,6 +10,8 @@ By the end of this section you will be able to:
10
10
- Understand the difference between Python lists and NumPy arrays
11
11
- Add comments to your code
12
12
- Use `help()` to look up documentation
13
+
- Perform vectorized numerical operations
14
+
- Use logical conditions to select values
13
15
14
16
These are the building blocks for everything that follows.
15
17
@@ -32,12 +34,21 @@ print(x)
32
34
print(name)
33
35
```
34
36
35
-
In Jupyter notebooks, simply writing the variable name will also show its value:
37
+
In Jupyter notebooks, simply writing the variable name as the last statement of the cell will also show its value:
36
38
37
39
```python
38
40
x
39
41
```
40
42
43
+
What would happen if you have another statement after the variable name?
44
+
45
+
Try:
46
+
47
+
```python
48
+
x
49
+
a =14
50
+
```
51
+
41
52
---
42
53
43
54
# Comments
@@ -65,8 +76,8 @@ genes
65
76
You can access elements using square brackets `[]`:
66
77
67
78
```python
68
-
genes[0]
69
-
genes[1]
79
+
print(genes[0])
80
+
print(genes[1])
70
81
```
71
82
72
83
Python starts counting at **0**, not 1.
@@ -84,6 +95,20 @@ First import NumPy:
84
95
import numpy as np
85
96
```
86
97
98
+
This means:
99
+
- Import the `numpy` library
100
+
- Make it available as `np`
101
+
102
+
Later we can call functions like:
103
+
104
+
```python
105
+
np.array(x)
106
+
```
107
+
108
+
---
109
+
110
+
# Creating arrays
111
+
87
112
Create a numeric vector:
88
113
89
114
```python
@@ -124,7 +149,9 @@ arr + 1
124
149
```
125
150
126
151
This adds 1 to every element.
127
-
This is called **vectorized computation** and is extremely important for performance.
152
+
This is called **vectorized computation**.
153
+
154
+
It is much faster than Python loops because the operations are executed in optimized compiled code (mostly written in C) and work on entire blocks of memory at once.
128
155
129
156
---
130
157
@@ -157,13 +184,7 @@ np.linspace(0, 100, 11)
157
184
Python has built-in documentation.
158
185
159
186
```python
160
-
help(np.arange)
161
-
```
162
-
163
-
In Jupyter you can also use:
164
-
165
-
```python
166
-
np.arange?
187
+
help(np.arange) # or in Jupyter: ?np.arange
167
188
```
168
189
169
190
Look at:
@@ -172,7 +193,7 @@ Look at:
172
193
173
194
---
174
195
175
-
# Simple calculations
196
+
# Vectorized mathematics
176
197
177
198
Create a vector:
178
199
@@ -210,6 +231,119 @@ np.std(v)
210
231
211
232
---
212
233
234
+
## Mathematical background
235
+
236
+
For a vector with values:
237
+
238
+
\[
239
+
x_1, x_2, x_3, \dots, x_n
240
+
\]
241
+
242
+
### Mean
243
+
244
+
The **mean** (average) is:
245
+
246
+
\[
247
+
\mu = \frac{1}{n} \sum_{i=1}^{n} x_i
248
+
\]
249
+
250
+
This means:
251
+
- Add up all values
252
+
- Divide by the number of values
253
+
254
+
---
255
+
256
+
### Standard deviation
257
+
258
+
The **standard deviation** measures how much the values vary around the mean.
259
+
260
+
Population standard deviation:
261
+
262
+
\[
263
+
\sigma = \sqrt{
264
+
\frac{1}{n}
265
+
\sum_{i=1}^{n} (x_i - \mu)^2
266
+
}
267
+
\]
268
+
269
+
Steps:
270
+
1. Subtract the mean from each value
271
+
2. Square the differences
272
+
3. Compute the mean of those squared differences
273
+
4. Take the square root
274
+
275
+
---
276
+
277
+
# Logical comparisons
278
+
279
+
We can compare vectors to values:
280
+
281
+
```python
282
+
x = np.arange(1, 11)
283
+
284
+
print(x ==5)
285
+
print(x <5)
286
+
print(x >=5)
287
+
x !=5
288
+
```
289
+
290
+
These comparisons return **boolean arrays** (True / False for each element).
291
+
292
+
---
293
+
294
+
# Using logical results to select values
295
+
296
+
We can use boolean arrays to subset data:
297
+
298
+
```python
299
+
x[x <5]
300
+
x[x >=5]
301
+
x[x !=5]
302
+
```
303
+
304
+
This is a very common pattern in data analysis. But would that also work with a list? Try it!
0 commit comments