Book Image

Applying Math with Python

By : Sam Morley
Book Image

Applying Math with Python

By: Sam Morley

Overview of this book

Python, one of the world's most popular programming languages, has a number of powerful packages to help you tackle complex mathematical problems in a simple and efficient way. These core capabilities help programmers pave the way for building exciting applications in various domains, such as machine learning and data science, using knowledge in the computational mathematics domain. The book teaches you how to solve problems faced in a wide variety of mathematical fields, including calculus, probability, statistics and data science, graph theory, optimization, and geometry. You'll start by developing core skills and learning about packages covered in Python’s scientific stack, including NumPy, SciPy, and Matplotlib. As you advance, you'll get to grips with more advanced topics of calculus, probability, and networks (graph theory). After you gain a solid understanding of these topics, you'll discover Python's applications in data science and statistics, forecasting, geometry, and optimization. The final chapters will take you through a collection of miscellaneous problems, including working with specific data formats and accelerating code. By the end of this book, you'll have an arsenal of practical coding solutions that can be used and modified to solve a wide range of practical problems in computational mathematics and data science.
Table of Contents (12 chapters)

How to do it...

Follow these steps to use Dask to perform some computations on a DataFrame object:

  1. First, we need to load the data from sample.csv into a Dask DataFrame:
data = dd.read_csv("sample.csv")
  1. Next, we perform a standard calculation on the columns of the DataFrame:
sum_data = data.lower + data.upper
print(sum_data)

Unlike with Pandas DataFrames, the result is not a new DataFrame. The print statement gives us the following information:

Dask Series Structure:
npartitions=1
float64
...
dtype: float64
Dask Name: add, 6 tasks
  1. To actually get the result, we need to use the compute method:
result = sum_data.compute()
print(result.head())

The result is now shown as expected:

0 -0.911811
1 0.947240
2 -0.552153
3 -0.429914
4 1.229118
dtype: float64
  1. We compute the means of the final two columns in exactly the same...