@@ -475,6 +475,7 @@ def apply_over_axes(func, a, axes):
475
475
"an array of the correct shape" )
476
476
return val
477
477
478
+
478
479
if apply_over_axes .__doc__ is not None :
479
480
apply_over_axes .__doc__ = np .apply_over_axes .__doc__ [
480
481
:np .apply_over_axes .__doc__ .find ('Notes' )].rstrip () + \
@@ -524,7 +525,8 @@ def apply_over_axes(func, a, axes):
524
525
"""
525
526
526
527
527
- def average (a , axis = None , weights = None , returned = False ):
528
+ def average (a , axis = None , weights = None , returned = False , * ,
529
+ keepdims = np ._NoValue ):
528
530
"""
529
531
Return the weighted average of array over the given axis.
530
532
@@ -550,6 +552,14 @@ def average(a, axis=None, weights=None, returned=False):
550
552
Flag indicating whether a tuple ``(result, sum of weights)``
551
553
should be returned as output (True), or just the result (False).
552
554
Default is False.
555
+ keepdims : bool, optional
556
+ If this is set to True, the axes which are reduced are left
557
+ in the result as dimensions with size one. With this option,
558
+ the result will broadcast correctly against the original `a`.
559
+ *Note:* `keepdims` will not work with instances of `numpy.matrix`
560
+ or other classes whose methods do not support `keepdims`.
561
+
562
+ .. versionadded:: 1.23.0
553
563
554
564
Returns
555
565
-------
@@ -582,14 +592,29 @@ def average(a, axis=None, weights=None, returned=False):
582
592
mask=[False, False],
583
593
fill_value=1e+20)
584
594
595
+ With ``keepdims=True``, the following result has shape (3, 1).
596
+
597
+ >>> np.ma.average(x, axis=1, keepdims=True)
598
+ masked_array(
599
+ data=[[0.5],
600
+ [2.5],
601
+ [4.5]],
602
+ mask=False,
603
+ fill_value=1e+20)
585
604
"""
586
605
a = asarray (a )
587
606
m = getmask (a )
588
607
589
608
# inspired by 'average' in numpy/lib/function_base.py
590
609
610
+ if keepdims is np ._NoValue :
611
+ # Don't pass on the keepdims argument if one wasn't given.
612
+ keepdims_kw = {}
613
+ else :
614
+ keepdims_kw = {'keepdims' : keepdims }
615
+
591
616
if weights is None :
592
- avg = a .mean (axis )
617
+ avg = a .mean (axis , ** keepdims_kw )
593
618
scl = avg .dtype .type (a .count (axis ))
594
619
else :
595
620
wgt = asarray (weights )
@@ -621,7 +646,8 @@ def average(a, axis=None, weights=None, returned=False):
621
646
wgt .mask |= a .mask
622
647
623
648
scl = wgt .sum (axis = axis , dtype = result_dtype )
624
- avg = np .multiply (a , wgt , dtype = result_dtype ).sum (axis )/ scl
649
+ avg = np .multiply (a , wgt ,
650
+ dtype = result_dtype ).sum (axis , ** keepdims_kw ) / scl
625
651
626
652
if returned :
627
653
if scl .shape != avg .shape :
@@ -713,6 +739,7 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
713
739
else :
714
740
return r
715
741
742
+
716
743
def _median (a , axis = None , out = None , overwrite_input = False ):
717
744
# when an unmasked NaN is present return it, so we need to sort the NaN
718
745
# values behind the mask
@@ -840,6 +867,7 @@ def compress_nd(x, axis=None):
840
867
data = data [(slice (None ),)* ax + (~ m .any (axis = axes ),)]
841
868
return data
842
869
870
+
843
871
def compress_rowcols (x , axis = None ):
844
872
"""
845
873
Suppress the rows and/or columns of a 2-D array that contain
@@ -912,6 +940,7 @@ def compress_rows(a):
912
940
raise NotImplementedError ("compress_rows works for 2D arrays only." )
913
941
return compress_rowcols (a , 0 )
914
942
943
+
915
944
def compress_cols (a ):
916
945
"""
917
946
Suppress whole columns of a 2-D array that contain masked values.
@@ -929,6 +958,7 @@ def compress_cols(a):
929
958
raise NotImplementedError ("compress_cols works for 2D arrays only." )
930
959
return compress_rowcols (a , 1 )
931
960
961
+
932
962
def mask_rows (a , axis = np ._NoValue ):
933
963
"""
934
964
Mask rows of a 2D array that contain masked values.
@@ -979,6 +1009,7 @@ def mask_rows(a, axis=np._NoValue):
979
1009
"will raise TypeError" , DeprecationWarning , stacklevel = 2 )
980
1010
return mask_rowcols (a , 0 )
981
1011
1012
+
982
1013
def mask_cols (a , axis = np ._NoValue ):
983
1014
"""
984
1015
Mask columns of a 2D array that contain masked values.
@@ -1516,6 +1547,7 @@ def __init__(self):
1516
1547
1517
1548
mr_ = mr_class ()
1518
1549
1550
+
1519
1551
#####--------------------------------------------------------------------------
1520
1552
#---- Find unmasked data ---
1521
1553
#####--------------------------------------------------------------------------
@@ -1682,6 +1714,7 @@ def flatnotmasked_contiguous(a):
1682
1714
i += n
1683
1715
return result
1684
1716
1717
+
1685
1718
def notmasked_contiguous (a , axis = None ):
1686
1719
"""
1687
1720
Find contiguous unmasked data in a masked array along the given axis.
0 commit comments