+5 votes
in Programming Languages by (77.8k points)

I have data and labels for a machine learning code. To save the data and labels in a TSV file, I want to add labels to the existing data as the last column. How can I do it?

E.g.

>>> x

array([[1, 2, 3],

       [4, 5, 6],

       [7, 8, 9]])

>>> y

array([0, 1, 2])

to

array([[1, 2, 3, 0],

       [4, 5, 6, 1],

       [7, 8, 9, 2]]) 

1 Answer

+1 vote
by (355k points)
selected by
 
Best answer

You need to first reshape the labels and then you can use the append() function to add the labels as the last column of the data.

Here is an example:

>>> x=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> x
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> y=np.array([0,1,2])
>>> y=y.reshape(3,1)
>>> data=np.append(x,y, axis=1)

>>> data
array([[1, 2, 3, 0],
       [4, 5, 6, 1],
       [7, 8, 9, 2]])

Related questions


...