python - Why does "numpy.mean" return 'inf'? -
i need calculate mean in columns of array more 1000 rows.
np.mean(some_array) gives me inf output
but pretty sure values ok. loading csv here data variable , column 'cement' "healthy" point of view.
in[254]:np.mean(data[:230]['cement']) out[254]:275.75 but if increment number of rows problem starts:
in [259]:np.mean(data[:237]['cement']) out[259]:inf but when @ data
in [261]:data[230:237]['cement'] out[261]: array([[ 425. ], [ 333. ], [ 250.25], [ 491. ], [ 160. ], [ 229.75], [ 338. ]], dtype=float16) i not find reason behaviour p.s happens in python 3.x using wakari (cloud based ipython)
numpy version '1.8.1'
i loading info with:
no_col=9 conv = lambda valstr: float(valstr.replace(',','.')) c={} in range(0,no_col,1): c[i] = conv data=np.genfromtxt(get_data,dtype=float16 , delimiter='\t', skip_header=0, names=true, converters=c)
i guess problem precision (as others have commented). quoting straight documentation mean() see
notes
the arithmetic mean sum of elements along axis divided number of elements.
note floating-point input, mean computed using same precision input has. depending on input data, can cause results inaccurate, float32 (see illustration below). specifying higher-precision accumulator using dtype keyword can alleviate issue.
since array of type float16 have limited precision. using dtype=np.float64 alleviate overflow. see examples in mean() documentation.
python numpy
No comments:
Post a Comment