From 579034a0c925ee9db5157141d73209beca037aeb Mon Sep 17 00:00:00 2001 From: Geoffrey Irving Date: Thu, 15 Feb 2018 11:56:56 -0800 Subject: ENH: make flatnonzero use np.ravel(a) instead of a.ravel() It now works for lists and other numpy-convertible input. Fixes #10598. --- numpy/core/numeric.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 123bff2ec..5c8951474 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -829,12 +829,12 @@ def flatnonzero(a): """ Return indices that are non-zero in the flattened version of a. - This is equivalent to a.ravel().nonzero()[0]. + This is equivalent to np.nonzero(np.ravel(a))[0]. Parameters ---------- - a : ndarray - Input array. + a : array_like + Input data. Returns ------- @@ -862,7 +862,7 @@ def flatnonzero(a): array([-2, -1, 1, 2]) """ - return a.ravel().nonzero()[0] + return np.nonzero(np.ravel(a))[0] _mode_from_name_dict = {'v': 0, -- cgit v1.2.1 From 96f346d2ac81f12081cacb6524bc779626132a68 Mon Sep 17 00:00:00 2001 From: Andrey Portnoy Date: Wed, 28 Feb 2018 05:25:13 -0800 Subject: STY: Minor stylistic cleanup of numeric.py No changes to the logic, only minor edits guided by flake8. --- numpy/core/numeric.py | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 5c8951474..d2d59d9b2 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -69,7 +69,7 @@ __all__ = [ 'False_', 'True_', 'bitwise_not', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS', 'ComplexWarning', 'full', 'full_like', 'matmul', 'shares_memory', 'may_share_memory', 'MAY_SHARE_BOUNDS', - 'MAY_SHARE_EXACT', 'TooHardError', 'AxisError' ] + 'MAY_SHARE_EXACT', 'TooHardError', 'AxisError'] if sys.version_info[0] < 3: __all__.extend(['getbuffer', 'newbuffer']) @@ -365,6 +365,7 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True): multiarray.copyto(res, fill_value, casting='unsafe') return res + def count_nonzero(a, axis=None): """ Counts the number of non-zero values in the array ``a``. @@ -686,12 +687,12 @@ def require(a, dtype=None, requirements=None): UPDATEIFCOPY : False """ - possible_flags = {'C':'C', 'C_CONTIGUOUS':'C', 'CONTIGUOUS':'C', - 'F':'F', 'F_CONTIGUOUS':'F', 'FORTRAN':'F', - 'A':'A', 'ALIGNED':'A', - 'W':'W', 'WRITEABLE':'W', - 'O':'O', 'OWNDATA':'O', - 'E':'E', 'ENSUREARRAY':'E'} + possible_flags = {'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C', + 'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F', + 'A': 'A', 'ALIGNED': 'A', + 'W': 'W', 'WRITEABLE': 'W', + 'O': 'O', 'OWNDATA': 'O', + 'E': 'E', 'ENSUREARRAY': 'E'} if not requirements: return asanyarray(a, dtype=dtype) else: @@ -1123,7 +1124,7 @@ def outer(a, b, out=None): """ a = asarray(a) b = asarray(b) - return multiply(a.ravel()[:, newaxis], b.ravel()[newaxis,:], out) + return multiply(a.ravel()[:, newaxis], b.ravel()[newaxis, :], out) def tensordot(a, b, axes=2): @@ -1790,6 +1791,7 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None): return moveaxis(cp, -1, axisc) + little_endian = (sys.byteorder == 'little') @@ -2313,12 +2315,12 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`)) Unlike the built-in `math.isclose`, the above equation is not symmetric - in `a` and `b` -- it assumes `b` is the reference value -- so that + in `a` and `b` -- it assumes `b` is the reference value -- so that `isclose(a, b)` might be different from `isclose(b, a)`. Furthermore, the default value of atol is not zero, and is used to determine what small values should be considered close to zero. The default value is appropriate for expected values of order unity: if the expected values - are significantly smaller than one, it can result in false positives. + are significantly smaller than one, it can result in false positives. `atol` should be carefully selected for the use case at hand. A zero value for `atol` will result in `False` if either `a` or `b` is zero. @@ -2471,12 +2473,12 @@ def array_equiv(a1, a2): return bool(asarray(a1 == a2).all()) -_errdict = {"ignore":ERR_IGNORE, - "warn":ERR_WARN, - "raise":ERR_RAISE, - "call":ERR_CALL, - "print":ERR_PRINT, - "log":ERR_LOG} +_errdict = {"ignore": ERR_IGNORE, + "warn": ERR_WARN, + "raise": ERR_RAISE, + "call": ERR_CALL, + "print": ERR_PRINT, + "log": ERR_LOG} _errdict_rev = {} for key in _errdict.keys(): @@ -2543,7 +2545,8 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None): {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} >>> np.seterr(**old_settings) # reset to default - {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} + {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', + 'under': 'ignore'} >>> np.int16(32000) * np.int16(3) 30464 @@ -2690,11 +2693,11 @@ def seterrcall(func): Function to call upon floating-point errors ('call'-mode) or object whose 'write' method is used to log such message ('log'-mode). - The call function takes two arguments. The first is a string describing the - type of error (such as "divide by zero", "overflow", "underflow", or "invalid value"), - and the second is the status flag. The flag is a byte, whose four - least-significant bits indicate the type of error, one of "divide", "over", - "under", "invalid":: + The call function takes two arguments. The first is a string describing + the type of error (such as "divide by zero", "overflow", "underflow", + or "invalid value"), and the second is the status flag. The flag is a + byte, whose four least-significant bits indicate the type of error, one + of "divide", "over", "under", "invalid":: [0 0 0 0 divide over under invalid] @@ -2811,6 +2814,8 @@ def geterrcall(): class _unspecified(object): pass + + _Unspecified = _unspecified() @@ -2918,6 +2923,7 @@ def extend_all(module): if a not in adict: __all__.append(a) + from .umath import * from .numerictypes import * from . import fromnumeric -- cgit v1.2.1 From 5a238789894a1f21961217eaaae3e18bcf6ea7c7 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Wed, 28 Feb 2018 16:38:55 -0800 Subject: DOC zeros, empty, and ones now have consistent docstrings closes #10611 --- numpy/core/numeric.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d2d59d9b2..1f249ae6c 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -160,9 +160,10 @@ def ones(shape, dtype=None, order='C'): dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.float64`. - order : {'C', 'F'}, optional - Whether to store multidimensional data in C- or Fortran-contiguous - (row- or column-wise) order in memory. + order : {'C', 'F'}, optional, default: C + Whether to store multi-dimensional data in row-major + (C-style) or column-major (Fortran-style) order in + memory. Returns ------- -- cgit v1.2.1 From 53b358ce7eddf78ac2bc22045fbe25e91e663b9a Mon Sep 17 00:00:00 2001 From: Frederick Lefebvre Date: Wed, 14 Mar 2018 03:33:05 +0000 Subject: TST: Import abstract classes from collections.abc Abstract collection classes accessed from the collections module have been deprecated since Python 3.3. They should be accessed through collections.abc. When run with Python 3.7, the deprecation warning cause multiple tests to fail. --- numpy/core/numeric.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 1f249ae6c..d2348f364 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1,6 +1,11 @@ from __future__ import division, absolute_import, print_function -import collections +try: + # Accessing collections abstact classes from collections + # has been deprecated since Python 3.3 + import collections.abc as collections_abc +except ImportError: + import collections as collections_abc import itertools import operator import sys @@ -2758,8 +2763,8 @@ def seterrcall(func): {'over': 'log', 'divide': 'log', 'invalid': 'log', 'under': 'log'} """ - if func is not None and not isinstance(func, collections.Callable): - if not hasattr(func, 'write') or not isinstance(func.write, collections.Callable): + if func is not None and not isinstance(func, collections_abc.Callable): + if not hasattr(func, 'write') or not isinstance(func.write, collections_abc.Callable): raise ValueError("Only callable can be used as callback") pyvals = umath.geterrobj() old = geterrcall() -- cgit v1.2.1 From 53a558ca2e6f400830210d30c9e1f5ccaa3268eb Mon Sep 17 00:00:00 2001 From: ChloeColeongco Date: Thu, 22 Mar 2018 12:25:39 -0600 Subject: Fixed author name in reference to book ('van' vs 'Van') --- numpy/core/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d2348f364..aa5059180 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1089,7 +1089,7 @@ def outer(a, b, out=None): References ---------- - .. [1] : G. H. Golub and C. F. van Loan, *Matrix Computations*, 3rd + .. [1] : G. H. Golub and C. F. Van Loan, *Matrix Computations*, 3rd ed., Baltimore, MD, Johns Hopkins University Press, 1996, pg. 8. -- cgit v1.2.1 From 108d01a0a4eea38ed5b8e88de34ee2d0324fec65 Mon Sep 17 00:00:00 2001 From: "luz.paz" Date: Fri, 30 Mar 2018 09:33:39 -0400 Subject: DOC: Fix minor typos Found via `codespell -q 3 -I ../numpy-whitelist.txt` --- numpy/core/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index aa5059180..d154206c5 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1,7 +1,7 @@ from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: -- cgit v1.2.1 From f2f17210a5e5824a9977d63957c09bbe5d64eda2 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 9 Apr 2018 12:14:24 -0700 Subject: Cross Link full/full_like in a few see-also. While teaching numpy I was asked the best way to create an array of nan, and `np.full` seem not be cross linked from many places; In particular in the documentation of `zeros` and `ones` seam like obvious candidates to add them. Reorder all the see-also to be - empty_like - ones_like - zero_like - full_like - empty - ones - zeros - full --- numpy/core/numeric.py | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d154206c5..a5ee5f815 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -123,11 +123,13 @@ def zeros_like(a, dtype=None, order='K', subok=True): See Also -------- - ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. - zeros : Return a new array setting values to zero. - ones : Return a new array setting values to one. + ones_like : Return an array of ones with shape and type of input. + full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. + ones : Return a new array setting values to one. + zeros : Return a new array setting values to zero. + full : Return a new array of given shape filled with value. Examples -------- @@ -177,7 +179,14 @@ def ones(shape, dtype=None, order='C'): See Also -------- - zeros, ones_like + empty_like : Return an empty array with shape and type of input. + ones_like : Return an array of ones with shape and type of input. + zeros_like : Return an array of zeros with shape and type of input. + full_like : Return a new array with shape of input filled with value. + empty : Return a new uninitialized array. + zeros : Return a new array setting values to zero. + full : Return a new array of given shape filled with value. + Examples -------- @@ -234,11 +243,13 @@ def ones_like(a, dtype=None, order='K', subok=True): See Also -------- - zeros_like : Return an array of zeros with shape and type of input. empty_like : Return an empty array with shape and type of input. - zeros : Return a new array setting values to zero. - ones : Return a new array setting values to one. + zeros_like : Return an array of zeros with shape and type of input. + full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. + ones : Return a new array setting values to one. + zeros : Return a new array setting values to zero. + full : Return a new array of given shape filled with value. Examples -------- @@ -287,13 +298,13 @@ def full(shape, fill_value, dtype=None, order='C'): See Also -------- - zeros_like : Return an array of zeros with shape and type of input. - ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. - full_like : Fill an array with shape and type of input. - zeros : Return a new array setting values to zero. - ones : Return a new array setting values to one. + ones_like : Return an array of ones with shape and type of input. + zeros_like : Return an array of zeros with shape and type of input. + full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. + ones : Return a new array setting values to one. + zeros : Return a new array setting values to zero. Examples -------- @@ -342,13 +353,13 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True): See Also -------- - zeros_like : Return an array of zeros with shape and type of input. - ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. - zeros : Return a new array setting values to zero. - ones : Return a new array setting values to one. + ones_like : Return an array of ones with shape and type of input. + zeros_like : Return an array of zeros with shape and type of input. empty : Return a new uninitialized array. - full : Fill a new array. + ones : Return a new array setting values to one. + zeros : Return a new array setting values to zero. + full : Return a new array of given shape filled with value. Examples -------- -- cgit v1.2.1 From 35753b1f28106d5b3fe48f31130e2dde7a02aa0c Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 9 Apr 2018 13:43:38 -0700 Subject: 'remove indirect relationships' --- numpy/core/numeric.py | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index a5ee5f815..77aaf6dec 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -126,10 +126,7 @@ def zeros_like(a, dtype=None, order='K', subok=True): empty_like : Return an empty array with shape and type of input. ones_like : Return an array of ones with shape and type of input. full_like : Return a new array with shape of input filled with value. - empty : Return a new uninitialized array. - ones : Return a new array setting values to one. zeros : Return a new array setting values to zero. - full : Return a new array of given shape filled with value. Examples -------- @@ -179,10 +176,7 @@ def ones(shape, dtype=None, order='C'): See Also -------- - empty_like : Return an empty array with shape and type of input. ones_like : Return an array of ones with shape and type of input. - zeros_like : Return an array of zeros with shape and type of input. - full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. zeros : Return a new array setting values to zero. full : Return a new array of given shape filled with value. @@ -246,10 +240,7 @@ def ones_like(a, dtype=None, order='K', subok=True): empty_like : Return an empty array with shape and type of input. zeros_like : Return an array of zeros with shape and type of input. full_like : Return a new array with shape of input filled with value. - empty : Return a new uninitialized array. ones : Return a new array setting values to one. - zeros : Return a new array setting values to zero. - full : Return a new array of given shape filled with value. Examples -------- @@ -298,9 +289,6 @@ def full(shape, fill_value, dtype=None, order='C'): See Also -------- - empty_like : Return an empty array with shape and type of input. - ones_like : Return an array of ones with shape and type of input. - zeros_like : Return an array of zeros with shape and type of input. full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. ones : Return a new array setting values to one. @@ -356,9 +344,6 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True): empty_like : Return an empty array with shape and type of input. ones_like : Return an array of ones with shape and type of input. zeros_like : Return an array of zeros with shape and type of input. - empty : Return a new uninitialized array. - ones : Return a new array setting values to one. - zeros : Return a new array setting values to zero. full : Return a new array of given shape filled with value. Examples -- cgit v1.2.1 From 1eef2af85df832b55c856935a820889cdee83581 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 11 Apr 2018 14:23:07 +0300 Subject: formatting fixes --- numpy/core/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d154206c5..028dfc745 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2533,7 +2533,7 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None): Notes ----- - The floating-point exceptions are defined in the IEEE 754 standard [1]: + The floating-point exceptions are defined in the IEEE 754 standard [1]_: - Division by zero: infinite result obtained from finite numbers. - Overflow: result too large to be expressed. -- cgit v1.2.1 From d7e0c7e34b293ab32a46960c65d063446115d4e8 Mon Sep 17 00:00:00 2001 From: Marten van Kerkwijk Date: Sun, 29 Apr 2018 15:24:01 -0400 Subject: MAINT: move matrix tests in core, polynomial to matrixlib. --- numpy/core/numeric.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 1108d4667..cd783d242 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -489,9 +489,9 @@ def asarray(a, dtype=None, order=None): Contrary to `asanyarray`, ndarray subclasses are not passed through: - >>> issubclass(np.matrix, np.ndarray) + >>> issubclass(np.recarray, np.ndarray) True - >>> a = np.matrix([[1, 2]]) + >>> a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray) >>> np.asarray(a) is a False >>> np.asanyarray(a) is a @@ -545,7 +545,7 @@ def asanyarray(a, dtype=None, order=None): Instances of `ndarray` subclasses are passed through as-is: - >>> a = np.matrix([1, 2]) + >>> a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray) >>> np.asanyarray(a) is a True @@ -2280,7 +2280,7 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): relative difference (`rtol` * abs(`b`)) and the absolute difference `atol` are added together to compare against the absolute difference between `a` and `b`. - + .. warning:: The default `atol` is not appropriate for comparing numbers that are much smaller than one (see Notes). -- cgit v1.2.1 From 6aa659951a63d7c491f1a06aea0605ea30ec699f Mon Sep 17 00:00:00 2001 From: zuko3d Date: Fri, 18 May 2018 19:54:01 +0300 Subject: BUG: Fix typo in variable name (#11116) Typo fix in variable name in core/numeric/binary_repr/warn_if_insufficient. This only worked before because it used binwidth from the outer namespace. --- numpy/core/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index cd783d242..7ade3d224 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2035,7 +2035,7 @@ def binary_repr(num, width=None): '11101' """ - def warn_if_insufficient(width, binwdith): + def warn_if_insufficient(width, binwidth): if width is not None and width < binwidth: warnings.warn( "Insufficient bit width provided. This behavior " -- cgit v1.2.1