Skip to content

As the climate changes, predicting the weather becomes ever more important for businesses. You have been asked to support on a machine learning project with the aim of building a pipeline to predict the climate in London, England. Specifically, the model should predict mean temperature in degrees Celsius (°C).

Since the weather depends on a lot of different factors, you will want to run a lot of experiments to determine what the best approach is to predict the weather. In this project, you will run experiments for different regression models predicting the mean temperature, using a combination of sklearn and mlflow.

You will be working with data stored in london_weather.csv, which contains the following columns:

  • date - recorded date of measurement - (int)
  • cloud_cover - cloud cover measurement in oktas - (float)
  • sunshine - sunshine measurement in hours (hrs) - (float)
  • global_radiation - irradiance measurement in Watt per square meter (W/m2) - (float)
  • max_temp - maximum temperature recorded in degrees Celsius (°C) - (float)
  • mean_temp - target mean temperature in degrees Celsius (°C) - (float)
  • min_temp - minimum temperature recorded in degrees Celsius (°C) - (float)
  • precipitation - precipitation measurement in millimeters (mm) - (float)
  • pressure - pressure measurement in Pascals (Pa) - (float)
  • snow_depth - snow depth measurement in centimeters (cm) - (float)
# Run this cell to install mlflow
!pip install mlflow
Hidden output
# Run this cell to import the modules you require
import pandas as pd
import numpy as np
import mlflow
import mlflow.sklearn
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline, make_pipeline
# Loading the data
df = pd.read_csv('london_weather.csv')
print(df.head())
# DataFrame information
print(df.info())
# Convert date to datetime data type
df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
# Create 'year' and 'month' columns
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month

print(df.head())
# Visualizations
df_per_month = df.groupby(['year','month'], as_index=False).mean()
print(df_per_month.head())
# Visualizing mean temperature per year
sns.lineplot(x='year', y='mean_temp', data=df_per_month, ci=None);
# Visualizing mean sunshine per month
sns.barplot(x='month', y='sunshine', data=df_per_month);
# Take a look at correlation
sns.heatmap(df.corr(), annot=True);
# Checking for missing values
df.isna().sum()
# Feature selection
df_features = ['cloud_cover', 'sunshine', 'global_radiation', 'snow_depth', 'month']
df_target = 'mean_temp'
# Drop missing values in target variable
df = df.dropna(subset=['mean_temp'])
# Check missing values once more to confirm missing values were dropped for target
df.isna().sum()
# Create X and y sets
X = df[df_features]
y = df[df_target]
# Split into Train and Test sets before any preprocessing steps
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.33,random_state=1)
# 'Project v1'
# Preprocessing Step - Impute missing values using the mean - using make_pipeline
# Scaling features for them all to be on same scale for enhanced model performance
imputer = SimpleImputer()
scaler = StandardScaler()
# Create pipeline
pipeline = make_pipeline(imputer, scaler)

X_train_scaled = pipeline.fit_transform(X_train)
X_test_scaled = pipeline.transform(X_test)