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

Using IPython as a shell


Scientists and engineers are used to experimenting. IPython was created by scientists with experimentation in mind. The interactive environment that IPython provides is viewed by many as a direct answer to MATLAB, Mathematica, Maple, and R.

The following is a list of features of the IPython shell:

  • Tab completion

  • History mechanism

  • Inline editing

  • The ability to call external Python scripts with %run

  • The ability to call magic functions that interact with the operating system shell

  • Access to system commands

  • The pylab switch

  • Access to Python debugger and profiler

How to do it...

This section describes how to use the IPython shell:

  • pylab: The pylab switch automatically imports all the SciPy, NumPy, and matplotlib packages. Without this switch, we would have to import these packages ourselves.

    All we need to do is enter the following instruction on the command line:

    $ ipython --pylab
    Type "copyright", "credits" or "license" for more information.
    
    IPython 2.4.1 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    Welcome to pylab, a matplotlib-based Python environment [backend: MacOSX].
    For more information, type 'help(pylab)'.
    
    In [1]: quit()
    quit() or Ctrl + D quits the IPython shell.
    
  • Saving a session: We might want to be able to go back to our experiments. In IPython, it is easy to save a session for later use. This is done with the following command:

    In [1]: %logstart
    Activating auto-logging. Current session state plus future input saved.
    Filename       : ipython_log.py
    Mode           : rotate
    Output logging : False
    Raw input log  : False
    Timestamping   : False
    State          : active
    

    Logging can be switched off using this command:

    In [9]: %logoff
    Switching logging OFF
    
  • Executing a system shell command: You can execute a system shell command in the default IPython profile by prefixing the command with the ! symbol. For instance, the following input will get the current date:

    In [1]: !date
    

    In fact, any line prefixed with ! is sent to the system shell. We can also store the command output, as shown here:

    In [2]: thedate = !date
    In [3]: thedate
    
  • Displaying history: We can show the history of commands with the %hist command, like this:

    In [1]: a = 2 + 2
    
    In [2]: a
    Out[2]: 4
    
    In [3]: %hist
    a = 2 + 2
    a
    %hist
    

    This is a common feature in Command-line Interface (CLI) environments. We can also look up the history with the -g switch:

    In [5]: %hist -g a = 2
        1: a = 2 + 2
    

Tip

Downloading the example code

You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to get the files e-mailed directly to you.

How it works...

We saw a number of so-called magic functions in action. These functions start with the % character. If a magic function is used in a line by itself, the % prefix is optional.

See also