Skip to content

Commit f498f5a

Browse files
committed
Numpy Exercise
1 parent c4b597d commit f498f5a

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

lectures/numpy.md

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ For a comprehensive list of what's available in NumPy see [this documentation](h
710710

711711
## Exercises
712712

713+
```{code-cell} ipython
714+
%matplotlib inline
715+
import matplotlib.pyplot as plt
716+
plt.rcParams['figure.figsize'] = (10,6)
717+
```
718+
713719
```{exercise-start}
714720
:label: np_ex1
715721
```
@@ -733,6 +739,35 @@ Now write a new function that does the same job, but uses NumPy arrays and array
733739
```{exercise-end}
734740
```
735741

742+
```{solution-start} np_ex1
743+
:class: dropdown
744+
```
745+
746+
This code does the job
747+
748+
```{code-cell} python3
749+
def p(x, coef):
750+
X = np.ones_like(coef)
751+
X[1:] = x
752+
y = np.cumprod(X) # y = [1, x, x**2,...]
753+
return coef @ y
754+
```
755+
756+
Let's test it
757+
758+
```{code-cell} python3
759+
x = 2
760+
coef = np.linspace(2, 4, 3)
761+
print(coef)
762+
print(p(x, coef))
763+
# For comparison
764+
q = np.poly1d(np.flip(coef))
765+
print(q(x))
766+
```
767+
768+
```{solution-end}
769+
```
770+
736771

737772
```{exercise-start}
738773
:label: np_ex2
@@ -784,56 +819,6 @@ If you can, write the method so that `draw(k)` returns `k` draws from `q`.
784819
```{exercise-end}
785820
```
786821

787-
788-
```{exercise}
789-
:label: np_ex3
790-
791-
Recall our {ref}`earlier discussion <oop_ex1>` of the empirical cumulative distribution function.
792-
793-
Your task is to
794-
795-
1. Make the `__call__` method more efficient using NumPy.
796-
1. Add a method that plots the ECDF over $[a, b]$, where $a$ and $b$ are method parameters.
797-
```
798-
799-
## Solutions
800-
801-
```{code-cell} ipython
802-
%matplotlib inline
803-
import matplotlib.pyplot as plt
804-
plt.rcParams['figure.figsize'] = (10,6)
805-
```
806-
807-
```{solution-start} np_ex1
808-
:class: dropdown
809-
```
810-
811-
This code does the job
812-
813-
```{code-cell} python3
814-
def p(x, coef):
815-
X = np.ones_like(coef)
816-
X[1:] = x
817-
y = np.cumprod(X) # y = [1, x, x**2,...]
818-
return coef @ y
819-
```
820-
821-
Let's test it
822-
823-
```{code-cell} python3
824-
x = 2
825-
coef = np.linspace(2, 4, 3)
826-
print(coef)
827-
print(p(x, coef))
828-
# For comparison
829-
q = np.poly1d(np.flip(coef))
830-
print(q(x))
831-
```
832-
833-
```{solution-end}
834-
```
835-
836-
837822
```{solution-start} np_ex2
838823
:class: dropdown
839824
```
@@ -899,6 +884,17 @@ using descriptors that behaves as we desire can be found
899884
```
900885

901886

887+
```{exercise}
888+
:label: np_ex3
889+
890+
Recall our {ref}`earlier discussion <oop_ex1>` of the empirical cumulative distribution function.
891+
892+
Your task is to
893+
894+
1. Make the `__call__` method more efficient using NumPy.
895+
1. Add a method that plots the ECDF over $[a, b]$, where $a$ and $b$ are method parameters.
896+
```
897+
902898
```{solution-start} np_ex3
903899
:class: dropdown
904900
```

0 commit comments

Comments
 (0)