08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You will also find the book NumPy Beginner's Guide - Second Edition, Ivan Idris,<br />

Packt Publishing very valuable. Additional tutorial style guides are at http://<br />

scipy-lectures.github.com; you may also visit the official SciPy tutorial at<br />

http://docs.scipy.org/doc/scipy/reference/tutorial.<br />

In this book, we will use NumPy Version 1.6.2 and SciPy Version 0.11.0.<br />

Chapter 1<br />

<strong>Learning</strong> NumPy<br />

So let us import NumPy and play a bit <strong>with</strong> it. For that, we need to start the <strong>Python</strong><br />

interactive shell.<br />

>>> import numpy<br />

>>> numpy.version.full_version<br />

1.6.2<br />

As we do not want to pollute our namespace, we certainly should not do the following:<br />

>>> from numpy import *<br />

The numpy.array array will potentially shadow the array package that is included<br />

in standard <strong>Python</strong>. Instead, we will use the following convenient shortcut:<br />

>>> import numpy as np<br />

>>> a = np.array([0,1,2,3,4,5])<br />

>>> a<br />

array([0, 1, 2, 3, 4, 5])<br />

>>> a.ndim<br />

1<br />

>>> a.shape<br />

(6,)<br />

We just created an array in a similar way to how we would create a list in <strong>Python</strong>.<br />

However, NumPy arrays have additional information about the shape. In this case,<br />

it is a one-dimensional array of five elements. No surprises so far.<br />

We can now transform this array in to a 2D matrix.<br />

>>> b = a.reshape((3,2))<br />

>>> b<br />

array([[0, 1],<br />

[2, 3],<br />

[4, 5]])<br />

>>> b.ndim<br />

2<br />

>>> b.shape<br />

(3, 2)<br />

[ 13 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!