numpy or _numpy or Numeric?

A

auditory

I am a newbie here

I am trying to read "space separated floating point data" from file

I read about csv module by searching this group,
but I couldn't read space separated values with csv.
(which may be matter of course..)

I also read about numpy.fromfile(file, sep=' ') which i can use.
but on my machine(ubuntu linux) numpy is unknown module,
which I didn't install by myself.

While trying to install numpy accroding to its homepage.
(http://numpy.scipy.org/numpydoc/numdoc.htm).
i am quite confused.

it's talking about the Numerical Python,
and it says to test whether it is installed or not,
try import Numeric instead of numpy.

I got Nurmeric modules and
as a matter of fact i got a file named '_numpy.so' in lib directory.

I can import _numpy but _numpy does not have 'fromfile' method

My question is:
1. Do i need to install numpy module?
2. Then Is it different from Numeric module?
3. Then where can i get it?

4. Or what is general way to read 'space separated values' from file?

Thanks in advance.
 
G

George Sakkis

I am a newbie here

I am trying to read "space separated floating point data" from file

I read about csv module by searching this group,
but I couldn't read space separated values with csv.
(which may be matter of course..)

I also read about numpy.fromfile(file, sep=' ') which i can use.
but on my machine(ubuntu linux) numpy is unknown module,
which I didn't install by myself.

While trying to install numpy accroding to its homepage.
(http://numpy.scipy.org/numpydoc/numdoc.htm).
i am quite confused.

it's talking about the Numerical Python,
and it says to test whether it is installed or not,
try import Numeric instead of numpy.

I got Nurmeric modules and
as a matter of fact i got a file named '_numpy.so' in lib directory.

I can import _numpy but _numpy does not have 'fromfile' method

My question is:
1. Do i need to install numpy module?
2. Then Is it different from Numeric module?
3. Then where can i get it?

4. Or what is general way to read 'space separated values' from file?

Thanks in advance.

If *all* you need is to read a space-separated file with floating point
values, installing numpy (or Numeric or numarray..) is an overkill; you
can do it in one line in pure Python:

matrix = [map(float, line.split()) for line in
open('my_space_separated_file.txt')]

This stores the values as a list of lists, each list corresponding to a
row in the file. Depending on what you plan to do next with these
numbers, this may or may not be the best way to go about it, but since
you only mentioned the file reading part, we can't assume much.

George
 
T

Travis E. Oliphant

auditory said:
I am a newbie here

I am trying to read "space separated floating point data" from file

I read about csv module by searching this group,
but I couldn't read space separated values with csv.
(which may be matter of course..)

I also read about numpy.fromfile(file, sep=' ') which i can use.
but on my machine(ubuntu linux) numpy is unknown module,
which I didn't install by myself.

You will need to install NumPy.
While trying to install numpy accroding to its homepage.
(http://numpy.scipy.org/numpydoc/numdoc.htm).
i am quite confused.

You are reading old documentation for Numeric and so any installation
description is how to install the Numeric module (not its newer
replacement which is called NumPy).


So:

1) Yes, you need NumPy
2) This *is different* from Numeric
3) You get it by either installing a pre-built package for your system
or by

a) downloading the source tar-file from
http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103
(get the numpy-<version>.tar.gz file
b) tar zxvf numpy-<version>.tar.gz
c) cd numpy-<version>
d) sudo python setup.py install

e) If you want to link against high-performance libraries on your
system, then either put them in standard locations or edit the site.cfg
file appropriately (Optional).
4. Or what is general way to read 'space separated values' from file?

You can easily read space-separated values from a file by reading in a
line at a time and using the split method of strings:

fid = open('filename')
linedata = fid.readlines()
new = [[float(x) for x in line.split()] for line in linedata]

new will be a nested sequence of floats. You can convert it to an array
(if you want to do math on it) using

anew = numpy.array(new)

-Travis
 
A

auditory

George Sakkis ? ?:
I am a newbie here

I am trying to read "space separated floating point data" from file

I read about csv module by searching this group,
but I couldn't read space separated values with csv.
(which may be matter of course..)

I also read about numpy.fromfile(file, sep=' ') which i can use.
but on my machine(ubuntu linux) numpy is unknown module,
which I didn't install by myself.

While trying to install numpy accroding to its homepage.
(http://numpy.scipy.org/numpydoc/numdoc.htm).
i am quite confused.

it's talking about the Numerical Python,
and it says to test whether it is installed or not,
try import Numeric instead of numpy.

I got Nurmeric modules and
as a matter of fact i got a file named '_numpy.so' in lib directory.

I can import _numpy but _numpy does not have 'fromfile' method

My question is:
1. Do i need to install numpy module?
2. Then Is it different from Numeric module?
3. Then where can i get it?

4. Or what is general way to read 'space separated values' from file?

Thanks in advance.

If *all* you need is to read a space-separated file with floating point
values, installing numpy (or Numeric or numarray..) is an overkill; you
can do it in one line in pure Python:

matrix = [map(float, line.split()) for line in
open('my_space_separated_file.txt')]

This stores the values as a list of lists, each list corresponding to a
row in the file. Depending on what you plan to do next with these
numbers, this may or may not be the best way to go about it, but since
you only mentioned the file reading part, we can't assume much.

George
Thanks a lot for your 'elegant' suggestion.
As a next step i wish to do some math with matrix and produce a vector
and write it on a file. (in fact math is just averaging now)

I hope i can do this with a little more efforts.
 
A

auditory

Travis E. Oliphant ? ?:
auditory said:
I am a newbie here

I am trying to read "space separated floating point data" from file

I read about csv module by searching this group,
but I couldn't read space separated values with csv.
(which may be matter of course..)

I also read about numpy.fromfile(file, sep=' ') which i can use.
but on my machine(ubuntu linux) numpy is unknown module,
which I didn't install by myself.

You will need to install NumPy.
While trying to install numpy accroding to its homepage.
(http://numpy.scipy.org/numpydoc/numdoc.htm).
i am quite confused.

You are reading old documentation for Numeric and so any installation
description is how to install the Numeric module (not its newer
replacement which is called NumPy).


So:

1) Yes, you need NumPy
2) This *is different* from Numeric
3) You get it by either installing a pre-built package for your system
or by

a) downloading the source tar-file from
http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103

(get the numpy-<version>.tar.gz file
b) tar zxvf numpy-<version>.tar.gz
c) cd numpy-<version>
d) sudo python setup.py install

e) If you want to link against high-performance libraries on your
system, then either put them in standard locations or edit the site.cfg
file appropriately (Optional).
4. Or what is general way to read 'space separated values' from file?

You can easily read space-separated values from a file by reading in a
line at a time and using the split method of strings:

fid = open('filename')
linedata = fid.readlines()
new = [[float(x) for x in line.split()] for line in linedata]

new will be a nested sequence of floats. You can convert it to an array
(if you want to do math on it) using

anew = numpy.array(new)

-Travis
Thank you for quick answer..

I found the above website from googling with "numpy" keyword,
and supprised at that the top matching page is old one.

In addition to your method and below one,
I found csv moudule can do this with "delimiter" paramter.
I made mistake not to read manual first.
My apology on that.
 
T

Travis E. Oliphant

auditory said:
I found the above website from googling with "numpy" keyword,
and supprised at that the top matching page is old one.

The website is not the old one. http://numpy.scipy.org is correct.

But, the documentation is old. But, it is still useful (except for
Numeric specific information like how to install it). Therefore we
still make a link to it.

-Travis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top