Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

NumPy: Beginner's Guide - Third Edition
NumPy: Beginner's Guide - Third Edition
NumPy: Beginner's Guide - Third Edition
Ebook831 pages3 hours

NumPy: Beginner's Guide - Third Edition

Rating: 3.5 out of 5 stars

3.5/5

()

Read preview

About this ebook

About This Book
  • Written as a step-by-step guide, this book aims to give you a strong foundation in NumPy and breaks down its complex library features into simple tasks
  • Perform high performance calculations with clean and efficient NumPy code
  • Analyze large datasets with statistical functions and execute complex linear algebra and mathematical computations
Who This Book Is For

This book is for the scientists, engineers, programmers, or analysts looking for a high-quality, open source mathematical library. Knowledge of Python is assumed. Also, some affinity, or at least interest, in mathematics and statistics is required. However, I have provided brief explanations and pointers to learning resources.

LanguageEnglish
Release dateJun 24, 2015
ISBN9781785288838
NumPy: Beginner's Guide - Third Edition
Author

Ivan Idris

Ivan Idris has an MSc in Experimental Physics. His graduation thesis had a strong emphasis on Applied Computer Science. After graduating, he worked for several companies as a Java Developer, Data warehouse Developer, and QA Analyst. His main professional interests are Business Intelligence, Big Data, and Cloud Computing. Ivan Idris enjoys writing clean, testable code and interesting technical articles. Ivan Idris is the author of NumPy 1.5 Beginner's Guide and NumPy Cookbook by Packt Publishing. You can find more information and a blog with a few NumPy examples at ivanidris.net.

Read more from Ivan Idris

Related to NumPy

Related ebooks

Applications & Software For You

View More

Related articles

Reviews for NumPy

Rating: 3.6666666666666665 out of 5 stars
3.5/5

3 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    NumPy - Ivan Idris

    Table of Contents

    NumPy Beginner's Guide Third Edition

    Credits

    About the Author

    About the Reviewers

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    Why subscribe?

    Free access for Packt account holders

    Preface

    What is NumPy?

    History

    Why use NumPy?

    Limitations of NumPy

    What this book covers

    What you need for this book

    Who this book is for

    Sections

    Time for action – heading

    What just happened?

    Pop quiz – heading

    Have a go hero – heading

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book

    Errata

    Piracy

    Questions

    1. NumPy Quick Start

    Python

    Time for action – installing Python on different operating systems

    What just happened?

    The Python help system

    Time for action – using the Python help system

    What just happened?

    Basic arithmetic and variable assignment

    Time for action – using Python as a calculator

    What just happened?

    Time for action – assigning values to variables

    What just happened?

    The print() function

    Time for action – printing with the print() function

    What just happened?

    Code comments

    Time for action – commenting code

    The if statement

    Time for action – deciding with the if statement

    What just happened?

    The for loop

    Time for action – repeating instructions with loops

    What just happened?

    Python functions

    Time for action – defining functions

    What just happened?

    Python modules

    Time for action – importing modules

    What just happened?

    NumPy on Windows

    Time for action – installing NumPy, matplotlib, SciPy, and IPython on Windows

    What just happened?

    NumPy on Linux

    Time for action – installing NumPy, matplotlib, SciPy, and IPython on Linux

    NumPy on Mac OS X

    Time for action – installing NumPy, SciPy, matplotlib, and IPython with MacPorts or Fink

    What just happened?

    Building from source

    Arrays

    Time for action – adding vectors

    What just happened?

    Pop quiz – Functioning of the arange() function

    Have a go hero – continue the analysis

    IPython – an interactive shell

    Online resources and help

    Summary

    2. Beginning with NumPy Fundamentals

    NumPy array object

    Time for action – creating a multidimensional array

    What just happened?

    Pop quiz – the shape of ndarray

    Have a go hero – create a three-by-three array

    Selecting elements

    NumPy numerical types

    Data type objects

    Character codes

    The dtype constructors

    The dtype attributes

    Time for action – creating a record data type

    What just happened?

    One-dimensional slicing and indexing

    Time for action – slicing and indexing multidimensional arrays

    What just happened?

    Time for action – manipulating array shapes

    What just happened?

    Stacking

    Time for action – stacking arrays

    What just happened?

    Splitting

    Time for action – splitting arrays

    What just happened?

    Array attributes

    Time for action – converting arrays

    What just happened?

    Summary

    3. Getting Familiar with Commonly Used Functions

    File I/O

    Time for action – reading and writing files

    What just happened?

    Comma-seperated value files

    Time for action – loading from CSV files

    Volume Weighted Average Price

    Time for action – calculating Volume Weighted Average Price

    What just happened?

    The mean() function

    Time-weighted average price

    Pop quiz – computing the weighted average

    Have a go hero – calculating other averages

    Value range

    Time for action – finding highest and lowest values

    What just happened?

    Statistics

    Time for action – performing simple statistics

    What just happened?

    Stock returns

    Time for action – analyzing stock returns

    What just happened?

    Dates

    Time for action – dealing with dates

    What just happened?

    Have a go hero – looking at VWAP and TWAP

    Time for action – using the datetime64 data type

    What just happened?

    Weekly summary

    Time for action – summarizing data

    What just happened?

    Have a go hero – improving the code

    Average True Range

    Time for action – calculating the Average True Range

    What just happened?

    Have a go hero – taking the minimum() function for a spin

    Simple Moving Average

    Time for action – computing the Simple Moving Average

    What just happened?

    Exponential Moving Average

    Time for action – calculating the Exponential Moving Average

    What just happened?

    Bollinger Bands

    Time for action – enveloping with Bollinger Bands

    What just happened?

    Have a go hero – switching to Exponential Moving Average

    Linear model

    Time for action – predicting price with a linear model

    What just happened?

    Trend lines

    Time for action – drawing trend lines

    What just happened?

    Methods of ndarray

    Time for action – clipping and compressing arrays

    What just happened?

    Factorial

    Time for action – calculating the factorial

    What just happened?

    Missing values and Jackknife resampling

    Time for action – handling NaNs with the nanmean(), nanvar(), and nanstd() functions

    What just happened?

    Summary

    4. Convenience Functions for Your Convenience

    Correlation

    Time for action – trading correlated pairs

    What just happened?

    Pop quiz – calculating covariance

    Polynomials

    Time for action – fitting to polynomials

    What just happened?

    Have a go hero – improving the fit

    On-balance volume

    Time for action – balancing volume

    What just happened?

    Simulation

    Time for action – avoiding loops with vectorize()

    What just happened?

    Have a go hero – analyzing consecutive wins and losses

    Smoothing

    Time for action – smoothing with the hanning() function

    What just happened?

    Have a go hero – smoothing variations

    Initialization

    Time for action – creating value initialized arrays with the full() and full_like() functions

    What just happened?

    Summary

    5. Working with Matrices and ufuncs

    Matrices

    Time for action – creating matrices

    What just happened?

    Creating a matrix from other matrices

    Time for action – creating a matrix from other matrices

    What just happened?

    Pop quiz – defining a matrix with a string

    Universal functions

    Time for action – creating universal functions

    What just happened?

    Universal function methods

    Time for action – applying the ufunc methods to the add function

    What just happened?

    Arithmetic functions

    Time for action – dividing arrays

    What just happened?

    Have a go hero – experimenting with __future__.division

    Modulo operation

    Time for action – computing the modulo

    What just happened?

    Fibonacci numbers

    Time for action – computing Fibonacci numbers

    What just happened?

    Have a go hero – timing the calculations

    Lissajous curves

    Time for action – drawing Lissajous curves

    What just happened?

    Square waves

    Time for action – drawing a square wave

    What just happened?

    Have a go hero – getting rid of the loop

    Sawtooth and triangle waves

    Time for action – drawing sawtooth and triangle waves

    What just happened?

    Have a go hero – getting rid of the loop

    Bitwise and comparison functions

    Time for action – twiddling bits

    What just happened?

    Fancy indexing

    Time for action – fancy indexing in-place for ufuncs with the at() method

    What just happened?

    Summary

    6. Moving Further with NumPy Modules

    Linear algebra

    Time for action – inverting matrices

    What just happened?

    Pop quiz – creating a matrix

    Have a go hero – inverting your own matrix

    Solving linear systems

    Time for action – solving a linear system

    What just happened?

    Finding eigenvalues and eigenvectors

    Time for action – determining eigenvalues and eigenvectors

    What just happened?

    Singular value decomposition

    Time for action – decomposing a matrix

    What just happened?

    Pseudo inverse

    Time for action – computing the pseudo inverse of a matrix

    What just happened?

    Determinants

    Time for action – calculating the determinant of a matrix

    What just happened?

    Fast Fourier transform

    Time for action – calculating the Fourier transform

    What just happened?

    Shifting

    Time for action – shifting frequencies

    What just happened?

    Random numbers

    Time for action – gambling with the binomial

    What just happened?

    Hypergeometric distribution

    Time for action – simulating a game show

    What just happened?

    Continuous distributions

    Time for action – drawing a normal distribution

    What just happened?

    Lognormal distribution

    Time for action – drawing the lognormal distribution

    What just happened?

    Bootstrapping in statistics

    Time for action – sampling with numpy.random.choice()

    What just happened?

    Summary

    7. Peeking into Special Routines

    Sorting

    Time for action – sorting lexically

    What just happened?

    Have a go hero – trying a different sort order

    Time for action – partial sorting via selection for a fast median with the partition() function

    What just happened?

    Complex numbers

    Time for action – sorting complex numbers

    What just happened?

    Pop quiz – generating random numbers

    Searching

    Time for action – using searchsorted

    What just happened?

    Array elements extraction

    Time for action – extracting elements from an array

    What just happened?

    Financial functions

    Time for action – determining the future value

    What just happened?

    Present value

    Time for action – getting the present value

    What just happened?

    Net present value

    Time for action – calculating the net present value

    What just happened?

    Internal rate of return

    Time for action – determining the internal rate of return

    What just happened?

    Periodic payments

    Time for action – calculating the periodic payments

    What just happened?

    Number of payments

    Time for action – determining the number of periodic payments

    What just happened?

    Interest rate

    Time for action – figuring out the rate

    What just happened?

    Window functions

    Time for action – plotting the Bartlett window

    What just happened?

    Blackman window

    Time for action – smoothing stock prices with the Blackman window

    What just happened?

    Hamming window

    Time for action – plotting the Hamming window

    What just happened?

    Kaiser window

    Time for action – plotting the Kaiser window

    What just happened?

    Special mathematical functions

    Time for action – plotting the modified Bessel function

    What just happened?

    sinc

    Time for action – plotting the sinc function

    What just happened?

    Summary

    8. Assuring Quality with Testing

    Assert functions

    Time for action – asserting almost equal

    What just happened?

    Pop quiz – specifying decimal precision

    Approximately equal arrays

    Time for action – asserting approximately equal

    What just happened?

    Almost equal arrays

    Time for action – asserting arrays almost equal

    What just happened?

    Have a go hero – comparing arrays with different shapes

    Equal arrays

    Time for action – comparing arrays

    What just happened?

    Ordering arrays

    Time for action – checking the array order

    What just happened?

    Object comparison

    Time for action – comparing objects

    What just happened?

    String comparison

    Time for action – comparing strings

    What just happened?

    Floating-point comparisons

    Time for action – comparing with assert_array_almost_equal_nulp

    What just happened?

    Comparison of floats with more ULPs

    Time for action – comparing using maxulp of 2

    What just happened?

    Unit tests

    Time for action – writing a unit test

    What just happened?

    Nose test decorators

    Time for action – decorating tests

    What just happened?

    Docstrings

    Time for action – executing doctests

    What just happened?

    Summary

    9. Plotting with matplotlib

    Simple plots

    Time for action – plotting a polynomial function

    What just happened?

    Pop quiz – the plot() function

    Plot format string

    Time for action – plotting a polynomial and its derivatives

    What just happened?

    Subplots

    Time for action – plotting a polynomial and its derivatives

    What just happened?

    Finance

    Time for action – plotting a year's worth of stock quotes

    What just happened?

    Histograms

    Time for action – charting stock price distributions

    What just happened?

    Have a go hero – drawing a bell curve

    Logarithmic plots

    Time for action – plotting stock volume

    What just happened?

    Scatter plots

    Time for action – plotting price and volume returns with a scatter plot

    What just happened?

    Fill between

    Time for action – shading plot regions based on a condition

    What just happened?

    Legend and annotations

    Time for action – using a legend and annotations

    What just happened?

    Three-dimensional plots

    Time for action – plotting in three dimensions

    What just happened?

    Contour plots

    Time for action – drawing a filled contour plot

    What just happened?

    Animation

    Time for action – animating plots

    What just happened?

    Summary

    10. When NumPy Is Not Enough – SciPy and Beyond

    MATLAB and Octave

    Time for action – saving and loading a .mat file

    What just happened?

    Pop quiz – loading .mat files

    Statistics

    Time for action – analyzing random values

    What just happened?

    Have a go hero – improving the data generation

    Sample comparison and SciKits

    Time for action – comparing stock log returns

    What just happened?

    Signal processing

    Time for action – detecting a trend in QQQ

    What just happened?

    Fourier analysis

    Time for action – filtering a detrended signal

    What just happened?

    Mathematical optimization

    Time for action – fitting to a sine

    What just happened?

    Numerical integration

    Time for action – calculating the Gaussian integral

    What just happened?

    Have a go hero – experiment a bit more

    Interpolation

    Time for action – interpolating in one dimension

    What just happened?

    Image processing

    Time for action – manipulating Lena

    What just happened?

    Audio processing

    Time for action – replaying audio clips

    What just happened?

    Summary

    11. Playing with Pygame

    Pygame

    Time for action – installing Pygame

    Hello World

    Time for action – creating a simple game

    What just happened?

    Animation

    Time for action – animating objects with NumPy and Pygame

    What just happened?

    matplotlib

    Time for Action – using matplotlib in Pygame

    What just happened?

    Surface pixels

    Time for Action – accessing surface pixel data with NumPy

    What just happened?

    Artificial Intelligence

    Time for Action – clustering points

    What just happened?

    OpenGL and Pygame

    Time for Action – drawing the Sierpinski gasket

    What just happened?

    Simulation game with Pygame

    Time for Action – simulating life

    What just happened?

    Summary

    A. Pop Quiz Answers

    Chapter 1, NumPy Quick Start

    Pop quiz – functioning of the arange() function

    Chapter 2, Beginning with NumPy Fundamentals

    Pop quiz – the shape of ndarray

    Chapter 3, Getting Familiar with Commonly Used Functions

    Pop quiz – computing the weighted average

    Chapter 4, Convenience Functions for Your Convenience

    Pop quiz – calculating covariance

    Chapter 5, Working with Matrices and ufuncs

    Pop quiz – defining a matrix with a string

    Chapter 6, Move Further with NumPy Modules

    Pop quiz – creating a matrix

    Chapter 7, Peeking into Special Routines

    Pop quiz – generating random numbers

    Chapter 8, Assuring Quality with Testing

    Pop quiz – specifying decimal precision

    Chapter 9, Plotting with matplotlib

    Pop quiz – the plot() function

    Chapter 10, When NumPy Is Not Enough –Scipy and Beyond

    Pop quiz – loading .mat files

    B. Additional Online Resources

    Python

    Mathematics and statistics

    C. NumPy Functions' References

    Index

    NumPy Beginner's Guide Third Edition


    NumPy Beginner's Guide Third Edition

    Copyright © 2015 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    First published: November 2011

    Second edition: April 2013

    Third edition: June 2015

    Production reference: 1160615

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-78528-196-9

    www.packtpub.com

    Credits

    Author

    Ivan Idris

    Reviewers

    Alexandre Devert

    Davide Fiacconi

    Ardo Illaste

    Commissioning Editor

    Amarabha Banerjee

    Acquisition Editors

    Shaon Basu

    Usha Iyer

    Rebecca Youe

    Content Development Editor

    Neeshma Ramakrishnan

    Technical Editor

    Rupali R. Shrawane

    Copy Editors

    Charlotte Carneiro

    Vikrant Phadke

    Sameen Siddiqui

    Project Coordinator

    Shweta H. Birwatkar

    Proofreader

    Safis Editing

    Indexer

    Rekha Nair

    Graphics

    Sheetal Aute

    Jason Monteiro

    Production Coordinator

    Aparna Bhagat

    Cover Work

    Aparna Bhagat

    About the Author

    Ivan Idris has an MSc in experimental physics. His graduation thesis had a strong emphasis on applied computer science. After graduating, he worked for several companies as a Java developer, data warehouse developer, and QA Analyst. His main professional interests are business intelligence, big data, and cloud computing. Ivan enjoys writing clean, testable code and interesting technical articles. He is the author of NumPy Beginner's Guide, NumPy Cookbook, Learning NumPy Array, and Python Data Analysis. You can find more information about him and a blog with a few examples of NumPy at http://ivanidris.net/wordpress/.

    I would like to take this opportunity to thank the reviewers and the team at Packt Publishing for making this book possible. Also thanks go to my teachers, professors, colleagues, Wikipedia contributors, Stack Overflow contributors, and other authors who taught me science and programming. Last but not least, I would like to acknowledge my parents, family, and friends for their support.

    About the Reviewers

    Davide Fiacconi is completing his PhD in theoretical astrophysics from the Institute for Computational Science at the University of Zurich. He did his undergraduate and graduate studies at the University of Milan-Bicocca, studying the evolution of collisional ring galaxies using hydrodynamic numerical simulations. Davide's research now focuses on the formation and coevolution of supermassive black holes and galaxies, using both massively parallel simulations and analytical techniques. In particular, his interests include the formation of the first supermassive black hole seeds, the dynamics of binary black holes, and the evolution of high-redshift galaxies.

    Ardo Illaste is a data scientist. He wants to provide everyone with easy access to data for making major life and career decisions. He completed his PhD in computational biophysics, prior to fully delving into data mining and machine learning. Ardo has worked and studied in Estonia, the USA, and Switzerland.

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    For support files and downloads related to your book, please visit www.PacktPub.com.

    Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

    At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

    https://www2.packtpub.com/books/subscription/packtlib

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Free access for Packt account holders

    If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

    I dedicate this book to my aunt Lies who recently passed away. Rest in peace.

    Preface

    Scientists, engineers, and quantitative data analysts face many challenges nowadays. Data scientists want to be able to perform numerical analysis on large datasets with minimal programming effort. They also want to write readable, efficient, and fast code that is as close as possible to the mathematical language they are used to. A number of accepted solutions are available in the scientific computing world.

    The C, C++, and Fortran programming languages have their benefits, but they are not interactive and considered too complex by many. The common commercial alternatives, such as MATLAB, Maple, and Mathematica, provide powerful scripting languages that are even more limited than any general-purpose programming language. Other open source tools similar to MATLAB exist, such as R, GNU Octave, and Scilab. Obviously, they too lack the power of a language such as Python.

    Python is a popular general-purpose programming language that is widely used in the scientific community. You can access legacy C, Fortran, or R code easily from Python. It is object-oriented and considered to be of a higher level than C or Fortran. It allows you to write readable and clean code with minimal fuss. However, it lacks an out-of-the-box MATLAB equivalent. That's where NumPy comes in. This book is about NumPy and related Python libraries, such as SciPy and matplotlib.

    What is NumPy?

    NumPy (short for numerical Python) is an open source Python library for scientific computing. It lets you work with arrays and matrices in a natural way. The library contains a long list of useful mathematical functions, including some functions for linear algebra, Fourier transformation, and random number generation routines. LAPACK, a linear algebra library, is used by the NumPy linear algebra module if you have it installed on your system. Otherwise, NumPy provides its own implementation. LAPACK is a well-known library, originally written in Fortran, on which MATLAB relies as well. In a way, NumPy replaces some of the functionality of MATLAB and Mathematica, allowing rapid interactive prototyping.

    We will not be discussing NumPy from a developing contributor's perspective, but from more of a user's perspective. NumPy is a very active project and has a lot of contributors. Maybe,

    Enjoying the preview?
    Page 1 of 1