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

SciPy Recipes

Book Description

Tackle the most sophisticated problems associated with scientific computing and data manipulation using SciPy

About This Book

  • Covers a wide range of data science tasks using SciPy, NumPy, pandas, and matplotlib
  • Effective recipes on advanced scientific computations, statistics, data wrangling, data visualization, and more
  • A must-have book if you're looking to solve your data-related problems using SciPy, on-the-go

Who This Book Is For

Python developers, aspiring data scientists, and analysts who want to get started with scientific computing using Python will find this book an indispensable resource. If you want to learn how to manipulate and visualize your data using the SciPy Stack, this book will also help you. A basic understanding of Python programming is all you need to get started.

What You Will Learn

  • Get a solid foundation in scientific computing using Python
  • Master common tasks related to SciPy and associated libraries such as NumPy, pandas, and matplotlib
  • Perform mathematical operations such as linear algebra and work with the statistical and probability functions in SciPy
  • Master advanced computing such as Discrete Fourier Transform and K-means with the SciPy Stack
  • Implement data wrangling tasks efficiently using pandas
  • Visualize your data through various graphs and charts using matplotlib

In Detail

With the SciPy Stack, you get the power to effectively process, manipulate, and visualize your data using the popular Python language. Utilizing SciPy correctly can sometimes be a very tricky proposition. This book provides the right techniques so you can use SciPy to perform different data science tasks with ease.

This book includes hands-on recipes for using the different components of the SciPy Stack such as NumPy, SciPy, matplotlib, and pandas, among others. You will use these libraries to solve real-world problems in linear algebra, numerical analysis, data visualization, and much more. The recipes included in the book will ensure you get a practical understanding not only of how a particular feature in SciPy Stack works, but also of its application to real-world problems. The independent nature of the recipes also ensure that you can pick up any one and learn about a particular feature of SciPy without reading through the other recipes, thus making the book a very handy and useful guide.

Style and approach

This book consists of hands-on recipes where you'll deal with real-world problems.

You'll execute a series of tasks as you walk through scientific computing challenges using SciPy.

Your one-stop solution for common and not-so-common pain points, this is a book that you must have on the shelf.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Getting to Know the Tools
    1. Introduction
    2. Installing Anaconda on Windows
      1. How to do it...
    3. Installing Anaconda on macOS
      1. How to do it...
    4. Installing Anaconda on Linux
      1. How to do it...
    5. Checking the Anaconda installation
      1. How to do it...
    6. Installing SciPy from a binary distribution on Windows
      1. How to do it...
        1. Installing Python
        2. Installing the SciPy stack
    7. Installing SciPy from a binary distribution on macOS
      1. How to do it...
        1. Installing the Xcode command-line tools
        2. Installing Homebrew
        3. Installing Python 3
        4. Installing the SciPy stack
    8. Installing SciPy from source on Linux
      1. How to do it...
        1. Installing Python 3
        2. Installing the SciPy stack
    9. Installing optional packages with conda
      1. Getting ready
      2. How to do it...
    10. Installing packages with pip
      1. How to do it...
    11. Setting up a virtual environment with conda
      1. Getting ready
      2. How to do it...
    12. Creating a virtual environment for development with conda 
      1. Getting ready
      2. How to do it...
    13. Creating a conda environment with a different version of a package
      1. Getting ready
      2. How to do it...
    14. Using conda environments to run different versions of Python
      1. Getting ready
      2. How to do it...
    15. Creating virtual environments with venv
      1. How to do it...
    16. Running SciPy in a script
      1. Getting ready
      2. How to do it...
    17. Running SciPy in Jupyter
      1. Getting ready
      2. How to do it...
    18. Running SciPy in Spyder
      1. Getting ready
      2. How to do it...
    19. Running SciPy in PyCharm
      1. Getting started
      2. How to do it...
  3. Getting Started with NumPy
    1. Introduction
    2. Creating NumPy arrays
      1. How to do it…
        1. Creating an array from a list
        2. Specifying the data type for elements in an array
        3. Creating an empty array with a given shape
        4. Creating arrays of zeros and ones with a single value
        5. Creating arrays with equally spaced values
        6. Creating an array by repeating elements
        7. Creating an array by tiling another array
        8. Creating an array with the same shape as another array
        9. Using object arrays to store heterogeneous data
      2. See also
    3. Querying and changing the shape of an array
      1. How to do it...
    4. Storing and retrieving NumPy arrays
      1. How to do it...
        1. Storing a NumPy array in text format
        2. Storing a NumPy array in CSV format
        3. Loading an array from a text file
        4. Storing a single array in binary format
        5. Storing several arrays in binary format
        6. Loading arrays stored in NPY binary format
    5. Indexing
      1. How to do it...
        1. Accessing sub arrays using slices
        2. Selecting subarrays using an index list
        3. Indexing with Boolean arrays
    6. Operations on arrays
      1. How to do it...
        1. Computing a function for all elements of an array
        2. Doing array operations
        3. Computing matrix products
    7. Using masked arrays to represent invalid data
      1. How to do it...
        1. Creating a masked array from an explicit mask
        2. Creating a masked array from a condition
    8. Using object arrays to store heterogeneous data
      1. How to do it...
    9. Defining, symbolically, a function operating on arrays
      1. Getting ready
      2. How to do it...
      3. How it works...
  4. Using Matplotlib to Create Graphs
    1. Introduction
    2. Creating two-dimensional plots of functions and data
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Generating multiple plots in a single figure
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Setting line styles and markers
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Using different backends to display graphs
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Saving plots to disk
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Annotating graphs
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Generating histograms and box plots
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Creating three-dimensional plots
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Generating interactive displays in the Jupyter Notebook
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Object-oriented graph creation using Artist objects
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Creating a map with cartopy
      1. Getting ready
      2. How to do it…
      3. How it works…
  5. Data Wrangling with pandas
    1. Creating Series objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    2. Creating DataFrame objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Inserting and deleting columns to a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Inserting and deleting rows to a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Selecting items by row indexes and column labels
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Selecting items by integer location
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Selecting items using mixed indexing
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Accessing, selecting, and modifying data
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Selecting rows using Boolean selection
      1. How to do it...
    10. Reading and storing data in different formats
      1. Getting ready
      2. How to do it...
        1. Working with CSV, text/tabular, and format data
      3. How it works...
        1. Reading a CSV file into a DataFrame
        2. Specifying the index column when reading a CSV file
        3. Reading and writing data in Excel format
        4. Reading and writing JSON files
        5. Reading HTML data from the web
        6. Accessing CSV data on the web
        7. Reading and writing from/to SQL databases
    11. Data displays employing different kinds of visual representation
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. How to apply numerical functions and operations to Series and DataFrame objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Computing statistical functions on Series and DataFrame objects
      1. Getting ready
      2. How to do it...
        1. Retrieving summary descriptive statistics
      3. How it works...
        1. Calculating the mean
        2. Calculating variance and standard deviation
    14. How to sort data in Series and DataFrame objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    15. Performing merging, joins, concatenation, and grouping
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Merging data from multiple pandas objects
  6. Matrices and Linear Algebra
    1. Introduction
    2. Matrix operations and functions on two-dimensional arrays
      1. How to do it…
    3. Solving linear systems using matrices
      1. How it works…
      2. How to do it…
    4. Calculating the null space of a matrix 
      1. How to do it…
    5. Calculating the LU decompositions of a matrix 
      1. How to do it…
    6. Calculating the QR decomposition of a matrix
      1. How to do it…
    7. Calculating the eigenvalue and eigenvector of a matrix
      1. How to do it…
    8. Diagonalizing a matrix
      1. How to do it…
    9. Calculating the Jordan form of a matrix
      1. How to do it…
    10. Calculating the singular value decomposition of a matrix
      1. How to do it…
    11. Creating a sparse matrix
      1. How to do it…
    12. Computations on top of a sparse matrix
      1. How to do it…
  7. Solving Equations and Optimization
    1. Introduction
    2. Non-linear equations and systems
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. System of equations and how to solve it
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Choosing the solver used to find the solution of equations
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Solving constrained non-linear optimization problems in several variables
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Solving one-dimensional optimization problems
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Solving multidimensional non-linear equations using the Newton-Krylov method
      1. Getting ready
      2. How to do it...
    8. Solving multidimensional non-linear equations using the Anderson method
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Finding the best linear fit for a set of data
      1. Getting ready
      2. How to do it...
      3. How it works ...
    10. Doing non-linear regression for a set of data
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Regression
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. Constants and Special Functions
    1. Introduction
    2. Physical and mathematical constants available in SciPy
      1. Getting ready...
      2. How to do it...
    3. Using constants in the CODATA database
      1. Getting ready
      2. How to do it...
    4. Bessel functions
      1. Getting ready...
      2. How to do it...
    5. Error functions
      1. Getting ready...
      2. How to do it...
    6. Orthogonal polynomials functions
      1. Getting ready...
      2. How to do it...
    7. Gamma function
      1. Getting ready...
      2. How to do it...
      3. How it works...
    8. The Riemann zeta function
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Airy and Bairy functions
      1. Getting ready...
      2. How to do it...
    10. The Bessel and Struve functions
      1. Getting ready...
      2. How to do it...
      3. How it works...
      4. There's more
  9. Calculus, Interpolation, and Differential Equations
    1. Introduction
    2. Integration
      1. Getting ready
      2. How to do it…
      3. How it works...
        1. Computing integrals using the Newton-Cotes method
    3. Computing integrals using a Gaussian quadrature
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Computing integrals with weighting functions
      1. Getting ready
      2. How to do it...
    5. Computing multiple integrals
      1. Getting ready
      2. How to do it...
    6. Interpolation
      1. Getting ready
      2. How to do it...
    7. Computing a polynomial interpolation for a set of data points
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Univariate interpolation
      1. Getting ready
      2. How to do it…
    9. Finding a cubic spline that interpolates a set of data
      1. Getting ready
      2. How to do it…
      3. How it works...
    10. Defining a B-spline for a given set of control points
      1. Getting ready
      2. How to do it…
      3. How it works ...
    11. Differentiation
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Solving a one-dimensional ordinary differential equation
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Solving a system of ordinary differential equations
      1. Getting ready
      2. How to do it...
      3. How it works...
    14. Solving differential equations and systems with parameters
      1. Getting ready
      2. How to do it...
      3. How it works...
    15. Using ode and the objected-oriented interface to solve differential equations
      1. Getting ready
      2. How to do it...
      3. How it works ...
  10. Statistics and Probability
    1. Introduction
    2. Computing the probability mass function of a discrete random variable
      1. Binomial discrete distribution
      2. Multinomial discrete distribution
      3. How to do it...
      4. Visualizing the probability mass function
    3. Computing the probability density function of a continuous random variable
      1. How to do it...
    4. Computing the cumulative distribution function for a random variable
      1. How to do it...
    5. Computing the values of inverse probabilities associated with a random variable
      1. How to do it...
    6. Computing the average, standard deviation, and higher moments of a distribution
      1. How to do it...
      2. Average and standard deviation of a distribution
      3. Calculating the moments of a distribution
    7. Computing probabilities associated with the multivariate Gaussian distribution
      1. How to do it...
        1. Getting started with simulation
    8. Computing the summary statistics of a dataset
      1. How to do it...
  11. Advanced Computations with SciPy
    1. Discrete Fourier transforms
      1. How to do it…
      2. How it works…
    2. Computing the discrete Fourier transform (DFT) of a data series using the FFT algorithm
      1. How to do it…
      2. How it works…
    3. Computing the inverse DFT of a data series
      1. How to do it…
      2. How it works…
    4. Computing signal construction
      1. How to do it…
    5. Getting started with filters
      1. How to do it…
      2. How it works…
    6. Computing the DFT for two-dimensional data
      1. How to do it…
      2. How it works…
    7. How to find the DFT of the derivative of a function
      1. How to do it…
    8. Computing the convolution of two functions
      1. How to do it…
    9. Mathematical imaging
      1. How to do it…
    10. Computing pairwise distances from a dataset, using different distance metrics
      1. How to do it…
    11. How to identify neighborhoods and nearest neighbors for a dataset and a given metric
      1. How to do it…
      2. How it works…
    12. Nearest neighbors regression
      1. How it works…