0% found this document useful (0 votes)
19 views5 pages

Weather Forecasting

The document shows steps to download and analyze a climate dataset using Google Colab. It mounts Google Drive, installs necessary libraries, downloads and extracts the climate dataset from Kaggle, then performs EDA and time series forecasting using Prophet. Key steps include: 1. Mounting Google Drive, installing Kaggle API and moving the Kaggle key file. 2. Downloading and extracting the climate dataset from Kaggle. 3. Performing EDA on the train and test datasets including plotting and preprocessing dates. 4. Forecasting future mean temperatures using Prophet and visualizing the results.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Weather Forecasting

The document shows steps to download and analyze a climate dataset using Google Colab. It mounts Google Drive, installs necessary libraries, downloads and extracts the climate dataset from Kaggle, then performs EDA and time series forecasting using Prophet. Key steps include: 1. Mounting Google Drive, installing Kaggle API and moving the Kaggle key file. 2. Downloading and extracting the climate dataset from Kaggle. 3. Performing EDA on the train and test datasets including plotting and preprocessing dates. 4. Forecasting future mean temperatures using Prophet and visualizing the results.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

from google.

colab import drive

# Mount Google Drive


drive.mount('/content/drive')

!pip install kaggle

import os
import json

# Upload your Kaggle API key file (kaggle.json) to Colab using the file upload
feature
from google.colab import files
files.upload()

# Read the contents of the kaggle.json file


with open('kaggle.json', 'r') as file:
kaggle_json = json.load(file)

# Move the saved kaggle.json file to the required directory


os.makedirs('/root/.kaggle', exist_ok=True)
os.rename('kaggle.json', '/root/.kaggle/kaggle.json')

# Set the appropriate permissions for the Kaggle API key file
os.chmod('/root/.kaggle/kaggle.json', 0o600)

import os

# Specify the path to the kaggle.json file


kaggle_json_path = os.path.join(os.path.expanduser("~"), ".kaggle", "kaggle.json")

# Check if the kaggle.json file already exists


if os.path.exists(kaggle_json_path):
print("kaggle.json file already exists.")
else:
# Move the uploaded Kaggle API key file to the required directory
!mkdir -p ~/.kaggle # This command creates a directory named '.kaggle'
inside the user's home directory (~). The -p option ensures that the parent
directories are also created if they don't exist. If the directory already exists,
this command will not throw an error
!mv kaggle.json ~/.kaggle/ # This command moves the file named 'kaggle.json'
to the ~/.kaggle/ directory. The mv command is used for file or directory
relocation. The first argument, kaggle.json, represents the current name/path of
the file, and the second argument, ~/.kaggle/, represents the destination directory
where the file should be moved.
!chmod 600 ~/.kaggle/kaggle.json
print("kaggle.json file moved and permissions set successfully.")

# Verify the Kaggle API is working


!kaggle datasets list

!kaggle datasets download --force sumanthvrao/daily-climate-time-series-data

import zipfile

# Specify the path to the ZIP file


zip_file_path = '/content/daily-climate-time-series-data.zip'
# creating directory to unzip dataset
!mkdir -p /content/daily-climate-time-series-data

# Specify the target directory to extract the files


target_directory = '/content/daily-climate-time-series-data'

# Open the ZIP file


with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
# Extract all the files to the target directory
zip_ref.extractall(target_directory)

print("ZIP file extracted successfully.")

import os

# Specify the directory path


directory_path = '/content/daily-climate-time-series-data'

# Create the directory if it doesn't already exist


if not os.path.exists(directory_path):
os.makedirs(directory_path)
print(f"Directory '{directory_path}' created successfully.")
else:
print(f"Directory '{directory_path}' already exists.")

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

train =
pd.read_csv("/content/daily-climate-time-series-data/DailyDelhiClimateTrain.csv")
test =
pd.read_csv('/content/daily-climate-time-series-data/DailyDelhiClimateTest.csv')
print(train.head())

print(test.head())

print(train.shape)
print(test.shape)

print(train.describe())

print(test.describe())

train.info()

test.info()

train['date'] = pd.to_datetime(train['date'], format='%Y-%m-%d')


train['year'] = train['date'].dt.year
train['month'] = train['date'].dt.month
print(train.head())

test['date'] = pd.to_datetime(test['date'], format='%Y-%m-%d')


test['year'] = test['date'].dt.year
test['month'] = test['date'].dt.month
print(test.head())

train.tail(5)

test.tail(5)

print(train.info())

print(test.info())

figure = px.line(train, x="date",


y="meantemp",
title='Mean Temperature in Delhi Over the Years')
figure.show()

fig, ax = plt.subplots(figsize=(14, 4)) # Adjust the size of the figure as needed

# Plot the data


ax.plot(train['date'], train['meantemp'])

# Set the title, x-label, and y-label for the plot


ax.set_title('Mean Temperature in Delhi Over the Years')
ax.set_xlabel('Date')
ax.set_ylabel('Mean Temperature')

# Add a background color


ax.axhspan(min(train['meantemp']), max(train['meantemp']), facecolor='lightgray',
alpha=0.3)
ax.axvspan(min(train['date']), max(train['date']), facecolor='lightgray',
alpha=0.3)

plt.show()

figure = px.line(train, x="date",


y="humidity",
title='Humidity in Delhi Over the Years')
figure.show()

figure = px.line(train, x="date",


y="wind_speed",
title='Wind Speed in Delhi Over the Years')
figure.show()

figure = px.scatter(data_frame = train, x="humidity",


y="meantemp", size="meantemp",
trendline="ols",
title = "Relationship Between Temperature and Humidity")
figure.show()

from matplotlib import style


print(style.available)

plt.style.use('fivethirtyeight')
plt.figure(figsize=(16, 6))
plt.title("Temperature Change in Delhi Over the Years")
sns.lineplot(data = train, x='month', y='meantemp', hue='year')
plt.show()
!pip install prophet

train_data = train.rename(columns = {'date' : 'ds', 'meantemp':'y'})


train_data

test_data = test.rename(columns = {'date' : 'ds', 'meantemp':'y'})


test_data

from prophet import Prophet

# Create and fit the Prophet model


model = Prophet()
model.fit(train_data)

# Create future dates to forecast


future_dates = model.make_future_dataframe(periods=len(test_data))

# Make predictions
predictions = model.predict(future_dates)

predictions

# Merge predictions with actual values from the test dataset


forecasted_data = predictions[['ds', 'yhat', 'yhat_lower',
'yhat_upper']].merge(test_data, on='ds', how='left')
forecasted_data

# Print the forecasted values


print(forecasted_data[['ds', 'yhat', 'y', 'yhat_lower',
'yhat_upper']].iloc[1462:1575])

# Visualize the forecasted_data


from prophet.plot import plot_plotly, plot_components_plotly

plot_plotly(model, predictions)

from prophet import Prophet

# Create and fit the Prophet model


model = Prophet()
model.fit(train_data)

# Create future dates to forecast


future_dates365 = model.make_future_dataframe(periods=365) # Adjust the number of
forecast steps as needed

# Make predictions
predictions365 = model.predict(future_dates365)

predictions365

# Print the forecasted values


print(predictions365[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(365))

# Visualize the forecasted values


from prophet.plot import plot_plotly, plot_components_plotly
plot_plotly(model, predictions365)

You might also like