In this article, I will walk you through a numerical computational task with Python to count rainy days with python. Imagine you have a data series that represents the amount of precipitation each day for a year in a given city.
For this task of numerical computing with python to count rainy days within a year, I will start this off by loading the data, that you can download from here:
import numpy as np import pandas as pd data = pd.read_csv("Seattle2014.csv") print(data.head()) rainfall = data["PRCP"].values inches = rainfall/254 print(inches.shape)
The table contains 365 values, giving the daily precipitation in inches from January 1 to December 31, 2014.
Before we go ahead with this task, let’s take a quick look at the rainy day histogram using matplotlib:
import matplotlib.pyplot as plt import seaborn seaborn.set() plt.hist(inches, 40) plt.show()
The histogram above gives us a general idea of what the data looks like: despite its reputation, the vast majority of days in Seattle saw near the zero measured rainfall in 2014.
Count Rainy Days with Numerical Computing
The focus of this article is about numerical computing with python to count rainy days within a year, so I will focus more on solving this problem by numerical computing rather than machine learning algorithms.
If you don’t know to perform the task of numerical computing in python we can easily perform our tasks using the NumPy package in Python. So let’s perform some necessary NumPy functions for numerical computing with Python.
Using the numerical calculation functions using NumPy, we could begin to answer the types of questions we receive about our precipitation data. Here are some examples of results we can calculate for counting rainy days using Python:
print("Number of days without rain: ", np.sum(inches == 0)) print("Number of days with rain: ", np.sum(inches != 0)) print("Number of days with rain more than 0.5 inches: ", np.sum(inches>0.5)) print("Number of days with rain < 0.2 inches: ", np.sum((inches > 0)& (inches < 0.2)))
Number days without rain: 215
Number days with rain: 150
Days with more than 0.5 inches: 37
Rainy days with < 0.1 inches: 75
I hope you liked this article on how we can use the power of numerical computing in python to count the number of rainy days in Python. I hope you will play more with the data. Feel free to ask your valuable questions in the comments section below.