24

Numpy v 1.9 contains two seemingly identical functions: 'flatten' and 'ravel'

What is the difference? and when might I pick one vs the other for converting a 2-D np.array to 1-D?

Bryan P
  • 5,900
  • 5
  • 34
  • 49
  • 5
    If you read the docs you linked to, note that `flatten` will always *"Return a copy"*, whereas `ravel` will make a copy *"only if needed"*. – jonrsharpe Mar 03 '15 at 16:51
  • 3
    I did read that in the docs, but what is meant by only if needed? What are cases where it is not needed? How does NP know if it is needed? It also seems in further testing that there are cases where flatten will not work on an array, but ravel will. Still not sure why. This is the type of information I was hoping to solicit with the question. – Bryan P Mar 04 '15 at 03:51
  • 2
    Then perhaps that's what you should have asked... – jonrsharpe Mar 04 '15 at 07:10
  • Thanks for the helpful link, but it certainly doesn't answer my question which is more general: What is the difference between these two functions and why pick one over the other. Check out my answer below. It may be basic, but a key difference is that one is a method and the other a function. This distinction is certainly not addressed in the other question and goes a long way to explaining why there are two "functions". These and perhaps other distinctions also make my question NOT a duplicate so I kindly ask that you remove that misleading flag. – Bryan P Mar 06 '15 at 04:00
  • @NikanaReklawyks: I asked my question 5 days before that [link](http://stackoverflow.com/q/28930465/1224158); so it should be the one marked as a duplicate. – Bryan P Nov 26 '16 at 02:37
  • 2
    @BryanP: on SO popularity is a thing :') – Nikana Reklawyks Nov 28 '16 at 10:47

1 Answers1

42

Aha: The primary functional difference is thatflatten is a method of an ndarray object and hence can only be called for true numpy arrays. In contrast ravel() is a library-level function and hence can be called on any object that can successfully be parsed. For example ravel() will work on a list of ndarrays, while flatten (obviously) won't.

In addition, as @jonrsharpe pointed out in his comment, the flatten method always returns a copy, while ravel only does so "if needed." Still not quite sure how this determination is made.

alex
  • 10,900
  • 15
  • 70
  • 100
Bryan P
  • 5,900
  • 5
  • 34
  • 49
  • Even if you are accessing your array via a multidimensional index, in memory the elements are still addressed linear-sequentially (using a simple raster-scan pattern to transform the multi-index to a linear-index). I would expect numpy to return a "view" if you are asking for the elements to be in same sequence as they are already stored in memory (and only to return a copy if you requested a dimension-ordering that necessitates the sequence of elements in-memory to be reshuffled). – benjimin Feb 14 '17 at 01:26