Practical example – how to predict the weather

Let's see how we can use the concepts developed in this chapter to predict the weather. Let's assume that we want to predict whether it will rain tomorrow based on the data collected over a year for a particular city. 

The data available to train this model is in the CSV file called weather.csv:

  1. Let's import the data as a pandas data frame:
import numpy as np 
import pandas as pd
df = pd.read_csv("weather.csv")
  1. Let's look at the columns of the data frame:

  1. Next, let's look at the header of the first 13 columns of the weather.csv data:

  1. Now, let's look at the last 10 columns of the weather.csv data:

  1. Let's use x to represent the input features. We will drop the Date field for the feature list as it is not useful in the context of predictions. We will also drop the RainTomorrow label:
x = df.drop(['Date','RainTomorrow'],axis=1)
  1. Let's use y to represent the label:
y = df['RainTomorrow']
  1. Now, let's divide the data into train_test_split:
from sklearn.model_selection import train_test_split
train_x , train_y ,test_x , test_y = train_test_split(x,y , test_size = 0.2,random_state = 2)
  1. As the label is a binary variable, we are training a classifier. So, logistic regression will be a good choice here. First, let's instantiate the logistic regression model:
model = LogisticRegression()
  1. Now, we can use train_x and test_x to train the model:
model.fit(train_x , test_x)
  1. Once the model is trained, let's use it for predictions:
predict = model.predict(train_y)
  1. Now, let's find the accuracy of our trained model:

Now, this binary classifier can be used to predict whether it will rain tomorrow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.113.188