Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Running an IPython notebook


IPython has an exciting feature—the web notebook. A so-called notebook server can serve notebooks over the Web. We can now start a notebook server and get a web-based IPython environment. This environment has most of the features that the regular IPython environment has. The IPython notebook's features include the following:

  • Displaying images and inline plots

  • Using HTML and Markdown (this is a simplified HTML-like language see https://en.wikipedia.org/wiki/Markdown) in text cells

  • Importing and exporting of notebooks

Getting ready

Before we start, we should make sure that all of the required software is installed. There is a dependency on tornado and zmq. See the Installing IPython recipe in this chapter for more information.

How to do it...

  • Running a notebook: We can start a notebook with the following command:

    $ ipython notebook
    
    [NotebookApp] Using existing profile dir: u'/Users/ivanidris/.ipython/profile_default'
    [NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888
    [NotebookApp] Use Control-C to stop this server and shut down all kernels.
    

    As you can see, we are using the default profile. A server started on the local machine at port 8888. You will learn how to configure these settings later on in this chapter. The notebook is opened in your default browser; this is configurable as well (see the following screenshot):

    IPython lists all the notebooks in the directory where you started the notebook. In this example, no notebooks were found. The server can be stopped by pressing Ctrl + C.

  • Running a notebook in the pylab mode: Run a web notebook in the pylab mode with the following command:

    $ ipython notebook --pylab
    

    This loads the SciPy, NumPy, and matplotlib modules.

  • Running a notebook with inline figures: We can display inline matplotlib plots with the inline directive using the following command:

    $ ipython notebook --pylab inline
    

    The following steps demonstrate the IPython notebook functionality:

    1. Click on the New Notebook button to create a new notebook.

    2. Create an array with the arange() function. Type the command shown in the following screenshot and click on Cell/Run:

    3. Next enter the following command and press Enter. You will see the output in Out [2], as shown in the following screenshot:

    4. Apply the sinc() function to the array and plot the result, as shown in this screenshot:

How it works...

The inline option lets you display inline matplotlib plots. When combined with the pylab mode, you don't need to import the NumPy, SciPy, and matplotlib packages.

See also