You are a graduate student researching deep learning and you need to read a dataset from…
- User
You are a graduate student researching deep learning and you need to read a dataset from "https://www.kaggle.com/datasets/muthuj7/weather-dataset", write detailed code using Google collab to build a suitable model for the purpose of predicting weather, and visualise the following results
- Assistant
To predict the weather using deep learning, you can follow these steps:
- Import necessary libraries
- Download and read the dataset
- Preprocess and prepare the dataset
- Build the model
- Train the model
- Evaluate the model
- Visualize results
Here is the Python code using Google Colab to build a suitable model for the purpose of predicting weather:
# 1. Import necessary libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import tensorflow as tf from tensorflow import keras from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler # 2. Download and read the dataset !pip install -q kaggle !mkdir -p ~/.kaggle !cp kaggle.json ~/.kaggle/ !chmod 600 ~/.kaggle/kaggle.json !kaggle datasets download -d muthuj7/weather-dataset !unzip weather-dataset.zip data = pd.read_csv('Weather.csv') # 3. Preprocess and prepare the dataset # Remove unrelevant columns data.drop(['Formatted Date', 'Loud Cover'], axis=1, inplace=True) # Convert the 'Summary' column to categorical data['Summary'] = pd.Categorical(data['Summary']).codes # Convert the 'Precip Type' column to binary data['Precip Type'] = data['Precip Type'].map({'rain': 1, 'snow': 0}) # Normalize data scaler = MinMaxScaler() data_normalized = scaler.fit_transform(data) # Create sequences sequence_length = 24 features_set = [] labels = [] for i in range(sequence_length, len(data_normalized)): features_set.append(data_normalized[i - sequence_length:i]) labels.append(data_normalized[i, -1]) # -1 is the index of the "Temperature" column features_set, labels = np.array(features_set), np.array(labels) X_train, X_test, y_train, y_test = train_test_split(features_set, labels, test_size=0.3, random_state=42) # 4. Build the model model = keras.Sequential([ keras.layers.LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])), keras.layers.Dropout(0.2), keras.layers.LSTM(units=50, return_sequences=True), keras.layers.Dropout(0.2), keras.layers.LSTM(units=50), keras.layers.Dense(units=1) ]) model.compile(optimizer='adam', loss='mean_squared_error') # 5. Train the model history = model.fit(X_train, y_train, epochs=30, batch_size=50, validation_data=(X_test, y_test), verbose=2) # 6. Evaluate the model test_loss = model.evaluate(X_test, y_test) # 7. Visualize results plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model Loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper right') plt.show()This code downloads the dataset from Kaggle, cleans, and preprocesses the data. Then it constructs an LSTM model in TensorFlow, trains it, and visualizes the training and validation loss. Remember to place your
kaggle.jsonfile with Kaggle API credentials in the current working directory before running the code.