Numpy multidimensional array assigment unknown logic error

Click For Summary
SUMMARY

The forum discussion centers on a logic error encountered while implementing a bubble sort algorithm on a multidimensional NumPy array using OpenCV's HoughLinesP function. The user reports that all elements in the array are being set to the same value after sorting, indicating a misunderstanding of how element assignment works in multidimensional arrays. The issue arises from the way the array is manipulated during sorting, specifically when using references instead of creating copies of the elements.

PREREQUISITES
  • Understanding of NumPy multidimensional arrays
  • Familiarity with Python programming and syntax
  • Knowledge of sorting algorithms, particularly bubble sort
  • Experience with OpenCV functions, specifically HoughLinesP
NEXT STEPS
  • Review NumPy documentation on array slicing and element assignment
  • Learn about deep copying in Python using the copy module
  • Explore alternative sorting methods in NumPy, such as np.sort() and np.argsort()
  • Investigate the use of structured arrays in NumPy for better data handling
USEFUL FOR

Python developers, data scientists, and computer vision practitioners who are working with NumPy and OpenCV, particularly those dealing with multidimensional arrays and sorting algorithms.

NotASmurf
Messages
150
Reaction score
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
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.