Reputation: 1
I'd like to perform some dimension-reduction (DR) methods such as PCA, ICA and tSNE, maybe LEM on the a data set data.txt to compare the methods.
Therefore, I need to read in the data as a numpy.ndarray. Every line corresponds to a row in the matrix, with delimiter = ' '.
Alternatively, I have the file as a numpy.array now but as a string:
[ '16.72083152\t12.91868366\t14.37818919\n' ... '16.9504402\t7.81951173\t12.81342726']
How can I convert this quickly into a numpy.array of the desired format: n x 3, delimiter for rows = ' ', delimiter between elements in each row = '\t' chopping the '\n' at the end?
A quick answer much appreciated. Other tips as well. Thanks!
Upvotes: 0
Views: 53
Reputation: 775
This should do it
import numpy
try: from StringIO import StringIO
except ImportError: from io import StringIO
foo = '16.72083152\t12.91868366\t14.37818919\n16.9504402\t7.81951173\t12.81342726\n'
fn = StringIO.StringIO(foo) #make a file object from the string
data = numpy.loadtxt(fn) #use loadtxt with default settings.
Upvotes: 2
Reputation: 200
you could just try below code:
import numpy as np
data = np.loadtxt('data.txt',delimiter='\t')
Upvotes: 2