Dealing with 'NaN' values in lists

i like this post (click again to cancel)
0
i dont like this post (click again to cancel)

Hello!

Maybe somebody here has an experience on working with 'NaN' values in Sage.

My situation is the following: I need to load some experimentally measured data into a list of float values. But unfortunately, the experimental data set is not "solid" - it has 'NaN' values somewhere inside.

What I currently need to perform first of all is to calculate the average value of a list slice. Preferably, simply using 'mean()' if it is possible.

So could somebody please give me an advice how to work with such arrays correctly?

Thank you.

asked Jul 31 '12

v_2e gravatar image v_2e flag of Ukraine
219 1 10 24
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel) v_2e has selected this answer as correct

Hello,

You should remove the values NaN from your list and then use mean. It is a bit tricky, because of the following behavior

sage: float(NaN) == float(NaN)
False

The answer may be found here. I reproduce what you want in your case

sage: l = [float(NaN), float(0.01), float(19), float(-3), float(NaN)]
sage: from math import isnan
sage: clean_list = [x for x in l if not isnan(x)]
sage: print clean_list
[0.01, 19.0, -3.0]
sage: mean(clean_list)
5.336666666666667

Vincent

link

posted Jul 31 '12

vdelecroix gravatar image vdelecroix
866 4 13 27

updated Jul 31 '12

Hello! Thank you for reply! I tried to use 'is NaN' as you suggested, but the problem is that I need to obtain the data from a text file. But when I use float('NaN'), I get the 'float' type, naturally. Whereas type(NaN) gives me <type 'sage.symbolic.expression.Expression'> which is different. So I cannot use 'is NaN' construction for the floats obtained by parsing the text file.

v_2e (Jul 31 '12)

I adapt my answer to your problem...

vdelecroix (Jul 31 '12)

Thanks! It works! It's a pity I have to check and remove every item in my lists. It may be rather time-consuming with large data sets. Anyway, thank you for the recipe!

v_2e (Aug 01 '12)

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Jul 31 '12

Seen: 52 times

Last updated: Jul 31 '12

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.