1
+ """Assignment - using numpy and making a PR.
2
+
3
+ The goals of this assignment are:
4
+ * Use numpy in practice with two easy exercises.
5
+ * Use automated tools to validate the code (`pytest` and `flake8`)
6
+ * Submit a Pull-Request on github to practice `git`.
7
+
8
+ The two functions below are skeleton functions. The docstrings explain what
9
+ are the inputs, the outputs and the expected error. Fill the function to
10
+ complete the assignment. The code should be able to pass the test that we
11
+ wrote. To run the tests, use `pytest test_numpy_question.py` at the root of
12
+ the repo. It should say that 2 tests ran with success.
13
+
14
+ We also ask to respect the pep8 convention: https://pep8.org.
15
+ This will be enforced with `flake8`. You can check that there is no flake8
16
+ errors by calling `flake8` at the root of the repo.
17
+ """
18
+
19
+
1
20
import numpy as np
2
21
3
22
4
23
def max_index (X ):
5
- # Raises
24
+ """
25
+ Return the index of the maximum in a numpy array.
26
+
27
+ Parameters
28
+ ----------
29
+ X : ndarray of shape (n_samples, n_features)
30
+ The input array.
31
+
32
+ Returns
33
+ -------
34
+ (i, j) : tuple(int)
35
+ The row and columnd index of the maximum.
36
+
37
+ Raises
38
+ ------
39
+ ValueError
40
+ If the input is not a numpy array or
41
+ if the shape is not 2D.
42
+ """
6
43
if not isinstance (X , np .ndarray ):
7
44
raise ValueError ("The input is not a numpy array" )
8
45
if X .ndim != 2 :
9
46
raise ValueError ("The shape is not 2D" )
10
- """ Return the index of the maximum in a numpy array
11
- (The row and columnd index of the maximum)"""
47
+ """
48
+ Return the index of the maximum in a numpy array
49
+ (The row and columnd index of the maximum)
50
+ """
12
51
i = 0
13
52
j = 0
14
53
max_index = np .argmax (X )
@@ -18,6 +57,23 @@ def max_index(X):
18
57
19
58
20
59
def wallis_product (n_terms ):
60
+ """
61
+ Implement the Wallis product to compute an approximation of pi.
62
+
63
+ See:
64
+ https://en.wikipedia.org/wiki/Wallis_product
65
+
66
+ Parameters
67
+ ----------
68
+ n_terms : int
69
+ Number of steps in the Wallis product. Note that `n_terms=0` will
70
+ consider the product to be `1`.
71
+
72
+ Returns
73
+ -------
74
+ pi : float
75
+ The approximation of order `n_terms` of pi using the Wallis product.
76
+ """
21
77
# So if n_terms = 0 -> product = 1
22
78
product = 1.0
23
79
for n in range (1 , n_terms + 1 ):
0 commit comments