np.nanmean: holy shit!
Last updated: Aug23,2020

The np.nanmean (and other) functions are REALLY useful. I should put this into table_stats!

 
 
This code: /home/sco/NumPy/nanmean_play.py
#!/usr/bin/env python

import numpy as np

#=======================================================
# I read the text lines directly into a numpy array 
b = np.loadtxt('test_tab.txt', dtype=float)
print "\nHere is my loaded numpy array (named b):\n", 
print b 
print "Type of b         = %s \n" % ( type(b) ),
print "b.dtype = ", b.dtype
print "b.shape = ", b.shape
print "b.ndim = ", b.ndim
print "b.size = ", b.size 
#=======================================================

cvalue = np.nanmean(b) 
cval = np.nanmin(b) 

print "Mean of b using mean     = %8.3f \n" % ( b.mean() ),
print "Mean of b using nanmean  = %8.3f \n" % ( cvalue )
print "Min of b using nanmin    = %8.3f \n" % ( cval )




 

The run: 
% cat test_tab.txt  

% cat test_tab.txt 
 1.0  2.0   3.0 
 1.0  2.0   nan 

% python nanmean_play.py  
% python nanmean_play.py

Here is my loaded numpy array (named b):
[[ 1.  2.  3.]
 [ 1.  2. nan]]
Type of b         =  
b.dtype =  float64
b.shape =  (2, 3)
b.ndim =  2
b.size =  6
Mean of b using mean     =      nan 
Mean of b using nanmean  =    1.800      # This is correct! 

Min of b using nanmin    =    1.000      # This is correct! 


This is so cool!




Back to calling page