0

I'm new to programming and have practically no grasp on terminology, so please excuse me for any basic questions I might have.

I'm trying to list an array of arrays.

I have written a function over a 2D array that lists the highest value in the array and the points where it occurs. These points (maxima) form arrays [i,j] which, for ease of display, I want to collect into a single numpy array or list max_p. To my knowledge, the most sensible way to do that is with numpy.append( max_p, [i,j] ). The issue with this is that it merges [i,j]) into the max_p array so that I get an array of single values instead of ordered pairs. So I decided to convert the whole thing into a list.

This worked well, for the most part- I got my list of ordered pairs I can print off in a single line.

However, the arrays in the big list max_pare not printed as, say [a,b]. They're printed as array([a,b]). This happens regardless of whether I use max_p.tolist() or list(max_p).

And, of course, since none of this is meaningful without the actual code, here it is:

def maxfinder_2D(array):
    maxima_array = numpy.array([]) # placeholder array
    for i in range(0, 422): # side note: learn how to get dim.s of 
                            # a multidimensional array
        x_array = array [i,:] # set array to be the i-th row
       # row_max = numpy.append(row_maxfinder_1D(x_array))
        maxima_array = numpy.append(maxima_array, numpy.array([maxfinder_1D(x_array)]))
            # We construct a new array, maxima_array, to list the 
            # maximum of every row in the plot.
            # The maximum, then, must be the maximum of this array.
    max_2D = maxfinder_1D(maxima_array)
    print("The maximum value in the image is: ", max_2D)
    global max_p
    max_p = []
    # This gives us the maximum value. Finding its location is another
    # step, though I do wish I could come up with a way of finding the
    # location and the maximum in a single step.
    for i in range(0,422): 
        for j in range(400): # the nested loop runs over the entire array
            val = img[i][j]
            if val == max_2D:
                max_pos_array = numpy.array([])
                max_pos_array = numpy.append(max_pos_array , i)
                max_pos_array = numpy.append(max_pos_array , j)
                list(max_pos_array)
                    #print(max_pos_array.tolist())
                max_p.append(max_pos_array)
    return max_2D
print("The function attains its maximum of ",maxfinder_2D(img)," on ", max_p)

And here is (part of) the output:

The maximum value in the image is: 255.0 The function attains its maximum of 255.0 on [array([200., 191.]), array([200., 192.]), array([201., 190.]), array([201., 193.]), array([202., 190.]), array([202., 193.]), array([203., 193.]), array([204., 191.]),...

I want the arrays to show up as simply, for example, [200. , 191.].

Why does this "artifact" occur? Does it have something to do with how numpy relates arrays to lists?

EDIT: As it turns out all I needed to do was to treat max_pos_array as a list as well, but I'm still curious about why, exactly, this happens.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
ygtozc
  • 3
  • 3
  • 1
    Because that's how `numpy.ndarray.__repr__` is defined... I'm not sure exactly what you are asking. – juanpa.arrivillaga Sep 17 '20 at 15:08
  • I have no idea what `.__repr__` even means. – ygtozc Sep 17 '20 at 16:40
  • 1
    When you `print(some_object)`, the actual `str` object that is being printed is the string returned by either `__str__` or `__repr__`. [See this question for the difference](https://stackoverflow.com/questions/1436703/difference-between-str-and-repr). In any case, the main point is, *because that's how it's designed to work*. Whoever designed `numpy` could have had `print(my_array)` print `banana` or `foo` if they *wanted*. – juanpa.arrivillaga Sep 17 '20 at 16:43
  • Thank you for the explanation. – ygtozc Sep 18 '20 at 17:17

2 Answers2

1

Python objects have repr and str display methods

In [1]: x = np.arange(3)
In [2]: x
Out[2]: array([0, 1, 2])
In [3]: repr(x)
Out[3]: 'array([0, 1, 2])'
In [4]: str(x)
Out[4]: '[0 1 2]'
In [5]: print(x)
[0 1 2]

For a list, they are the same, [,] for the list itself and repr for the elements:

In [6]: alist = [x]
In [7]: alist
Out[7]: [array([0, 1, 2])]
In [8]: repr(alist)
Out[8]: '[array([0, 1, 2])]'
In [9]: str(alist)
Out[9]: '[array([0, 1, 2])]'
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

Maybe you can try with a string representation of your array

print("The function attains its maximum of ",maxfinder_2D(img)," on ", np.array2string(max_p))
Ivan Calderon
  • 580
  • 6
  • 14