ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 01 Aug 2012 07:01:15 +0200Dealing with 'NaN' values in listshttps://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/ 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.Tue, 31 Jul 2012 06:42:58 +0200https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/Answer by vdelecroix for <p>Hello!</p>
<p>Maybe somebody here has an experience on working with 'NaN' values in Sage.</p>
<p>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.</p>
<p>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.</p>
<p>So could somebody please give me an advice how to work with such arrays correctly?</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?answer=13872#post-id-13872Hello,
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](http://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python). 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
VincentTue, 31 Jul 2012 09:27:58 +0200https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?answer=13872#post-id-13872Comment by v_2e for <p>Hello,</p>
<p>You should remove the values NaN from your list and then use mean. It is a bit tricky, because of the following behavior</p>
<pre><code>sage: float(NaN) == float(NaN)
False
</code></pre>
<p>The answer may be found <a href="http://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python">here</a>. I reproduce what you want in your case</p>
<pre><code>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
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19301#post-id-19301Thanks! 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!Wed, 01 Aug 2012 07:01:15 +0200https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19301#post-id-19301Comment by vdelecroix for <p>Hello,</p>
<p>You should remove the values NaN from your list and then use mean. It is a bit tricky, because of the following behavior</p>
<pre><code>sage: float(NaN) == float(NaN)
False
</code></pre>
<p>The answer may be found <a href="http://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python">here</a>. I reproduce what you want in your case</p>
<pre><code>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
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19303#post-id-19303I adapt my answer to your problem...Tue, 31 Jul 2012 23:58:59 +0200https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19303#post-id-19303Comment by v_2e for <p>Hello,</p>
<p>You should remove the values NaN from your list and then use mean. It is a bit tricky, because of the following behavior</p>
<pre><code>sage: float(NaN) == float(NaN)
False
</code></pre>
<p>The answer may be found <a href="http://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python">here</a>. I reproduce what you want in your case</p>
<pre><code>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
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19309#post-id-19309Hello! 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.Tue, 31 Jul 2012 10:29:25 +0200https://ask.sagemath.org/question/9195/dealing-with-nan-values-in-lists/?comment=19309#post-id-19309