10

I have a data file with only one line like:

 1.2  2.1  3.2

I used numpy version 1.3.0 loadtxt to load it

 a,b,c = loadtxt("data.dat", usecols(0,1,2), unpack=True)

The output was a float instead of array like

 a = 1.2

I expect it would be:

 a = array([1.2])

If i read a file with multiple lines, it's working.

3 Answers 3

5

Simply use the numpy's inbuit loadtxt parameter ndmin.

a,b,c=np.loadtxt('data.dat',ndmin=2,unpack=True)

output

a=[1.2]
Sign up to request clarification or add additional context in comments.

2 Comments

In a case like this you should submit an edit to the accepted answer, and clearly state that this is an update some 6 years later. Note that ndim was implemented in numpy 1.6 which is not the version used by OP.
this ndmin parameter saved my life, this is what I was looking for, for this kind of problem
2

What is happening is that when you load the array you obtain a monodimensional one. When you unpack it, it obtain a set of numbers, i.e. array without dimension. This is because when you unpack an array, it decrease it's number of dimension by one. starting with a monodimensional array, it boil down to a simple number.

If you test for the type of a, it is not a float, but a numpy.float, that has all the properties of an array but a void tuple as shape. So it is an array, just is not represented as one.

If what you need is a monodimensional array with just one element, the simplest way is to reshape your array before unpacking it:

#note the reshape function to transform the shape
a,b,c = loadtxt("text.txt").reshape((-1,1))

This gives you the expected result. What is happening is that whe reshaped it into a bidimensional array, so that when you unpack it, the number of dimensions go down to one.

EDIT:

If you need it to work normally for multidimensional array and to keep one-dimensional when you read onedimensional array, I thik that the best way is to read normally with loadtxt and reshape you arrays in a second phase, converting them to monodimensional if they are pure numbers

a,b,c = loadtxt("text.txt",unpack=True)
for e in [a,b,c]
    e.reshape(e.shape if e.shape else (-1,))

3 Comments

This gives me an error if i use it for the data files, with multiple lines
you just have to adjust the reshape for your data. But of you have a multidimensional matrix you should have no problem of non-dimensional array, right? maybe you should extend your question with more specification...
As, I have mentioned in the question, without reshape it's working for files with multiple lines. If i use reshape it's only working for the files with single line. I need a generic command, such that it reads both single line files and multiple line files and return the output as an array.
0

The simple way without using reshape is, to explicitly typecast the list

 a,b,c = loadtxt("data.dat", usecols(0,1,2), unpack=True)
 a,b,c = (a,b,c) if usi.shape else ([a], [b], [c])

This works faster than the reshape!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.