4
votes

I'm at my wit's end as I keep getting "cannot perform reduce with flexible type" when I try to compute the mean of a column, the file is read in just fine (no missing values in any rows/column) but when I put in the line: Brain_wt_mean = np.mean(ifile axis=0) then Python 2.7.5 does not like it. I am using this within the Spyder IDE. Thanks much for any help.

import os
import numpy as np

if __name__ == "__main__":
    try:        
        curr_dir = os.getcwd()
        file_path = curr_dir + '\\brainandbody.csv'
        ifile = np.loadtxt('brainandbody.csv', delimiter=',', skiprows=1, dtype=[('brainwt', 'f8'), ('bodywt', 'f8')])

    except IOError:
        print "The file does not exist, exiting gracefully"        

Brain_wt_mean = np.mean(ifile axis=0)




### BELOW is a sample of the csv file ######

Brain Weight    Body Weight
3.385   44.5
0.48    15.5
1.35    8.1
465 423
36.33   119.5
27.66   115
14.83   98.2
1.04    5.5
1
Did some commas not survive the copy-paste? Your sample isn't comma-delimited, but your loadtxt sets delimiter=',', and ifile axis=0 isn't valid PythonDSM

1 Answers

6
votes

When you're working with structured arrays like that you lose some of the flexibility you'd otherwise have. You can take the mean after selecting the appropriate piece, though:

>>> ifile
array([(3.385, 44.5), (0.48, 15.5), (1.35, 8.1), (465.0, 423.0),
       (36.33, 119.5), (27.66, 115.0), (14.83, 98.2), (1.04, 5.5)], 
      dtype=[('brainwt', '<f8'), ('bodywt', '<f8')])
>>> ifile["brainwt"].mean()
68.759375000000006
>>> ifile["bodywt"].mean()
103.66249999999999

I use numpy almost every day, but when working with data of the sort where I want to name columns, I think the pandas library makes things much more convenient, and it interoperates very well. It's worth a look. Example:

>>> import pandas as pd
>>> df = pd.read_csv("brainandbody.csv", skipinitialspace=True)
>>> df
   Brain Weight  Body Weight
0         3.385         44.5
1         0.480         15.5
2         1.350          8.1
3       465.000        423.0
4        36.330        119.5
5        27.660        115.0
6        14.830         98.2
7         1.040          5.5
>>> df.mean()
Brain Weight     68.759375
Body Weight     103.662500
dtype: float64