Numpy multidimensional array assigment unknown logic error

In summary, you are implementing a bubble sort on an array of ints, but it is not working correctly because you don't understand how multidimensional arrays work in Python.
  • #1
NotASmurf
150
2
I'm confused as to why this bubble sort I'm implementing is setting _all_ items to one of the items (I have no idea which one as array is too big)

the data type is of
Python:
...
[[1128 1026 1192 1023]]
[[ 771  195  858  196]]
[[ 953 1799  955 1738]]]

when I have an array of int, this same algorithm sorts it perfectly, I presume my issue is that I do not understand multidimensional numpy array element assigment, please explain why this breaks:

Python:
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)
#sort by y
if(0==0):
    for i in range(0,len(lines)):
        for j in range(0,len(lines)):
            if(lines[i][0][1]<lines[j][0][1]):
                cnt = lines[i][0]
                lines[i][0] = lines[j][0]
                lines[j][0] = cnt

now the array is

Python:
[[[ 738 1831  867 1831]]
...
[[ 738 1831  867 1831]]
[[ 738 1831  867 1831]]
[[ 738 1831  867 1831]]]

why?
 
Technology news on Phys.org
  • #2
NotASmurf said:
I'm confused as to why this bubble sort I'm implementing is setting _all_ items to one of the items (I have no idea which one as array is too big)

the data type is of
Python:
...
[[1128 1026 1192 1023]]
[[ 771  195  858  196]]
[[ 953 1799  955 1738]]]

when I have an array of int, this same algorithm sorts it perfectly, I presume my issue is that I do not understand multidimensional numpy array element assigment, please explain why this breaks:

Python:
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)
#sort by y
if(0==0):
    for i in range(0,len(lines)):
        for j in range(0,len(lines)):
            if(lines[i][0][1]<lines[j][0][1]):
                cnt = lines[i][0]
                lines[i][0] = lines[j][0]
                lines[j][0] = cnt

now the array is

Python:
[[[ 738 1831  867 1831]]
...
[[ 738 1831  867 1831]]
[[ 738 1831  867 1831]]
[[ 738 1831  867 1831]]]

why?
I think it's likely that you haven't defined your array correctly. See http://cs231n.github.io/python-numpy-tutorial/#numpy-arrays

Here's an example from the page in the link:
Python:
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
This is a two-dimensional array. In Python-speak, it's a list of lists. Notice that the elements in each list are separated by commas, and that the sublists themselves are also separated by commas.

When an arrays is printed, commas aren't used, but they need to be there when you define the array.
 

1. What is a multidimensional array in Numpy?

A multidimensional array in Numpy is a data structure that can hold multiple values of the same data type, organized in a grid-like format with rows and columns. It allows for efficient storage and manipulation of large amounts of data.

2. What does "assignment unknown logic error" mean in Numpy?

"Assignment unknown logic error" in Numpy refers to an error that occurs when trying to assign values to a multidimensional array using incorrect or undefined logic. This can happen when trying to access an element that does not exist or when trying to assign incompatible values to the array.

3. How do I troubleshoot a multidimensional array assignment unknown logic error?

To troubleshoot a multidimensional array assignment unknown logic error, start by checking the code for any syntax errors and making sure the array is properly initialized. Then, check if the indices used for assignment are within the array's dimensions and if the values being assigned are of the correct data type. You can also use print statements or a debugger to track the flow of the code and identify where the error is occurring.

4. Can I assign values to specific elements in a multidimensional array in Numpy?

Yes, you can assign values to specific elements in a multidimensional array in Numpy by using the array's indices. For example, to assign the value 5 to the element at index (0,0) of a 2D array, you would use the code: array[0,0] = 5. Just make sure the indices used are within the array's dimensions.

5. Are there any best practices for assigning values to a multidimensional array in Numpy?

Some best practices for assigning values to a multidimensional array in Numpy include using descriptive variable names, checking for errors and handling them appropriately, and using built-in Numpy functions for efficient assignment. It is also recommended to use vectorized operations instead of iterating through the array to improve performance.

Back
Top