Skip to content

Commit aca550a

Browse files
committed
up my solution
1 parent e344828 commit aca550a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

numpy_questions.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,23 @@ def max_index(X):
2929
Returns
3030
-------
3131
(i, j) : tuple(int)
32-
The row and columnd index of the maximum.
32+
The row and column index of the maximum.
3333
3434
Raises
3535
------
3636
ValueError
3737
If the input is not a numpy array or
3838
if the shape is not 2D.
3939
"""
40-
i = 0
41-
j = 0
42-
43-
# TODO
44-
X = np.array(X)
45-
rowMax = np.max(X, axis=1)
46-
maxValue = np.max(rowMax)
47-
indices = np.where(X == maxValue)
48-
i = indices[0][0]
49-
j = indices[1][0]
50-
51-
return i, j
40+
if not isinstance(X, np.ndarray):
41+
raise ValueError("The input must be a numpy array.")
42+
43+
if X.ndim != 2:
44+
raise ValueError("The input array must be 2D.")
45+
46+
max_idx = np.unravel_index(np.argmax(X, axis=None), X.shape)
47+
48+
return max_idx
5249

5350

5451
def wallis_product(n_terms):

0 commit comments

Comments
 (0)