Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning NumPy Array

You're reading from   Learning NumPy Array Supercharge your scientific Python computations by understanding how to use the NumPy library effectively

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783983902
Length 164 pages
Edition Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ivan Idris Ivan Idris
Author Profile Icon Ivan Idris
Ivan Idris
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with NumPy FREE CHAPTER 2. NumPy Basics 3. Basic Data Analysis with NumPy 4. Simple Predictive Analytics with NumPy 5. Signal Processing Techniques 6. Profiling, Debugging, and Testing 7. 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 $19.99/month. Cancel anytime
Banner background image