Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2050eb0

Browse files
njsmithcertik
authored andcommitted
[FIX] preserve memory order in np.copy()
This switches us back to the behaviour seen in numpy 1.6 and earlier, which it turns out that scikit-learn (and probably others) relied on.
1 parent eb730a5 commit 2050eb0

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

numpy/add_newdocs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3212,10 +3212,13 @@ def luf(lamdaexpr, *args, **kwargs):
32123212
Controls the memory layout of the copy. 'C' means C-order,
32133213
'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
32143214
'C' otherwise. 'K' means match the layout of `a` as closely
3215-
as possible.
3215+
as possible. (Note that this function and :func:numpy.copy are very
3216+
similar, but have different default values for their order=
3217+
arguments.)
32163218
32173219
See also
32183220
--------
3221+
numpy.copy
32193222
numpy.copyto
32203223
32213224
Examples

numpy/lib/function_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def select(condlist, choicelist, default=0):
778778
S = S*ones(asarray(pfac).shape, S.dtype)
779779
return choose(S, tuple(choicelist))
780780

781-
def copy(a, order='C'):
781+
def copy(a, order='K'):
782782
"""
783783
Return an array copy of the given object.
784784
@@ -790,7 +790,9 @@ def copy(a, order='C'):
790790
Controls the memory layout of the copy. 'C' means C-order,
791791
'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
792792
'C' otherwise. 'K' means match the layout of `a` as closely
793-
as possible.
793+
as possible. (Note that this function and :meth:ndarray.copy are very
794+
similar, but have different default values for their order=
795+
arguments.)
794796
795797
Returns
796798
-------

numpy/lib/tests/test_function_base.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ def test_nd(self):
4242
assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
4343

4444

45+
class TestCopy(TestCase):
46+
def test_basic(self):
47+
a = np.array([[1, 2], [3, 4]])
48+
a_copy = np.copy(a)
49+
assert_array_equal(a, a_copy)
50+
a_copy[0, 0] = 10
51+
assert_equal(a[0, 0], 1)
52+
assert_equal(a_copy[0, 0], 10)
53+
54+
def test_order(self):
55+
# It turns out that people rely on np.copy() preserving order by
56+
# default; changing this broke scikit-learn:
57+
# https://github.com/scikit-learn/scikit-learn/commit/7842748cf777412c506a8c0ed28090711d3a3783
58+
a = np.array([[1, 2], [3, 4]])
59+
assert_(a.flags.c_contiguous)
60+
assert_(not a.flags.f_contiguous)
61+
a_fort = np.array([[1, 2], [3, 4]], order="F")
62+
assert_(not a_fort.flags.c_contiguous)
63+
assert_(a_fort.flags.f_contiguous)
64+
a_copy = np.copy(a)
65+
assert_(a_copy.flags.c_contiguous)
66+
assert_(not a_copy.flags.f_contiguous)
67+
a_fort_copy = np.copy(a_fort)
68+
assert_(not a_fort_copy.flags.c_contiguous)
69+
assert_(a_fort_copy.flags.f_contiguous)
70+
4571
class TestAverage(TestCase):
4672
def test_basic(self):
4773
y1 = np.array([1, 2, 3])

0 commit comments

Comments
 (0)