On Tue, Nov 26, 2013 at 10:30 AM, Peter Prettenhofer <
peter.prettenho...@gmail.com> wrote:
>
> Hi all,
>
> I'm currently modifying our tree code so that it runs on both fortran and
c continuous arrays. After some benchmarking I got aware of the following
numpy behavior that was contrary to what I was expecting::
>
> >>> X = # some feature matrix
> >>> X = np.asfortranarray(X)
> >>> X.flags.f_contiguous
> True
> >>> # so far so good
> >>> X_train = X[:1000]
> >>> X_train.flags.f_contiguous
> False
> >>> X_train.flags.c_contiguous
> False
> >>> # damn - seems like a view is neither c nor fortran continuous
> >>> X_train = X_train.copy() # lets materialize the view
> >>> X_train.flags.f_contiguous
> False
> >>> X_train.flags.c_contiguous
> True
>
> In the tree code, I check if an array is continuous - if not, I call
``np.asarray`` and set the ``order`` according to ``flags.f_contiguous`` or
``flags.c_contiguous``, however, in the case of views that does not work.
> How would you handle this case?
Check .f_contiguous and .c_contiguous. If neither is True, use
np.ascontiguousarray() to get a C-contiguous array. Or np.asfortranarray()
to get a Fortran-contiguous array if that's more convenient. np.asarray()
will not ensure either contiguity, just ndarray-ness.
--
Robert Kern
------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing
conversations that shape the rapidly evolving mobile landscape. Sign up now.
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
_______________________________________________
Scikit-learn-general mailing list
Scikit-learn-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general