What is the difference between flatten and ravel functions in numpy?

|

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

import numpy as np
y = np.array(((1,2,3),(4,5,6),(7,8,9)))
OUTPUT:
print(y.flatten())
[1   2   3   4   5   6   7   8   9]
print(y.ravel())
[1   2   3   4   5   6   7   8   9]

Both function return the same list. Then what is the need of two different functions performing same job.

👻 Read also: what is the best laptop for engineering students?

What is the difference between flatten and ravel functions in numpy? ravel: Questions

What is the difference between flatten and ravel functions in numpy?

2 answers

import numpy as np
y = np.array(((1,2,3),(4,5,6),(7,8,9)))
OUTPUT:
print(y.flatten())
[1   2   3   4   5   6   7   8   9]
print(y.ravel())
[1   2   3   4   5   6   7   8   9]

Both function return the same list. Then what is the need of two different functions performing same job.

357

Answer #1

The current API is that:

  • flatten always returns a copy.
  • ravel returns a view of the original array whenever possible. This isn"t visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel will often be faster since no memory is copied, but you have to be more careful about modifying the array it returns.
  • reshape((-1,)) gets a view whenever the strides of the array allow it even if that means you don"t always get a contiguous array.

What is the difference between flatten and ravel functions in numpy? ravel: Questions

List to array conversion to use ravel() function

2 answers

I have a list in python and I want to convert it to an array to be able to use ravel() function.

110

Answer #1

Use numpy.asarray:

import numpy as np
myarray = np.asarray(mylist)

We hope this article has helped you to resolve the problem. Apart from What is the difference between flatten and ravel functions in numpy?, check other ravel-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Ken Chamberlet

Munchen | 2022-11-26

Simply put and clear. Thank you for sharing. What is the difference between flatten and ravel functions in numpy? and other issues with ravel was always my weak point 😁. I just hope that will not emerge anymore

Manuel Galleotti

Prague | 2022-11-26

Simply put and clear. Thank you for sharing. What is the difference between flatten and ravel functions in numpy? and other issues with ravel was always my weak point 😁. Checked yesterday, it works!

Cornwall Zelotti

Paris | 2022-11-26

I was preparing for my coding interview, thanks for clarifying this - What is the difference between flatten and ravel functions in numpy? in Python is not the simplest one. Checked yesterday, it works!

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically