Skip to content

Commit e8b4cf6

Browse files
Resolve docsstyle
1 parent ab7d07d commit e8b4cf6

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

numpy_questions.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,53 @@
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+
120
import numpy as np
221

322

423
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+
"""
643
if not isinstance(X, np.ndarray):
744
raise ValueError("The input is not a numpy array")
845
if X.ndim != 2:
946
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+
"""
1251
i = 0
1352
j = 0
1453
max_index = np.argmax(X)
@@ -18,6 +57,23 @@ def max_index(X):
1857

1958

2059
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+
"""
2177
# So if n_terms = 0 -> product = 1
2278
product = 1.0
2379
for n in range(1, n_terms + 1):

0 commit comments

Comments
 (0)