@@ -433,7 +433,7 @@ def asarray_chkfinite(a, dtype=None, order=None):
433
433
By default, the data-type is inferred from the input data.
434
434
order : {'C', 'F', 'A', 'K'}, optional
435
435
Memory layout. 'A' and 'K' depend on the order of input array a.
436
- 'C' row-major (C-style),
436
+ 'C' row-major (C-style),
437
437
'F' column-major (Fortran-style) memory representation.
438
438
'A' (any) means 'F' if `a` is Fortran contiguous, 'C' otherwise
439
439
'K' (keep) preserve input order
@@ -1624,6 +1624,57 @@ def trim_zeros(filt, trim='fb'):
1624
1624
>>> np.trim_zeros([0, 1, 2, 0])
1625
1625
[1, 2]
1626
1626
1627
+ """
1628
+ try :
1629
+ return _trim_zeros_new (filt , trim )
1630
+ except Exception as ex :
1631
+ # Numpy 1.20.0, 2020-07-31
1632
+ warning = DeprecationWarning (
1633
+ "in the future trim_zeros will require a 1-D array as input "
1634
+ "that is compatible with ndarray.astype(bool)"
1635
+ )
1636
+ warning .__cause__ = ex
1637
+ warnings .warn (warning , stacklevel = 3 )
1638
+
1639
+ # Fall back to the old implementation if an exception is encountered
1640
+ # Note that the same exception may or may not be raised here as well
1641
+ return _trim_zeros_old (filt , trim )
1642
+
1643
+
1644
+ def _trim_zeros_new (filt , trim = 'fb' ):
1645
+ """Newer optimized implementation of ``trim_zeros()``."""
1646
+ arr = np .asanyarray (filt ).astype (bool , copy = False )
1647
+
1648
+ if arr .ndim != 1 :
1649
+ raise ValueError ('trim_zeros requires an array of exactly one dimension' )
1650
+ elif not len (arr ):
1651
+ return filt
1652
+
1653
+ trim_upper = trim .upper ()
1654
+ first = last = None
1655
+
1656
+ if 'F' in trim_upper :
1657
+ first = arr .argmax ()
1658
+ # If `arr[first] is False` then so are all other elements
1659
+ if not arr [first ]:
1660
+ return filt [:0 ]
1661
+
1662
+ if 'B' in trim_upper :
1663
+ last = len (arr ) - arr [::- 1 ].argmax ()
1664
+ # If `arr[last - 1] is False` then so are all other elements
1665
+ if not arr [last - 1 ]:
1666
+ return filt [:0 ]
1667
+
1668
+ return filt [first :last ]
1669
+
1670
+
1671
+ def _trim_zeros_old (filt , trim = 'fb' ):
1672
+ """
1673
+ Older unoptimized implementation of ``trim_zeros()``.
1674
+
1675
+ Used as fallback in case an exception is encountered
1676
+ in ``_trim_zeros_new()``.
1677
+
1627
1678
"""
1628
1679
first = 0
1629
1680
trim = trim .upper ()
@@ -2546,11 +2597,11 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
2546
2597
for backwards compatibility with previous versions of this function. These
2547
2598
arguments had no effect on the return values of the function and can be
2548
2599
safely ignored in this and previous versions of numpy.
2549
-
2600
+
2550
2601
Examples
2551
- --------
2602
+ --------
2552
2603
In this example we generate two random arrays, ``xarr`` and ``yarr``, and
2553
- compute the row-wise and column-wise Pearson correlation coefficients,
2604
+ compute the row-wise and column-wise Pearson correlation coefficients,
2554
2605
``R``. Since ``rowvar`` is true by default, we first find the row-wise
2555
2606
Pearson correlation coefficients between the variables of ``xarr``.
2556
2607
@@ -2566,11 +2617,11 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
2566
2617
array([[ 1. , 0.99256089, -0.68080986],
2567
2618
[ 0.99256089, 1. , -0.76492172],
2568
2619
[-0.68080986, -0.76492172, 1. ]])
2569
-
2570
- If we add another set of variables and observations ``yarr``, we can
2620
+
2621
+ If we add another set of variables and observations ``yarr``, we can
2571
2622
compute the row-wise Pearson correlation coefficients between the
2572
2623
variables in ``xarr`` and ``yarr``.
2573
-
2624
+
2574
2625
>>> yarr = rng.random((3, 3))
2575
2626
>>> yarr
2576
2627
array([[0.45038594, 0.37079802, 0.92676499],
@@ -2592,7 +2643,7 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
2592
2643
1. ]])
2593
2644
2594
2645
Finally if we use the option ``rowvar=False``, the columns are now
2595
- being treated as the variables and we will find the column-wise Pearson
2646
+ being treated as the variables and we will find the column-wise Pearson
2596
2647
correlation coefficients between variables in ``xarr`` and ``yarr``.
2597
2648
2598
2649
>>> R3 = np.corrcoef(xarr, yarr, rowvar=False)
0 commit comments