Search icon CANCEL
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
NumPy Cookbook

You're reading from   NumPy Cookbook If you're a Python developer with basic NumPy skills, the 70+ recipes in this brilliant cookbook will boost your skills in no time. Learn to raise productivity levels and code faster and cleaner with the open source mathematical library.

Arrow left icon
Product type Paperback
Published in Oct 2012
Publisher Packt
ISBN-13 9781849518925
Length 226 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (17) Chapters Close

NumPy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Winding Along with IPython 2. Advanced Indexing and Array Concepts FREE CHAPTER 3. Get to Grips with Commonly Used Functions 4. Connecting NumPy with the Rest of the World 5. Audio and Image Processing 6. Special Arrays and Universal Functions 7. Profiling and Debugging 8. Quality Assurance 9. Speed Up Code with Cython 10. Fun with Scikits Index

Importing a web notebook


Python scripts can be imported as a web notebook. Obviously, we can also import previously exported notebooks.

How to do it...

The following steps show how a python script can be imported as a web notebook:

  1. Import a python script by dragging it from Explorer or Finder into the notebook page. The following screenshot is an example of what we see after dragging the vectorsum.py from NumPy Beginner's Guide into the notebook page:

  2. Click the Upload button to import the program. IPython does a decent job of importing the code. Unfortunately, as shown in the following screenshot, the code is all placed in one cell. At least that is how it worked at the time of writing:

  3. Tag the script for multiple cells.

    In order to split the code into multiple cells we need to use special tags. These tags are in fact Python comments, but they look a bit like XML tags. The code has to start with the following tag:

    # <nbformat>2</nbformat>

    This indicates the format of the notebook. Each new code cell is indicated with the following tag:

    # <codecell>

    The following is the tagged code:

    # <nbformat>2</nbformat>
    #!/usr/bin/env/python
    
    from datetime import datetime
    import numpy
    """
     Chapter 1 of NumPy Beginners Guide.
     This program demonstrates vector addition the Python way.
     Run from the command line as follows
         
      python vectorsum.py n
     
     where n is an integer that specifies the size of the vectors.
    
     The first vector to be added contains the squares of 0 up to n. 
     The second vector contains the cubes of 0 up to n.
     The program prints the last 2 elements of the sum and the elapsed time.
    """
    
    def numpysum(n):
       a = numpy.arange(n) ** 2
       b = numpy.arange(n) ** 3
       c = a + b
    
       return c
    
    def pythonsum(n):
       a = range(n)
       b = range(n)
       c = []
    
       for i in range(len(a)):
           a[i] = i ** 2
           b[i] = i ** 3
           c.append(a[i] + b[i])
    
       return c
       
    # <codecell>
    size = int(50)
    
    # <codecell>
    start = datetime.now()
    c = pythonsum(size)
    delta = datetime.now() - start
    print "The last 2 elements of the sum", c[-2:]
    print "PythonSum elapsed time in microseconds", delta.microseconds
    
    # <codecell>
    start = datetime.now()
    c = numpysum(size)
    delta = datetime.now() - start
    print "The last 2 elements of the sum", c[-2:]
    print "NumPySum elapsed time in microseconds", delta.microseconds

    The code is split into several cells according to the tags, as shown in the following screenshot:

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