ValueError: setting an array element with a sequence. according to data read from csv

I try to load data from csv line by line, and then create a 2d array from each line and save it inside the array:

loading:

with open('data_more.csv', newline='') as csvfile:
    data = list(csv.reader(csvfile))

parsing:

def getTrainingData():
    label_data = []
    for i in range( 0 , len(data) - 1):
        y = list(data[i][1:41:1])
        y = list(map(lambda x: list(map(lambda z: int(z),x)),y))
        y = create2Darray(y)
        label_data.append(y)
    labelY = np.array(label_data,dtype=float)

create2Darray func:

def create2Darray( arr ):
    final_arr = []
    index = 0
    while( index < len(arr)):
        temp = arr[index:index+4:1]
        final_arr.append(temp)
        index+=4
    return final_arr

This is a simple task, but I continue to count erro:

ValueError: setting an array element with a sequence.

I read that this is due to a situation where the shape of the elements is not the same. However, when I print the shape of all the elements inside labelY, it outputs the same shape.

What causes this problem? The problem occurs in this line.

labelY = np.array(label_data,dtype=float)

My csv has a format

number, number, number

basically N numbers per line, separated by "," thanks for the help.

+4
source share
2 answers

Start from the beginning:

  • , , . range(0, len(data)), range(0, len(data) - 1): , . , range(len(data)), Pythonic, do

    for y in data:
        y = y[1:41]
    
  • , , , 40 y . y[1:41] ( :1). , y[0:40] Pythonically y[:40]. , , .

  • y . , . ,

    y = [float(x) for x in y]
    

    y = list(map(float, y))
    

    , . , ? , , .

  • create2Darray, , 4n n-by-4. Python , range:

    def create2Darray(arr):
        return [arr[i:i + 4] for i in range(0, len(arr), 4)]
    
  • 2D 3D- label_data.append(y). - label_data 4D- . , . , for, .
  • , 4D- (, , 3D), numpy. , . № 3, . , dtype=np.float, int, .
  • getTrainingData!

TL; DR

, , 2D numpy.

with open('data_more.csv', newline='') as file:
    reader = csv.reader(file)
    data = [float(x) for x in line[1:] for line in reader]
data = np.array(data).reshape(data.shape[0], -1, 4)
+1

copy-n-paste :

In [367]: txt="""frame_video_02_0.jpg,126,37,147,112,100,41,126,116,79,34,96,92,
     ...: 68,31,77,88,1
     ...: """
In [368]: txt=txt.splitlines()
In [369]: data =np.genfromtxt(txt, delimiter=',')

data - 2d- :

In [370]: data.shape
Out[370]: (3, 401)
In [371]: data[0,:10]
Out[371]: array([ nan, 126.,  37., 147., 112., 100.,  41., 126., 116.,  79.])

nan, , . data = data[:, 1:]

:

In [373]: labels = np.genfromtxt(txt, delimiter=',', usecols=[0],dtype=None,encoding=None)
In [374]: labels
Out[374]: 
array(['frame_video_02_0.jpg', 'frame_video_02_50.jpg',
       'frame_video_02_100.jpg'], dtype='<U22')

, Python .

0

Source: https://habr.com/ru/post/1695716/


All Articles