Recarrays loops and headers??

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Recarrays loops and headers??

mdekauwe
Hi,

I have three problems which I can't quite seem to solve when using recarrays.

1). If I read some data from a csv file like so...

import numpy as np

# data look like...
#Col1 Col2 Col3 Col4 Fruit Month Day
#59.9541 2.1631 37.8446 3.5895 Plum Aug 1
#46.7951 1.8935 24.3321 2.0352 Plum Aug 5
#35.3719 1.8274 14.5228 1.3906 Pear Aug 8
#74.5296 1.9938 60.7940 4.2176 Pear Aug 9
data_all = np.recfromcsv("results.txt", delimiter=" ", names=True,
                                  case_sensitive=True)

# data look like....
# Col1 Col2 Col3 Col4 Fruit Month Day
# 59.9541 2.1631 37.8446 3.5895 Plum Aug 1
# 74.5296 1.9938 60.7940 4.2176 Pear Aug 9
data_25 = np.recfromcsv("data_at_25.txt", delimiter=" ", names=True,
                                  case_sensitive=True)

And then query data_25.shape

print data_25.shape
(2,)

it returns only the number of rows, rather than the number of columns and rows. Is there a way round this?! As this means I can't for example reshape the array can i?

2). If I wanted to check the index matching the smaller array and use the matching column value to perform a calculation with the larger array is there a better way than two loops? e.g.

vnorm = np.ones(len(data_all)) * -9999.
jnorm = np.ones(len(data_all)) * -9999.

for i in xrange(len(data_all)):
    for j in xrange(len(data_25)):
        if ((data_25["Fruit"][j] == data_all["Fruit"][i]) and
            (data_25["Month"][j] == data_all["Month"][i]) and
            (data_25["Day"][j] == data_all["Day"][i])):
           
            v25 = data_25["Col1"][j]
            j25 = data_25["Col2"][j]  
    vnorm[i] = data_all["Col1"][i] / v25
    jnorm[i] = data_all["Col2"][i] / j25

3). Is there a way to join the vnorm and jnorm to the data_all array to easily write it as a CSV file e.g. using

import matplotlib.mlab as mlab
data_all = np.hstack((data_all, vnorm))
f = open("crap.txt", "w")
mlab.rec2csv(data_all, f, delimiter=" ")
f.close()

I think this doesn't work because vnorm doesn't have a header?

return _nx.concatenate(map(atleast_1d,tup),1)
TypeError: invalid type promotion

The only way I seem to be able to get what I want is to explitly loop over the rows and columns again and print the outputs.

thanks,

Martin