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.
>>> xarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>>> yarray([0, 1, 2])
>>> 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]])
array([[1, 2, 3, 0],
[4, 5, 6, 1],
[7, 8, 9, 2]])
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]])>>> xarray([[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)>>> dataarray([[1, 2, 3, 0], [4, 5, 6, 1], [7, 8, 9, 2]])