0

I wanna print the index of the row containing the minimum element of the matrix

my matrix is matrix = [[22,33,44,55],[22,3,4,12],[34,6,4,5,8,2]]

and the code

matrix = [[22,33,44,55],[22,3,4,12],[34,6,4,5,8,2]]
a = np.array(matrix)
buff_min = matrix.argmin(axis = 0)

print(buff_min)   #index of the row containing the minimum element

min = np.array(matrix[buff_min])

print(str(min.min(axis=0)))  #print the minium of that row

print(min.argmin(axis = 0)) #index of the minimum

print(matrix[buff_min]) # print all row containing the minimum

after running, my result is

1
3
1
[22, 3, 4, 12]

the first number should be 2, because the minimum is 2 in the third list ([34,6,4,5,8,2]), but it returns 1. It returns 3 as minimum of the matrix. What's the error?

1
  • buff_min = matrix.argmin(axis = 0) ? I think no attribute for list as argmin Commented May 19, 2016 at 10:04

2 Answers 2

1

I am not sure which version of Python you are using, i tested it for Python 2.7 and 3.2 as mentioned your syntax for argmin is not correct, its should be in the format

import numpy as np
np.argmin(array_name,axis)

Next, Numpy knows about arrays of arbitrary objects, it's optimized for homogeneous arrays of numbers with fixed dimensions. If you really need arrays of arrays, better use a nested list. But depending on the intended use of your data, different data structures might be even better, e.g. a masked array if you have some invalid data points.

If you really want flexible Numpy arrays, use something like this:

np.array([[22,33,44,55],[22,3,4,12],[34,6,4,5,8,2]], dtype=object)

However this will create a one-dimensional array that stores references to lists, which means that you will lose most of the benefits of Numpy (vector processing, locality, slicing, etc.).

Also, to mention if you can resize your numpy array thing might work, i haven't tested it, but by the concept that should be an easy solution. But i will prefer use a nested list in this case of input matrix

Sign up to request clarification or add additional context in comments.

2 Comments

You're right 'bout fixed dimensions of arrays, I've got distracted. Anyway, i type buff_min = a.argmin(axis = 0) with "a" (that is now homogeneous array list) and it works without the array parameter of the method, too; your solution is clearer, but i don't understand if it is an error, because my enviroment doesn't return a warning when I run the code, and the result is what I expected
Yes Lorenzo, must be due to environmental based compiler difference, but that's weird. Anyways, you got your solution i guess.
0

Does this work?

np.where(a == a.min())[0][0]

Note that all rows of the matrix need to contain the same number of elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.