Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
IPython Notebook Essentials

You're reading from   IPython Notebook Essentials Compute scientific data and execute code interactively with NumPy and SciPy

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783988341
Length 190 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Luiz Felipe Martins Luiz Felipe Martins
Author Profile Icon Luiz Felipe Martins
Luiz Felipe Martins
Arrow right icon
View More author details
Toc

Indexing and Slicing

To illustrate indexing, let's first create an array with random data using the following command:

import numpy.random
a = np.random.rand(6,5)
print a

This creates an array of dimension (6,5) that contains random data. Individual elements of the array are accessed with the usual index notation, for example, a[2,4].

An important technique to manipulate data in NumPy is the use of slices. A slice can be thought of as a subarray of an array. For example, let's say we want to extract a subarray with the middle two rows and first two columns of the array a. Consider the following command lines:

b = a[2:4,0:2]
print b

Now, let's make a very important observation. A slice is simply a view of an array, and no data is actually copied. This can be seen by running the following commands:

b[0,0]=0
print a

So, changes in b affect the array a! If we really need a copy, we need to explicitly say we want one. This can be done using the following command line:

c = np.copy(a...
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