O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NumPy : Beginner's Guide - Third Edition

Book Description

Build efficient, high-speed programs using the high-performance NumPy mathematical library

In Detail

In today's world of science and technology, it's all about speed and flexibility. When it comes to scientific computing, NumPy tops the list. NumPy will give you both speed and high productivity. This book will walk you through NumPy with clear, step-by-step examples and just the right amount of theory. The book focuses on the fundamentals of NumPy, including array objects, functions, and matrices, each of them explained with practical examples. You will then learn about different NumPy modules while performing mathematical operations such as calculating the Fourier transform, finding the inverse of a matrix, and determining eigenvalues, among many others. This book is a one-stop solution to knowing the ins and outs of the vast NumPy library, empowering you to use its wide range of mathematical features to build efficient, high-speed programs.

What You Will Learn

  • Install NumPy, matplotlib, SciPy, and IPython on various operating systems
  • Use NumPy array objects to perform array operations
  • Familiarize yourself with commonly used NumPy functions
  • Use NumPy matrices for matrix algebra
  • Work with the NumPy modules to perform various algebraic operations
  • Test NumPy code with the numpy.testing module
  • Plot simple plots, subplots, histograms, and more with matplotlib

Downloading the example code for this book. You can download the example code files for all Packt 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 have the files e-mailed directly to you.

Table of Contents

  1. NumPy Beginner's Guide Third Edition
    1. Table of Contents
    2. NumPy Beginner's Guide Third Edition
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What is NumPy?
      2. History
      3. Why use NumPy?
      4. Limitations of NumPy
      5. What this book covers
      6. What you need for this book
      7. Who this book is for
      8. Sections
      9. Time for action – heading
        1. What just happened?
        2. Pop quiz – heading
        3. Have a go hero – heading
      10. Conventions
      11. Reader feedback
      12. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. NumPy Quick Start
      1. Python
      2. Time for action – installing Python on different operating systems
        1. What just happened?
      3. The Python help system
      4. Time for action – using the Python help system
        1. What just happened?
      5. Basic arithmetic and variable assignment
      6. Time for action – using Python as a calculator
        1. What just happened?
      7. Time for action – assigning values to variables
        1. What just happened?
      8. The print() function
      9. Time for action – printing with the print() function
        1. What just happened?
      10. Code comments
      11. Time for action – commenting code
      12. The if statement
      13. Time for action – deciding with the if statement
        1. What just happened?
      14. The for loop
      15. Time for action – repeating instructions with loops
        1. What just happened?
      16. Python functions
      17. Time for action – defining functions
        1. What just happened?
      18. Python modules
      19. Time for action – importing modules
        1. What just happened?
      20. NumPy on Windows
      21. Time for action – installing NumPy, matplotlib, SciPy, and IPython on Windows
        1. What just happened?
      22. NumPy on Linux
      23. Time for action – installing NumPy, matplotlib, SciPy, and IPython on Linux
      24. NumPy on Mac OS X
      25. Time for action – installing NumPy, SciPy, matplotlib, and IPython with MacPorts or Fink
        1. What just happened?
      26. Building from source
      27. Arrays
      28. Time for action – adding vectors
        1. What just happened?
        2. Pop quiz – Functioning of the arange() function
        3. Have a go hero – continue the analysis
      29. IPython – an interactive shell
      30. Online resources and help
      31. Summary
    9. 2. Beginning with NumPy Fundamentals
      1. NumPy array object
      2. Time for action – creating a multidimensional array
        1. What just happened?
        2. Pop quiz – the shape of ndarray
        3. Have a go hero – create a three-by-three array
        4. Selecting elements
        5. NumPy numerical types
        6. Data type objects
        7. Character codes
        8. The dtype constructors
        9. The dtype attributes
      3. Time for action – creating a record data type
        1. What just happened?
      4. One-dimensional slicing and indexing
      5. Time for action – slicing and indexing multidimensional arrays
        1. What just happened?
      6. Time for action – manipulating array shapes
        1. What just happened?
        2. Stacking
      7. Time for action – stacking arrays
        1. What just happened?
        2. Splitting
      8. Time for action – splitting arrays
        1. What just happened?
        2. Array attributes
      9. Time for action – converting arrays
        1. What just happened?
      10. Summary
    10. 3. Getting Familiar with Commonly Used Functions
      1. File I/O
      2. Time for action – reading and writing files
        1. What just happened?
      3. Comma-seperated value files
      4. Time for action – loading from CSV files
      5. Volume Weighted Average Price
      6. Time for action – calculating Volume Weighted Average Price
        1. What just happened?
        2. The mean() function
        3. Time-weighted average price
        4. Pop quiz – computing the weighted average
        5. Have a go hero – calculating other averages
      7. Value range
      8. Time for action – finding highest and lowest values
        1. What just happened?
      9. Statistics
      10. Time for action – performing simple statistics
        1. What just happened?
      11. Stock returns
      12. Time for action – analyzing stock returns
        1. What just happened?
      13. Dates
      14. Time for action – dealing with dates
        1. What just happened?
        2. Have a go hero – looking at VWAP and TWAP
      15. Time for action – using the datetime64 data type
        1. What just happened?
      16. Weekly summary
      17. Time for action – summarizing data
        1. What just happened?
        2. Have a go hero – improving the code
      18. Average True Range
      19. Time for action – calculating the Average True Range
        1. What just happened?
        2. Have a go hero – taking the minimum() function for a spin
      20. Simple Moving Average
      21. Time for action – computing the Simple Moving Average
        1. What just happened?
      22. Exponential Moving Average
      23. Time for action – calculating the Exponential Moving Average
        1. What just happened?
      24. Bollinger Bands
      25. Time for action – enveloping with Bollinger Bands
        1. What just happened?
        2. Have a go hero – switching to Exponential Moving Average
      26. Linear model
      27. Time for action – predicting price with a linear model
        1. What just happened?
      28. Trend lines
      29. Time for action – drawing trend lines
        1. What just happened?
      30. Methods of ndarray
      31. Time for action – clipping and compressing arrays
        1. What just happened?
      32. Factorial
      33. Time for action – calculating the factorial
        1. What just happened?
      34. Missing values and Jackknife resampling
      35. Time for action – handling NaNs with the nanmean(), nanvar(), and nanstd() functions
        1. What just happened?
      36. Summary
    11. 4. Convenience Functions for Your Convenience
      1. Correlation
      2. Time for action – trading correlated pairs
        1. What just happened?
        2. Pop quiz – calculating covariance
      3. Polynomials
      4. Time for action – fitting to polynomials
        1. What just happened?
        2. Have a go hero – improving the fit
      5. On-balance volume
      6. Time for action – balancing volume
        1. What just happened?
      7. Simulation
      8. Time for action – avoiding loops with vectorize()
        1. What just happened?
        2. Have a go hero – analyzing consecutive wins and losses
      9. Smoothing
      10. Time for action – smoothing with the hanning() function
        1. What just happened?
        2. Have a go hero – smoothing variations
      11. Initialization
      12. Time for action – creating value initialized arrays with the full() and full_like() functions
        1. What just happened?
      13. Summary
    12. 5. Working with Matrices and ufuncs
      1. Matrices
      2. Time for action – creating matrices
        1. What just happened?
      3. Creating a matrix from other matrices
      4. Time for action – creating a matrix from other matrices
        1. What just happened?
        2. Pop quiz – defining a matrix with a string
      5. Universal functions
      6. Time for action – creating universal functions
        1. What just happened?
      7. Universal function methods
      8. Time for action – applying the ufunc methods to the add function
        1. What just happened?
      9. Arithmetic functions
      10. Time for action – dividing arrays
        1. What just happened?
        2. Have a go hero – experimenting with __future__.division
      11. Modulo operation
      12. Time for action – computing the modulo
        1. What just happened?
      13. Fibonacci numbers
      14. Time for action – computing Fibonacci numbers
        1. What just happened?
        2. Have a go hero – timing the calculations
      15. Lissajous curves
      16. Time for action – drawing Lissajous curves
        1. What just happened?
      17. Square waves
      18. Time for action – drawing a square wave
        1. What just happened?
        2. Have a go hero – getting rid of the loop
      19. Sawtooth and triangle waves
      20. Time for action – drawing sawtooth and triangle waves
        1. What just happened?
        2. Have a go hero – getting rid of the loop
      21. Bitwise and comparison functions
      22. Time for action – twiddling bits
        1. What just happened?
      23. Fancy indexing
      24. Time for action – fancy indexing in-place for ufuncs with the at() method
        1. What just happened?
      25. Summary
    13. 6. Moving Further with NumPy Modules
      1. Linear algebra
      2. Time for action – inverting matrices
        1. What just happened?
        2. Pop quiz – creating a matrix
        3. Have a go hero – inverting your own matrix
      3. Solving linear systems
      4. Time for action – solving a linear system
        1. What just happened?
      5. Finding eigenvalues and eigenvectors
      6. Time for action – determining eigenvalues and eigenvectors
        1. What just happened?
      7. Singular value decomposition
      8. Time for action – decomposing a matrix
        1. What just happened?
      9. Pseudo inverse
      10. Time for action – computing the pseudo inverse of a matrix
        1. What just happened?
      11. Determinants
      12. Time for action – calculating the determinant of a matrix
        1. What just happened?
      13. Fast Fourier transform
      14. Time for action – calculating the Fourier transform
        1. What just happened?
      15. Shifting
      16. Time for action – shifting frequencies
        1. What just happened?
      17. Random numbers
      18. Time for action – gambling with the binomial
        1. What just happened?
      19. Hypergeometric distribution
      20. Time for action – simulating a game show
        1. What just happened?
      21. Continuous distributions
      22. Time for action – drawing a normal distribution
        1. What just happened?
      23. Lognormal distribution
      24. Time for action – drawing the lognormal distribution
        1. What just happened?
      25. Bootstrapping in statistics
      26. Time for action – sampling with numpy.random.choice()
        1. What just happened?
      27. Summary
    14. 7. Peeking into Special Routines
      1. Sorting
      2. Time for action – sorting lexically
        1. What just happened?
        2. Have a go hero – trying a different sort order
      3. Time for action – partial sorting via selection for a fast median with the partition() function
        1. What just happened?
      4. Complex numbers
      5. Time for action – sorting complex numbers
        1. What just happened?
        2. Pop quiz – generating random numbers
      6. Searching
      7. Time for action – using searchsorted
        1. What just happened?
      8. Array elements extraction
      9. Time for action – extracting elements from an array
        1. What just happened?
      10. Financial functions
      11. Time for action – determining the future value
        1. What just happened?
      12. Present value
      13. Time for action – getting the present value
        1. What just happened?
      14. Net present value
      15. Time for action – calculating the net present value
        1. What just happened?
      16. Internal rate of return
      17. Time for action – determining the internal rate of return
        1. What just happened?
      18. Periodic payments
      19. Time for action – calculating the periodic payments
        1. What just happened?
      20. Number of payments
      21. Time for action – determining the number of periodic payments
        1. What just happened?
      22. Interest rate
      23. Time for action – figuring out the rate
        1. What just happened?
      24. Window functions
      25. Time for action – plotting the Bartlett window
        1. What just happened?
      26. Blackman window
      27. Time for action – smoothing stock prices with the Blackman window
        1. What just happened?
      28. Hamming window
      29. Time for action – plotting the Hamming window
        1. What just happened?
      30. Kaiser window
      31. Time for action – plotting the Kaiser window
        1. What just happened?
      32. Special mathematical functions
      33. Time for action – plotting the modified Bessel function
        1. What just happened?
      34. sinc
      35. Time for action – plotting the sinc function
        1. What just happened?
      36. Summary
    15. 8. Assuring Quality with Testing
      1. Assert functions
      2. Time for action – asserting almost equal
        1. What just happened?
        2. Pop quiz – specifying decimal precision
      3. Approximately equal arrays
      4. Time for action – asserting approximately equal
        1. What just happened?
      5. Almost equal arrays
      6. Time for action – asserting arrays almost equal
        1. What just happened?
        2. Have a go hero – comparing arrays with different shapes
      7. Equal arrays
      8. Time for action – comparing arrays
        1. What just happened?
      9. Ordering arrays
      10. Time for action – checking the array order
        1. What just happened?
      11. Object comparison
      12. Time for action – comparing objects
        1. What just happened?
      13. String comparison
      14. Time for action – comparing strings
        1. What just happened?
      15. Floating-point comparisons
      16. Time for action – comparing with assert_array_almost_equal_nulp
        1. What just happened?
      17. Comparison of floats with more ULPs
      18. Time for action – comparing using maxulp of 2
        1. What just happened?
      19. Unit tests
      20. Time for action – writing a unit test
        1. What just happened?
      21. Nose test decorators
      22. Time for action – decorating tests
        1. What just happened?
      23. Docstrings
      24. Time for action – executing doctests
        1. What just happened?
      25. Summary
    16. 9. Plotting with matplotlib
      1. Simple plots
      2. Time for action – plotting a polynomial function
        1. What just happened?
        2. Pop quiz – the plot() function
      3. Plot format string
      4. Time for action – plotting a polynomial and its derivatives
        1. What just happened?
      5. Subplots
      6. Time for action – plotting a polynomial and its derivatives
        1. What just happened?
      7. Finance
      8. Time for action – plotting a year's worth of stock quotes
        1. What just happened?
      9. Histograms
      10. Time for action – charting stock price distributions
        1. What just happened?
        2. Have a go hero – drawing a bell curve
      11. Logarithmic plots
      12. Time for action – plotting stock volume
        1. What just happened?
      13. Scatter plots
      14. Time for action – plotting price and volume returns with a scatter plot
        1. What just happened?
      15. Fill between
      16. Time for action – shading plot regions based on a condition
        1. What just happened?
      17. Legend and annotations
      18. Time for action – using a legend and annotations
        1. What just happened?
      19. Three-dimensional plots
      20. Time for action – plotting in three dimensions
        1. What just happened?
      21. Contour plots
      22. Time for action – drawing a filled contour plot
        1. What just happened?
      23. Animation
      24. Time for action – animating plots
        1. What just happened?
      25. Summary
    17. 10. When NumPy Is Not Enough – SciPy and Beyond
      1. MATLAB and Octave
      2. Time for action – saving and loading a .mat file
        1. What just happened?
        2. Pop quiz – loading .mat files
      3. Statistics
      4. Time for action – analyzing random values
        1. What just happened?
        2. Have a go hero – improving the data generation
      5. Sample comparison and SciKits
      6. Time for action – comparing stock log returns
        1. What just happened?
      7. Signal processing
      8. Time for action – detecting a trend in QQQ
        1. What just happened?
      9. Fourier analysis
      10. Time for action – filtering a detrended signal
        1. What just happened?
      11. Mathematical optimization
      12. Time for action – fitting to a sine
        1. What just happened?
      13. Numerical integration
      14. Time for action – calculating the Gaussian integral
        1. What just happened?
        2. Have a go hero – experiment a bit more
      15. Interpolation
      16. Time for action – interpolating in one dimension
        1. What just happened?
      17. Image processing
      18. Time for action – manipulating Lena
        1. What just happened?
      19. Audio processing
      20. Time for action – replaying audio clips
        1. What just happened?
      21. Summary
    18. 11. Playing with Pygame
      1. Pygame
      2. Time for action – installing Pygame
      3. Hello World
      4. Time for action – creating a simple game
        1. What just happened?
      5. Animation
      6. Time for action – animating objects with NumPy and Pygame
        1. What just happened?
      7. matplotlib
      8. Time for Action – using matplotlib in Pygame
        1. What just happened?
      9. Surface pixels
      10. Time for Action – accessing surface pixel data with NumPy
        1. What just happened?
      11. Artificial Intelligence
      12. Time for Action – clustering points
        1. What just happened?
      13. OpenGL and Pygame
      14. Time for Action – drawing the Sierpinski gasket
        1. What just happened?
      15. Simulation game with Pygame
      16. Time for Action – simulating life
        1. What just happened?
      17. Summary
    19. A. Pop Quiz Answers
      1. Chapter 1, NumPy Quick Start
        1. Pop quiz – functioning of the arange() function
      2. Chapter 2, Beginning with NumPy Fundamentals
        1. Pop quiz – the shape of ndarray
      3. Chapter 3, Getting Familiar with Commonly Used Functions
        1. Pop quiz – computing the weighted average
      4. Chapter 4, Convenience Functions for Your Convenience
        1. Pop quiz – calculating covariance
      5. Chapter 5, Working with Matrices and ufuncs
        1. Pop quiz – defining a matrix with a string
      6. Chapter 6, Move Further with NumPy Modules
        1. Pop quiz – creating a matrix
      7. Chapter 7, Peeking into Special Routines
        1. Pop quiz – generating random numbers
      8. Chapter 8, Assuring Quality with Testing
        1. Pop quiz – specifying decimal precision
      9. Chapter 9, Plotting with matplotlib
        1. Pop quiz – the plot() function
      10. Chapter 10, When NumPy Is Not Enough –Scipy and Beyond
        1. Pop quiz – loading .mat files
    20. B. Additional Online Resources
      1. Python
      2. Mathematics and statistics
    21. C. NumPy Functions' References
    22. Index