Home > OS >  Cannot convert numpy array to type: float from type : Object despite the data being of same dimensio
Cannot convert numpy array to type: float from type : Object despite the data being of same dimensio

Time:01-18

I am trying to pre-process data for LSMT layer input. The numpy array is formed from a 245 .npy files, in 72 folders each. Each .npy file has a 1D array of length 100. (This has been double checked).

The way I am reading data from each .npy file and appending it to an array is as follows:

for action in actions:
    for root, subdirectories, filenames in (sorted(os.walk(directory_2))):
        window = []      
        filenames = natsorted(filenames)
        for filename in range(sequence_length):
            res=np.load(os.path.join(directory_2, "{}.npy".format(filename)))
            window.append(res)
        sequences.append(window)
        labels.append(label_map[action])

Checking on the res, window and sequences data type I get the following

#Input print(res.dtype)  
#Output float64  

#Input type(window)  
#Output list  

#Input type(sequences)  
#Output list  

Checking the output of res to verify if the data has been read correctly from .npy file

#Input print(res)  
#Output [13. 14. 17. 17. 18. 19. 19. 20. 21. 21. 21. 21. 22. 22. 22. 22. 23. 23.
 23. 23. 23. 23. 25. 25. 25. 26. 26. 26. 26. 26. 26. 26. 27. 27. 27. 27.
 27. 28. 28. 28. 28. 28. 29. 29. 29. 29. 29. 30. 30. 30. 30. 30. 30. 30.
 30. 30. 30. 31. 31. 31. 31. 31. 32. 32. 32. 32. 33. 33. 33. 33. 33. 33.
 33. 33. 33. 34. 34. 34. 34. 34. 34. 34. 34. 34. 35. 35. 36. 36. 36. 36.
 36. 36. 36. 36. 37. 37. 37. 37. 38. 38.]  

#Input len(res)  
#Output 100  

I then go on to save the sequences is variable X as follows

X = np.array(sequences)  

Looking at X's output I get a dtype=object

#INPUT X.view()  
#OUTPUT  
array([[array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])],
       [array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])],
       [array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])],
       ...,
       [array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])],
       [array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])],
       [array([ 5.,  6.,  7.,  7.,  8.,  8.,  8.,  9.,  9.,  9.,  9., 10., 10.,
       10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 13., 14., 14.,
       14., 14., 14., 14., 14., 15., 15., 15., 15., 15., 15., 15., 15.,
       15., 15., 15., 15., 15., 15., 15., 16., 16., 16., 16., 16., 16.,
       16., 16., 17., 17., 17., 17., 17., 17., 17., 17., 17., 17., 18.,
       18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.,
       18., 18., 18., 19., 19., 19., 19., 19., 19., 19., 19., 19., 19.,
       19., 19., 20., 20., 20., 20., 20., 20., 20.]),
        array([[ 5.,  6.,  8.,  9., 10., 10., 11., 13., 13., 14., 17., 27., 27.,
        28., 30., 32., 33., 35., 35., 37., 41., 42., 43., 45., 45., 45.,
        45., 52.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),
        array([ 2.,  4.,  5.,  5.,  6.,  6.,  7.,  7.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10., 10.,
       10., 10., 10., 11., 11., 11., 11., 11., 11., 11., 11., 11., 12.,
       12., 12., 12., 12., 12., 12., 12., 13., 13., 13., 13., 13., 13.,
       13., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14.,
       14., 15., 15., 15., 15., 15., 15., 15., 15., 15., 16., 16., 16.,
       16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16., 16.,
       17., 17., 17., 17., 17., 17., 17., 17., 18.]),
        ...,
        array([14., 15., 15., 16., 16., 16., 16., 17., 17., 18., 18., 18., 18.,
       19., 19., 19., 20., 20., 21., 21., 21., 22., 22., 22., 22., 22.,
       23., 23., 23., 23., 23., 24., 24., 24., 24., 24., 24., 25., 25.,
       25., 25., 26., 26., 26., 26., 26., 26., 26., 27., 27., 27., 27.,
       27., 27., 28., 28., 28., 28., 28., 28., 28., 28., 28., 29., 29.,
       29., 29., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30., 31.,
       31., 31., 31., 31., 32., 32., 32., 32., 32., 32., 33., 33., 33.,
       33., 33., 34., 34., 34., 34., 34., 34., 34.]),
        array([ 4.,  5.,  5.,  5.,  6.,  6.,  6.,  6.,  7.,  7.,  7.,  7.,  7.,
        7.,  7.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,
        8.,  8.,  9.,  9.,  9.,  9.,  9.,  9.,  9., 10., 10., 10., 10.,
       10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 11.,
       11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11.,
       11., 11., 11., 11., 12., 12., 12., 12., 12., 12., 13., 13., 13.,
       13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 13., 14.,
       14., 14., 14., 14., 14., 14., 14., 14., 14.]),
        array([13., 14., 17., 17., 18., 19., 19., 20., 21., 21., 21., 21., 22.,
       22., 22., 22., 23., 23., 23., 23., 23., 23., 25., 25., 25., 26.,
       26., 26., 26., 26., 26., 26., 27., 27., 27., 27., 27., 28., 28.,
       28., 28., 28., 29., 29., 29., 29., 29., 30., 30., 30., 30., 30.,
       30., 30., 30., 30., 30., 31., 31., 31., 31., 31., 32., 32., 32.,
       32., 33., 33., 33., 33., 33., 33., 33., 33., 33., 34., 34., 34.,
       34., 34., 34., 34., 34., 34., 35., 35., 36., 36., 36., 36., 36.,
       36., 36., 36., 37., 37., 37., 37., 38., 38.])]], dtype=object)  

Now if I go ahead and check X's shape and type I get the following results

#Input X.shape  
#Output (72, 245)  

#Inpiut X.dtype  
#Output dtype('O')  

Here is when problems occur. I then try to convert X into float type using following methods but none of the methods listed on stackoverflow previous solutions work

1. #Input X=np.asarray(X)  
   #Output dtype remains as ('O')  
2. #Input X=X.astype('float64')  
   #Output ValueError: setting an array element with a sequence.  
3. #Input X=X.astype(int)  
   #Output ValueError: setting an array element with a sequence.  
4. #Input X=np.hstack(X)  
   #Output dtype remains as ('O')  
5. #Input X=np.vstack(X)  
   #Output dtype remains as ('O')  

Just in case anyone in interested to know how am I actually generating the data, following is the code. I am simply collecting keypoints using BF Matcher for different images and storing them as .npy files. This code also ensures that none of the .npy file saved is less or more than of length 100.

if len(matches) > MIN_MATCHES:
                frame = cv2.drawMatches(model, kp_model, frame, kp_frame, matches[:50], 0, flags=2)
                dist=[]
                for m in matches:
                    dist.append(m.distance)   
                dist_np = np.array(dist).flatten()
            else:
                print("Not enough matches found - %d/%d" % (len(matches), MIN_MATCHES))
                dist=[]
                for m in matches:
                    dist.append(m.distance)   
                dist_np = np.array(dist).flatten()

            array_len = np.array(dist_np).size
            max_len = 100
            if array_len > max_len:    
                dist_np = dist_np[:-(array_len - max_len)]

            else:
                dist_np = [np.pad(dist_np, (0, max_len - array_len), 'constant')]
            
            print(loop)
            print(filename)
            action = action = Path(filename).stem.split('_')[1]
            npy_path = os.path.join(DATA_PATH, action, str(loop))
            np.save(npy_path, dist_np)

CodePudding user response:

your data is not really of the same dimensions.

dist_np = [np.pad(dist_np, (0, max_len - array_len), 'constant')]

remove the list brackets, it makes the arrays, 1x100, instead of just 100, alternatively, just flatten the arrays as they are read from disk, by adding this after the line that reads it from disk.

res = res.flatten()
  •  Tags:  
  • Related