I want to read data from a .txt file and enter each column as a variable (say x_1, x_2, etc) Normally I use
x_1, x_2, x_3 = numpy.loadtxt('filename.txt', delimiter=',' , unpack = True)
Now the problem is each data file is going to have different number of columns. So I was looking for a way to create multiple variable names x_i
I tried using a dictionary with each key as an empty list in the following way:
features = dict(('x_%d' %i,[]) for i in range (1,n)) # n is specified by user
The problem is I want to convert these lists(x_1, x_2, etc into a matrix and carry out some matrix operations later. And I just cannot refer these keys as variables as in:
x_1.T # Transpose OR
x_2 - Y # both x_2 and Y are matrices
And I don't want to use something like features['x_3'] every time. Any suggestions?
update: Okay found out a way: create a list of matrices and then refer to them as x[o], x[1], etc. Even though x is a list, x[2] is a matrix.
Any better/smarter solution?
locals()method. It'd be okay for smaller scripts and applications but it gets unruly fast IMO.x = numpy.loadtxt(...)and thenx[0].T,x[1] - Y?