Python - "cannot Perform Reduce With Flexible Type" When Trying To Use Numpy.mean
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
Solution 1:
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
Post a Comment for "Python - "cannot Perform Reduce With Flexible Type" When Trying To Use Numpy.mean"