Reader small image

You're reading from  Learning IPython for Interactive Computing and Data Visualization, Second Edition

Product typeBook
Published inOct 2015
Reading LevelBeginner
Publisher
ISBN-139781783986989
Edition1st Edition
Languages
Right arrow
Author (1)
Cyrille Rossant
Cyrille Rossant
author image
Cyrille Rossant

Cyrille Rossant, PhD, is a neuroscience researcher and software engineer at University College London. He is a graduate of École Normale Supérieure, Paris, where he studied mathematics and computer science. He has also worked at Princeton University and Collège de France. While working on data science and software engineering projects, he gained experience in numerical computing, parallel computing, and high-performance data visualization. He is the author of Learning IPython for Interactive Computing and Data Visualization, Second Edition, Packt Publishing.
Read more about Cyrille Rossant

Right arrow

Basic array manipulations


Let's see some basic array manipulations around multiplication tables.

In [1]: import numpy as np

We first create an array of integers between 1 and 10, as shown here:

In [2]: x = np.arange(1, 11)
In [3]: x
Out[3]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Note that in np.arange(start, end), start is included while end is excluded.

To create our multiplication table, we first need to transform x into a row and column vector. Our vector x is a 1D array, whereas row and column vectors are 2D arrays (also known as matrices). There are many ways to transform a 1D array to a 2D array. We will see the two most common methods here.

The first method is to use reshape():

In [4]: x_row = x.reshape((1, -1))
        x_row
Out[4]: array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]])

The reshape() method takes the new shape as parameter. The total number of elements must be unchanged. For example, reshaping a (2, 3) array to a (5,) array would raise an error. The number -1 can...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning IPython for Interactive Computing and Data Visualization, Second Edition
Published in: Oct 2015Publisher: ISBN-13: 9781783986989

Author (1)

author image
Cyrille Rossant

Cyrille Rossant, PhD, is a neuroscience researcher and software engineer at University College London. He is a graduate of École Normale Supérieure, Paris, where he studied mathematics and computer science. He has also worked at Princeton University and Collège de France. While working on data science and software engineering projects, he gained experience in numerical computing, parallel computing, and high-performance data visualization. He is the author of Learning IPython for Interactive Computing and Data Visualization, Second Edition, Packt Publishing.
Read more about Cyrille Rossant