NumPy 1.13.0 Release Notes#
This release supports Python 2.7 and 3.4 - 3.6.
Highlights#
Operations like
a+b+cwill reuse temporaries on some platforms,resulting in less memory use and faster execution.Inplace operations check if inputs overlap outputs and create temporariesto avoid problems.
New
__array_ufunc__attribute provides improved ability for classes tooverride default ufunc behavior.New
np.blockfunction for creating blocked arrays.
New functions#
New
np.positiveufunc.New
np.divmodufunc provides more efficient divmod.New
np.isnatufunc tests for NaT special values.New
np.heavisideufunc computes the Heaviside function.New
np.isinfunction, improves onin1d.New
np.blockfunction for creating blocked arrays.New
PyArray_MapIterArrayCopyIfOverlapadded to NumPy C-API.
See below for details.
Deprecations#
Calling
np.fix,np.isposinf, andnp.isneginfwithf(x,y=out)is deprecated - the argument should be passed asf(x,out=out), whichmatches other ufunc-like interfaces.Use of the C-API
NPY_CHARtype number deprecated since version 1.7 willnow raise deprecation warnings at runtime. Extensions built with older f2pyversions need to be recompiled to remove the warning.np.ma.argsort,np.ma.minimum.reduce, andnp.ma.maximum.reduceshould be called with an explicitaxis argument when applied to arrays withmore than 2 dimensions, as the default value of this argument (None) isinconsistent with the rest of numpy (-1,0, and0, respectively).np.ma.MaskedArray.miniis deprecated, as it almost duplicates thefunctionality ofnp.MaskedArray.min. Exactly equivalent behaviourcan be obtained withnp.ma.minimum.reduce.The single-argument form of
np.ma.minimumandnp.ma.maximumisdeprecated.np.maximum.np.ma.minimum(x)should now be speltnp.ma.minimum.reduce(x), which is consistent with how this would be donewithnp.minimum.Calling
ndarray.conjugateon non-numeric dtypes is deprecated (itshould match the behavior ofnp.conjugate, which throws an error).Calling
expand_dimswhen theaxiskeyword does not satisfy-a.ndim-1<=axis<=a.ndim, whereais the array being reshaped,is deprecated.
Future Changes#
Assignment between structured arrays with different field names will changein NumPy 1.14. Previously, fields in the dst would be set to the value of theidentically-named field in the src. In numpy 1.14 fields will instead beassigned ‘by position’: The n-th field of the dst will be set to the n-thfield of the src array. Note that the
FutureWarningraised in NumPy 1.12incorrectly reported this change as scheduled for NumPy 1.13 rather thanNumPy 1.14.
Build System Changes#
numpy.distutilsnow automatically determines C-file dependencies withGCC compatible compilers.
Compatibility notes#
Error type changes#
numpy.hstack()now throwsValueErrorinstead ofIndexErrorwheninput is empty.Functions taking an axis argument, when that argument is out of range, nowthrow
np.AxisErrorinstead of a mixture ofIndexErrorandValueError. For backwards compatibility,AxisErrorsubclasses both ofthese.
Tuple object dtypes#
Support has been removed for certain obscure dtypes that were unintentionallyallowed, of the form(old_dtype,new_dtype), where either of the dtypesis or contains theobject dtype. As an exception, dtypes of the form(object,[('name',object)]) are still supported due to evidence ofexisting use.
DeprecationWarning to error#
See Changes section for more detail.
partition, TypeError when non-integer partition index is used.NpyIter_AdvancedNew, ValueError whenoa_ndim==0andop_axesis NULLnegative(bool_), TypeError when negative applied to booleans.subtract(bool_,bool_), TypeError when subtracting boolean from boolean.np.equal,np.not_equal, object identity doesn’t override failed comparison.np.equal,np.not_equal, object identity doesn’t override non-boolean comparison.Deprecated boolean indexing behavior dropped. See Changes below for details.
Deprecated
np.alterdot()andnp.restoredot()removed.
FutureWarning to changed behavior#
See Changes section for more detail.
numpy.averagepreserves subclassesarray==Noneandarray!=Nonedo element-wise comparison.np.equal,np.not_equal, object identity doesn’t override comparison result.
dtypes are now always true#
Previouslybool(dtype) would fall back to the default pythonimplementation, which checked iflen(dtype)>0. Sincedtype objectsimplement__len__ as the number of record fields,bool of scalar dtypeswould evaluate toFalse, which was unintuitive. Nowbool(dtype)==Truefor all dtypes.
__getslice__ and__setslice__ are no longer needed inndarray subclasses#
When subclassing np.ndarray in Python 2.7, it is no longer _necessary_ toimplement__*slice__ on the derived class, as__*item__ will interceptthese calls correctly.
Any code that did implement these will work exactly as before. Code thatinvokes``ndarray.__getslice__`` (e.g. throughsuper(...).__getslice__) willnow issue a DeprecationWarning -.__getitem__(slice(start,end)) should beused instead.
Indexing MaskedArrays/Constants with... (ellipsis) now returns MaskedArray#
This behavior mirrors that of np.ndarray, and accounts for nested arrays inMaskedArrays of object dtype, and ellipsis combined with other forms ofindexing.
C API changes#
GUfuncs on empty arrays and NpyIter axis removal#
It is now allowed to remove a zero-sized axis from NpyIter. Which may meanthat code removing axes from NpyIter has to add an additional check whenaccessing the removed dimensions later on.
The largest followup change is that gufuncs are now allowed to have zero-sizedinner dimensions. This means that a gufunc now has to anticipate an empty innerdimension, while this was never possible and an error raised instead.
For most gufuncs no change should be necessary. However, it is now possiblefor gufuncs with a signature such as(...,N,M)->(...,M) to returna valid result ifN=0 without further wrapping code.
PyArray_MapIterArrayCopyIfOverlap added to NumPy C-API#
Similar toPyArray_MapIterArray but with an additionalcopy_if_overlapargument. Ifcopy_if_overlap!=0, checks if input has memory overlap withany of the other arrays and make copies as appropriate to avoid problems if theinput is modified during the iteration. See the documentation for more completedocumentation.
New Features#
__array_ufunc__ added#
This is the renamed and redesigned__numpy_ufunc__. Any class, ndarraysubclass or not, can define this method or set it toNone in order tooverride the behavior of NumPy’s ufuncs. This works quite similarly to Python’s__mul__ and other binary operation routines. See the documentation for amore detailed description of the implementation and behavior of this newoption. The API is provisional, we do not yet guarantee backward compatibilityas modifications may be made pending feedback. SeeNEP 13 anddocumentation for more details.
Newpositive ufunc#
This ufunc corresponds to unary+, but unlike+ on an ndarray it will raisean error if array values do not support numeric operations.
Newdivmod ufunc#
This ufunc corresponds to the Python builtindivmod, and is used to implementdivmod when called on numpy arrays.np.divmod(x,y) calculates a resultequivalent to(np.floor_divide(x,y),np.remainder(x,y)) but isapproximately twice as fast as calling the functions separately.
np.isnat ufunc tests for NaT special datetime and timedelta values#
The new ufuncnp.isnat finds the positions of special NaT valueswithin datetime and timedelta arrays. This is analogous tonp.isnan.
np.heaviside ufunc computes the Heaviside function#
The new functionnp.heaviside(x,h0) (a ufunc) computes the Heavisidefunction:
{0ifx<0,heaviside(x,h0)={h0ifx==0,{1ifx>0.
np.block function for creating blocked arrays#
Add a newblock function to the current stacking functionsvstack,hstack, andstack. This allows concatenation across multiple axessimultaneously, with a similar syntax to array creation, but where elementscan themselves be arrays. For instance:
>>>A=np.eye(2)*2>>>B=np.eye(3)*3>>>np.block([...[A,np.zeros((2,3))],...[np.ones((3,2)),B]...])array([[ 2., 0., 0., 0., 0.], [ 0., 2., 0., 0., 0.], [ 1., 1., 3., 0., 0.], [ 1., 1., 0., 3., 0.], [ 1., 1., 0., 0., 3.]])
While primarily useful for block matrices, this works for arbitrary dimensionsof arrays.
It is similar to Matlab’s square bracket notation for creating block matrices.
isin function, improving onin1d#
The new functionisin tests whether each element of an N-dimensionalarray is present anywhere within a second array. It is an enhancementofin1d that preserves the shape of the first array.
Temporary elision#
On platforms providing thebacktrace function NumPy will try to avoidcreating temporaries in expression involving basic numeric types.For exampled=a+b+c is transformed tod=a+b;d+=c which canimprove performance for large arrays as less memory bandwidth is required toperform the operation.
axes argument forunique#
In an N-dimensional array, the user can now choose the axis along which to lookfor duplicate N-1-dimensional elements usingnumpy.unique. The originalbehaviour is recovered ifaxis=None (default).
np.gradient now supports unevenly spaced data#
Users can now specify a not-constant spacing for data.In particularnp.gradient can now take:
A single scalar to specify a sample distance for all dimensions.
N scalars to specify a constant sample distance for each dimension.i.e.
dx,dy,dz, …N arrays to specify the coordinates of the values along each dimension of F.The length of the array must match the size of the corresponding dimension
Any combination of N scalars/arrays with the meaning of 2. and 3.
This means that, e.g., it is now possible to do the following:
>>>f=np.array([[1,2,6],[3,4,5]],dtype=np.float_)>>>dx=2.>>>y=[1.,1.5,3.5]>>>np.gradient(f,dx,y)[array([[ 1. , 1. , -0.5], [ 1. , 1. , -0.5]]), array([[ 2. , 2. , 2. ], [ 2. , 1.7, 0.5]])]
Support for returning arrays of arbitrary dimensions inapply_along_axis#
Previously, only scalars or 1D arrays could be returned by the function passedtoapply_along_axis. Now, it can return an array of any dimensionality(including 0D), and the shape of this array replaces the axis of the arraybeing iterated over.
.ndim property added todtype to complement.shape#
For consistency withndarray andbroadcast,d.ndim is a shorthandforlen(d.shape).
Support for tracemalloc in Python 3.6#
NumPy now supports memory tracing withtracemalloc module of Python 3.6 ornewer. Memory allocations from NumPy are placed into the domain defined bynumpy.lib.tracemalloc_domain.Note that NumPy allocation will not show up intracemalloc of earlier Pythonversions.
NumPy may be built with relaxed stride checking debugging#
Setting NPY_RELAXED_STRIDES_DEBUG=1 in the environment when relaxed stridechecking is enabled will cause NumPy to be compiled with the affected stridesset to the maximum value of npy_intp in order to help detect invalid usage ofthe strides in downstream projects. When enabled, invalid usage often resultsin an error being raised, but the exact type of error depends on the details ofthe code. TypeError and OverflowError have been observed in the wild.
It was previously the case that this option was disabled for releases andenabled in master and changing between the two required editing the code. It isnow disabled by default but can be enabled for test builds.
Improvements#
Ufunc behavior for overlapping inputs#
Operations where ufunc input and output operands have memory overlapproduced undefined results in previous NumPy versions, due to datadependency issues. In NumPy 1.13.0, results from such operations arenow defined to be the same as for equivalent operations where there isno memory overlap.
Operations affected now make temporary copies, as needed to eliminatedata dependency. As detecting these cases is computationallyexpensive, a heuristic is used, which may in rare cases result toneedless temporary copies. For operations where the data dependencyis simple enough for the heuristic to analyze, temporary copies willnot be made even if the arrays overlap, if it can be deduced copiesare not necessary. As an example,``np.add(a, b, out=a)`` will notinvolve copies.
To illustrate a previously undefined operation:
>>>x=np.arange(16).astype(float)>>>np.add(x[1:],x[:-1],out=x[1:])
In NumPy 1.13.0 the last line is guaranteed to be equivalent to:
>>>np.add(x[1:].copy(),x[:-1].copy(),out=x[1:])
A similar operation with simple non-problematic data dependence is:
>>>x=np.arange(16).astype(float)>>>np.add(x[1:],x[:-1],out=x[:-1])
It will continue to produce the same results as in previous NumPyversions, and will not involve unnecessary temporary copies.
The change applies also to in-place binary operations, for example:
>>>x=np.random.rand(500,500)>>>x+=x.T
This statement is now guaranteed to be equivalent tox[...]=x+x.T,whereas in previous NumPy versions the results were undefined.
Partial support for 64-bit f2py extensions with MinGW#
Extensions that incorporate Fortran libraries can now be built using the freeMinGW toolset, also under Python 3.5. This works best for extensions that onlydo calculations and uses the runtime modestly (reading and writing from files,for instance). Note that this does not remove the need for Mingwpy; if you makeextensive use of the runtime, you will most likely run intoissues. Instead,it should be regarded as a band-aid until Mingwpy is fully functional.
Extensions can also be compiled using the MinGW toolset using the runtimelibrary from the (moveable) WinPython 3.4 distribution, which can be useful forprograms with a PySide1/Qt4 front-end.
Performance improvements forpackbits andunpackbits#
The functionsnumpy.packbits with boolean input andnumpy.unpackbits havebeen optimized to be a significantly faster for contiguous data.
Fix for PPC long double floating point information#
In previous versions of NumPy, thefinfo function returned invalidinformation about thedouble double format of thelongdouble float typeon Power PC (PPC). The invalid values resulted from the failure of the NumPyalgorithm to deal with the variable number of digits in the significandthat are a feature ofPPC long doubles. This release by-passes the failingalgorithm by using heuristics to detect the presence of the PPC double doubleformat. A side-effect of using these heuristics is that thefinfofunction is faster than previous releases.
Better default repr forndarray subclasses#
Subclasses of ndarray with norepr specialization now correctly indenttheir data and type lines.
More reliable comparisons of masked arrays#
Comparisons of masked arrays were buggy for masked scalars and failed forstructured arrays with dimension higher than one. Both problems are nowsolved. In the process, it was ensured that in getting the result for astructured array, masked fields are properly ignored, i.e., the result is equalif all fields that are non-masked in both are equal, thus making the behaviouridentical to what one gets by comparing an unstructured masked array and thendoing.all() over some axis.
np.matrix with booleans elements can now be created using the string syntax#
np.matrix failed whenever one attempts to use it with booleans, e.g.,np.matrix('True'). Now, this works as expected.
Morelinalg operations now accept empty vectors and matrices#
All of the following functions innp.linalg now work when given inputarrays with a 0 in the last two dimensions:det,slogdet,pinv,eigvals,eigvalsh,eig,eigh.
Bundled version of LAPACK is now 3.2.2#
NumPy comes bundled with a minimal implementation of lapack for systems withouta lapack library installed, under the name oflapack_lite. This has beenupgraded from LAPACK 3.0.0 (June 30, 1999) to LAPACK 3.2.2 (June 30, 2010). SeetheLAPACK changelogs for details on the all the changes this entails.
While no new features are exposed throughnumpy, this fixes some bugsregarding “workspace” sizes, and in some places may use faster algorithms.
reduce ofnp.hypot.reduce andnp.logical_xor allowed in more cases#
This now works on empty arrays, returning 0, and can reduce over multiple axes.Previously, aValueError was thrown in these cases.
Betterrepr of object arrays#
Object arrays that contain themselves no longer cause a recursion error.
Object arrays that containlist objects are now printed in a way that makesclear the difference between a 2d object array, and a 1d object array of lists.
Changes#
argsort on masked arrays takes the same default arguments assort#
By default,argsort now places the masked values at the end of the sortedarray, in the same way thatsort already did. Additionally, theend_with argument is added toargsort, for consistency withsort.Note that this argument is not added at the end, so breaks any code thatpassedfill_value as a positional argument.
average now preserves subclasses#
For ndarray subclasses,numpy.average will now return an instance of thesubclass, matching the behavior of most other NumPy functions such asmean.As a consequence, also calls that returned a scalar may now return a subclassarray scalar.
array==None andarray!=None do element-wise comparison#
Previously these operations returned scalarsFalse andTrue respectively.
np.equal,np.not_equal for object arrays ignores object identity#
Previously, these functions always treated identical objects as equal. This hadthe effect of overriding comparison failures, comparison of objects that didnot return booleans, such as np.arrays, and comparison of objects where theresults differed from object identity, such as NaNs.
Boolean indexing changes#
Boolean array-likes (such as lists of python bools) are always treated asboolean indexes.
Boolean scalars (including python
True) are legal boolean indexes andnever treated as integers.Boolean indexes must match the dimension of the axis that they index.
Boolean indexes used on the lhs of an assignment must match the dimensions ofthe rhs.
Boolean indexing into scalar arrays return a new 1-d array. This means that
array(1)[array(True)]givesarray([1])and not the original array.
np.random.multivariate_normal behavior with bad covariance matrix#
It is now possible to adjust the behavior the function will have when dealingwith the covariance matrix by using two new keyword arguments:
tolcan be used to specify a tolerance to use when checking thatthe covariance matrix is positive semidefinite.check_validcan be used to configure what the function will do in thepresence of a matrix that is not positive semidefinite. Valid options areignore,warnandraise. The default value,warnkeeps thethe behavior used on previous releases.
assert_array_less comparesnp.inf and-np.inf now#
Previously,np.testing.assert_array_less ignored all infinite values. Thisis not the expected behavior both according to documentation and intuitively.Now, -inf < x < inf is consideredTrue for any real number x and allother cases fail.
assert_array_ and masked arraysassert_equal hide less warnings#
Some warnings that were previously hidden by theassert_array_functions are not hidden anymore. In most cases the warnings should becorrect and, should they occur, will require changes to the tests usingthese functions.For the masked arrayassert_equal version, warnings may occur whencomparing NaT. The function presently does not handle NaT or NaNspecifically and it may be best to avoid it at this time should a warningshow up due to this change.
offset attribute value inmemmap objects#
Theoffset attribute in amemmap object is now set to theoffset into the file. This is a behaviour change only for offsetsgreater thanmmap.ALLOCATIONGRANULARITY.
np.real andnp.imag return scalars for scalar inputs#
Previously,np.real andnp.imag used to return array objects whenprovided a scalar input, which was inconsistent with other functions likenp.angle andnp.conj.
The polynomial convenience classes cannot be passed to ufuncs#
The ABCPolyBase class, from which the convenience classes are derived, sets__array_ufun__=None in order of opt out of ufuncs. If a polynomialconvenience class instance is passed as an argument to a ufunc, aTypeErrorwill now be raised.
Output arguments to ufuncs can be tuples also for ufunc methods#
For calls to ufuncs, it was already possible, and recommended, to use anout argument with a tuple for ufuncs with multiple outputs. This has nowbeen extended to output arguments in thereduce,accumulate, andreduceat methods. This is mostly for compatibility with__array_ufunc;there are no ufuncs yet that have more than one output.