[SciPy-User] Loadtxt vs genfromtxt

Bjorn Burr Nyberg bjorn.burr.nyberg at gmail.com
Thu Mar 1 11:58:22 EST 2012


Thanks that's exactly what I was looking for. Juraset.dat was saved by using require(gstat) data(jura) ...

Nyberg

Sent from my iPad

On 1. mars 2012, at 14:12, Warren Weckesser <warren.weckesser at enthought.com> wrote:

> 
> 
> On Thu, Mar 1, 2012 at 4:14 AM, Bjorn Burr Nyberg <bjorn.burr.nyberg at gmail.com> wrote:
> 
> Hi,
> I have a general question about loading data into numpy as I want to compare numpy and r by loading the juraset.dat ASCII file from the gstat package. Reading the support documents I have decided that it is better to use the loadtxt function as I do not have any missing data as useful by the genfromtxt function. However I receive this error when running loadtxt:
> 
> File ..... Numpy\lib\npyio.py, line 796, in loadtxt
> Items = [conv(Val) for (conv,val) in zip(converts,Vals)]
> ValueError: invalid literal for float()
> 
> Using the same parameters but with genfromtxt works, although the first entry of the array is Nan(not a numeric - expected a header like in a data frame of r). I suppose I was wondering if there was any way to save header data of an array whereby one could simply call that header for the data?Do I just have to remember the data associated with each column and call using data[]? Even loading the data as x,y,z = loadtxt is problematic when there are several columns associated with the data that I do not necessarily remember offhand.
> 
> Thanks for any advice and with your patience as I'm rather new to Numpy.
> Nyberg
> 
> 
> Bjorn,
> 
> I don't see the text file 'juraset.dat' in the gstat package (gstat_1.0-10), but google finds this:
>  http://www.ualberta.ca/~jbb/files/juraset.dat 
> 
> If that is your file, you can read it with genfromtxt like this:
> 
> 
> In [1]: data = genfromtxt('juraset.dat', skiprows=26, names=True)
> 
> In [2]: data[0]
> Out[2]: (2.386, 3.077, 3.0, 3.0, 1.74, 25.72, 77.36, 9.32, 38.32, 21.32, 92.56)
> 
> In [3]: data['Zn'][:3]
> Out[3]: array([ 92.56,  73.56,  64.8 ])
> 
> In [4]: data.dtype.names
> Out[4]: ('X', 'Y', 'Rock', 'Land', 'Cd', 'Cu', 'Pb', 'Co', 'Cr', 'Ni', 'Zn')
> 
> 
> The option 'names=True' tells genfromtxt to create a structured array, using the fields in first line (after skiprows) as the field names for the array.
> 
> Warren
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20120301/e29b42d1/attachment.html>


More information about the SciPy-User mailing list