You are previewing NumPy Beginner's Guide Second Edition.
O'Reilly logo
NumPy Beginner's Guide Second Edition

Book Description

An action packed guide using real world examples of the easy to use, high performance, free open source NumPy mathematical library.

  • Perform high performance calculations with clean and efficient NumPy code.

  • Analyze large data sets with statistical functions

  • Execute complex linear algebra and mathematical computations

In Detail

NumPy is an extension to, and the fundamental package for scientific computing with Python. In today's world of science and technology, it is all about speed and flexibility. When it comes to scientific computing, NumPy is on the top of the list.

NumPy Beginner's Guide will teach you about NumPy, a leading scientific computing library. NumPy replaces a lot of the functionality of Matlab and Mathematica, but in contrast to those products, is free and open source.

Write readable, efficient, and fast code, which is as close to the language of mathematics as is currently possible with the cutting edge open source NumPy software library. Learn all the ins and outs of NumPy that requires you to know basic Python only. Save thousands of dollars on expensive software, while keeping all the flexibility and power of your favourite programming language.You will learn about installing and using NumPy and related concepts. At the end of the book we will explore some related scientific computing projects. This book will give you a solid foundation in NumPy arrays and universal functions. Through examples, you will also learn about plotting with Matplotlib and the related SciPy project. NumPy Beginner's Guide will help you be productive with NumPy and have you writing clean and fast code in no time at all.

Table of Contents

  1. NumPy Beginner's Guide Second Edition
    1. Table of Contents
    2. Numpy Beginner's Guide Second 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. Conventions
      9. Reader feedback
      10. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. NumPy Quick Start
      1. Python
      2. Time for action – installing Python on different operating systems
        1. What just happened?
      3. Windows
      4. Time for action – installing NumPy, Matplotlib, SciPy, and IPython on Windows
        1. What just happened?
      5. Linux
      6. Time for action – installing NumPy, Matplotlib, SciPy, and IPython on Linux
        1. What just happened?
      7. Mac OS X
      8. Time for action – installing NumPy, Matplotlib, and SciPy on Mac OS X
        1. What just happened?
      9. Time for action – installing NumPy, SciPy, Matplotlib, and IPython with MacPorts or Fink
        1. What just happened?
      10. Building from source
      11. Arrays
      12. 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
      13. IPython—an interactive shell
      14. Online resources and help
      15. 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 matrix
        4. Selecting elements
        5. NumPy numerical types
        6. Data type objects
        7. Character codes
        8. dtype constructors
        9. 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. Get in Terms with Commonly Used Functions
      1. File I/O
      2. Time for action – reading and writing files
        1. What just happened?
      3. CSV files
      4. Time for action – loading from CSV files
        1. What just happened?
      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 – doing 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. Weekly summary
      16. Time for action – summarizing data
        1. What just happened?
        2. Have a go hero – improving the code
      17. Average true range
      18. Time for action – calculating the average true range
        1. What just happened?
        2. Have a go hero – taking the minimum function for a spin
      19. Simple moving average
      20. Time for action – computing the simple moving average
        1. What just happened?
      21. Exponential moving average
      22. Time for action – calculating the exponential moving average
        1. What just happened?
      23. Bollinger bands
      24. Time for action – enveloping with Bollinger bands
        1. What just happened?
        2. Have a go hero – switching to exponential moving average
      25. Linear model
      26. Time for action – predicting price with a linear model
        1. What just happened?
      27. Trend lines
      28. Time for action – drawing trend lines
        1. What just happened?
      29. Methods of ndarray
      30. Time for action – clipping and compressing arrays
        1. What just happened?
      31. Factorial
      32. Time for action – calculating the factorial
        1. What just happened?
      33. 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. 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 function
        1. What just happened?
      7. Universal function methods
      8. Time for action – applying the ufunc methods on add
        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
        3. Modulo operation
      11. Time for action – computing the modulo
        1. What just happened?
      12. Fibonacci numbers
      13. Time for action – computing Fibonacci numbers
        1. What just happened?
        2. Have a go hero – timing the calculations
      14. Lissajous curves
      15. Time for action – drawing Lissajous curves
        1. What just happened?
      16. Square waves
      17. Time for action – drawing a square wave
        1. What just happened?
        2. Have a go hero – getting rid of the loop
      18. Sawtooth and triangle waves
      19. Time for action – drawing sawtooth and triangle waves
        1. What just happened?
        2. Have a go hero – getting rid of the loop
      20. Bitwise and comparison functions
      21. Time for action – twiddling bits
        1. What just happened?
      22. Summary
    13. 6. Move 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. Pseudoinverse
      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. 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. Complex numbers
      4. Time for action – sorting complex numbers
        1. What just happened?
        2. Pop quiz – generating random numbers
      5. Searching
      6. Time for action – using searchsorted
        1. What just happened?
      7. Array elements' extraction
      8. Time for action – extracting elements from an array
        1. What just happened?
      9. Financial functions
      10. Time for action – determining future value
        1. What just happened?
      11. Present value
      12. Time for action – getting the present value
        1. What just happened?
      13. Net present value
      14. Time for action – calculating the net present value
        1. What just happened?
      15. Internal rate of return
      16. Time for action – determining the internal rate of return
        1. What just happened?
      17. Periodic payments
      18. Time for action – calculating the periodic payments
        1. What just happened?
      19. Number of payments
      20. Time for action – determining the number of periodic payments
        1. What just happened?
      21. Interest rate
      22. Time for action – figuring out the rate
        1. What just happened?
      23. Window functions
      24. Time for action – plotting the Bartlett window
        1. What just happened?
      25. Blackman window
      26. Time for action – smoothing stock prices with the Blackman window
        1. What just happened?
      27. Hamming window
      28. Time for action – plotting the Hamming window
        1. What just happened?
      29. Kaiser window
      30. Time for action – plotting the Kaiser window
        1. What just happened?
      31. Special mathematical functions
      32. Time for action – plotting the modified Bessel function
        1. What just happened?
      33. sinc
      34. Time for action – plotting the sinc function
        1. What just happened?
      35. Summary
    15. 8. Assure 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 array 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. Objects 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 tests 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 derivative
        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 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 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. Samples’ 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?
      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
      2. Chapter 2, Beginning with NumPy Fundamentals
      3. Chapter 3, Get into Terms with Commonly Used Functions
      4. Chapter 4, Convenience functions for your convenience
      5. Chapter 5, Working with Matrices and ufuncs
      6. Chapter 6, Move further with NumPy modules
      7. Chapter 7, Peeking into special routines
      8. Chapter 8, Assure Quality with Testing
      9. Chapter 9, Plotting with Matplotlib
      10. Chapter 10, When NumPy is not enough Scipy and beyond
    20. Index