0

I am working with a structured array defined in this way:

scores = np.empty((num_of_imgs, 4),
                               dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])

then in a for loop I populate it:

scores[i] = [id, bestT, bestR, bestP]

where all the vars inside the list are numpy arrays with shape (1,). However this line of code throws the aforementioned error. Why?

aretor
  • 2,379
  • 2
  • 22
  • 38

1 Answers1

4
  1. Your scores assignment is making a nx4 array of 4-tuples, which is an extra dimension bigger than you want, I think. It should be

    scores = np.empty(num_of_imgs,
        dtype=[('id', 'u4'), ('bestT', 'u8'), ('bestR', 'f8'), ('bestP', 'f8')])
    
  2. Then you're trying to assign a list to a tuple, which is throwing your c-contiguous error (numpy isn't as helpful at converting types for structured arrays as it is for ndarrays). Make the assignment a tuple. (using () instead of [])

    scores[i] = (id, bestT, bestR, bestP)
    
Daniel F
  • 13,620
  • 2
  • 29
  • 55