Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning NumPy Array

You're reading from  Learning NumPy Array

Product type Book
Published in Jun 2014
Publisher
ISBN-13 9781783983902
Pages 164 pages
Edition 1st Edition
Languages
Author (1):
Ivan Idris Ivan Idris
Profile icon Ivan Idris

Table of Contents (14) Chapters

Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with NumPy NumPy Basics Basic Data Analysis with NumPy Simple Predictive Analytics with NumPy Signal Processing Techniques Profiling, Debugging, and Testing The Scientific Python Ecosystem Index

Manipulating array shapes


Another recurring task is flattening of arrays. Flattening in this context means transforming a multidimensional array into a one-dimensional array. In this example, we will demonstrate a number of ways to manipulate array shapes starting with flattening:

  • ravel(): We can accomplish flattening with the ravel() function (see the shapemanipulation.py file in the Chapter02 folder of this book's code bundle), as shown in the following code:

    In: b
    Out:
    array([[[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]],[[12, 13, 14, 15],[16, 17, 18, 19],[20, 21, 22, 23]]])
    In: b.ravel()
    Out:
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
  • flatten(): The appropriately-named function, flatten(), does the same as ravel(), but flatten() always allocates new memory, whereas ravel() might return a view of an array. This means that we can directly manipulate the array as follows:

    In: b.flatten()
    Out:
    array([ 0,  1,  2,  3,  4,  5...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}