Skip to content

Commit 7779cc8

Browse files
UP my solution
1 parent d31f7ef commit 7779cc8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

numpy_questions.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,38 @@
1515
This will be enforced with `flake8`. You can check that there is no flake8
1616
errors by calling `flake8` at the root of the repo.
1717
"""
18+
1819
import numpy as np
1920

2021

21-
def max_index(X):
22+
def max_index(x: np.ndarray) -> tuple:
2223
"""Return the index of the maximum in a numpy array.
2324
2425
Parameters
2526
----------
26-
X : ndarray of shape (n_samples, n_features)
27+
x : ndarray of shape (n_samples, n_features)
2728
The input array.
2829
2930
Returns
3031
-------
3132
(i, j) : tuple(int)
32-
The row and columnd index of the maximum.
33+
The row and column index of the maximum.
3334
3435
Raises
3536
------
3637
ValueError
3738
If the input is not a numpy array or
3839
if the shape is not 2D.
3940
"""
40-
i = 0
41-
j = 0
42-
43-
# TODO
41+
if not isinstance(x, np.ndarray):
42+
raise ValueError("Input must be a numpy array")
43+
if x.ndim != 2:
44+
raise ValueError("Input array must be 2D")
4445

46+
# index = x.argmax()
47+
# i = index // x.shape[1]
48+
# j = index % x.shape[1]
49+
i, j = np.unravel_index(np.argmax(x), x.shape)
4550
return i, j
4651

4752

@@ -64,4 +69,7 @@ def wallis_product(n_terms):
6469
"""
6570
# XXX : The n_terms is an int that corresponds to the number of
6671
# terms in the product. For example 10000.
67-
return 0.
72+
pi = 1.0
73+
for i in range(1, n_terms + 1):
74+
pi *= (4 * i**2) / (4 * i**2 - 1)
75+
return 2 * pi

0 commit comments

Comments
 (0)