0% found this document useful (0 votes)
24 views9 pages

Recurrent Neural Network-Programs

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

Recurrent Neural Network-Programs

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

Recurrent Neural Network (RNN) model for a temperature forecast, we'll use a dataset

containing time-series data of temperatures. This example will walk you through data
preparation, model creation, training, and evaluation using Python with TensorFlow/Keras.

Steps:

1. Data Preparation:
o Load and preprocess the dataset.
o Normalize the data for efficient training.
o Prepare sequences of data for the RNN input.
2. RNN Model Creation:
o Define the architecture of the RNN using Keras.
o Compile the model with appropriate loss functions and optimizers.
3. Model Training:
o Train the RNN model on the prepared dataset.
o Use validation data to monitor the model's performance.
4. Evaluation and Prediction:
o Evaluate the model using test data.
o Make predictions and visualize the results.

1. Data Preparation

Let's start with data preparation. We'll assume you have a time-series dataset containing
temperature values.

python
Copy code
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split

# Load the dataset


# Assuming 'temperature.csv' has columns 'Date' and 'Temperature'
data = pd.read_csv('temperature.csv', parse_dates=['Date'],
index_col='Date')

# Normalize the temperature values


scaler = MinMaxScaler(feature_range=(0, 1))
data['Temperature'] =
scaler.fit_transform(data['Temperature'].values.reshape(-1, 1))

# Prepare the sequences for the RNN


def create_sequences(data, seq_length):
X, y = [], []
for i in range(len(data) - seq_length):
X.append(data[i:i + seq_length])
y.append(data[i + seq_length])
return np.array(X), np.array(y)

seq_length = 10 # Number of previous time steps to consider


X, y = create_sequences(data['Temperature'].values, seq_length)

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

2. RNN Model Creation

We'll create a simple RNN model using Keras.

python
Copy code
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, SimpleRNN

# Define the RNN model


model = Sequential()
model.add(SimpleRNN(50, activation='relu', input_shape=(seq_length, 1)))
model.add(Dense(1)) # Output layer

# Compile the model


model.compile(optimizer='adam', loss='mse')

# Print the model summary


model.summary()

3. Model Training

We'll train the model on the training data and validate it using a portion of the data.

python
Copy code
# Reshape the data to fit the RNN input (samples, timesteps, features)
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))

# Train the model


history = model.fit(X_train, y_train, epochs=50, validation_split=0.2,
batch_size=16)

4. Evaluation and Prediction

Finally, we'll evaluate the model and visualize its predictions.

python
Copy code
import matplotlib.pyplot as plt

# Evaluate the model on the test data


test_loss = model.evaluate(X_test, y_test)
print(f"Test Loss: {test_loss}")

# Make predictions
predictions = model.predict(X_test)

# Inverse transform the predictions to get actual temperature values


predictions = scaler.inverse_transform(predictions)
y_test = scaler.inverse_transform(y_test.reshape(-1, 1))

# Plot the results


plt.figure(figsize=(10, 6))
plt.plot(y_test, label='Actual Temperature')
plt.plot(predictions, label='Predicted Temperature')
plt.xlabel('Time')
plt.ylabel('Temperature')
plt.legend()
plt.show()

create a CSV (Comma-Separated Values) file in Python, you can use the pandas library or
the built-in csv module. Below, I'll show you how to do it using both methods.

Method 1: Using Pandas

Pandas is a powerful library for data manipulation and analysis. It makes creating and
working with CSV files straightforward.

Example:

python
Copy code
import pandas as pd

# Sample data in dictionary form


data = {
'Date': ['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04'],
'Temperature': [30.5, 32.0, 33.1, 29.8]
}

# Create a DataFrame
df = pd.DataFrame(data)

# Save the DataFrame to a CSV file


df.to_csv('temperature.csv', index=False)

print("CSV file created successfully!")

Method 2: Using the CSV Module

The built-in csv module is also a simple way to create CSV files.

Example:

python
Copy code
import csv

# Sample data as a list of dictionaries


data = [
{'Date': '2024-01-01', 'Temperature': 30.5},
{'Date': '2024-01-02', 'Temperature': 32.0},
{'Date': '2024-01-03', 'Temperature': 33.1},
{'Date': '2024-01-04', 'Temperature': 29.8}
]

# Specify the CSV file's header


header = ['Date', 'Temperature']

# Write the data to a CSV file


with open('temperature.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=header)

# Write the header


writer.writeheader()

# Write the data


writer.writerows(data)

print("CSV file created successfully!")

Summary:

 Pandas Method: Ideal for working with data in a tabular format and offers more
flexibility.
 CSV Module: Useful for simple CSV operations when you don't want to rely on
external libraries.

Both methods will create a CSV file named temperature.csv in your working directory
with the given data.

1. Load the Dataset

First, you'll load the dataset using pandas. Assuming you have a CSV file named
temperature.csv:

python
Copy code
import pandas as pd

# Load the dataset


data = pd.read_csv('temperature.csv')

# Display the first few rows of the dataset


print(data.head())

2. Get Basic Information

You can use the info() method to get a summary of the DataFrame, including the number of
non-null entries and the data types of each column:

python
Copy code
# Get a summary of the dataset
print(data.info())

3. Check for Missing Values

Missing values can affect model performance, so it's essential to check if there are any in the
dataset:
python
Copy code
# Check for missing values
print(data.isnull().sum())

4. Statistical Summary

Use the describe() method to get a statistical summary of the numerical columns:

python
Copy code
# Get a statistical summary of the dataset
print(data.describe())

5. Inspect Specific Columns

If you want to inspect specific columns, you can print out the unique values or check for any
anomalies:

python
Copy code
# Inspect the 'Date' column
print(data['Date'].unique())

# Inspect the 'Temperature' column


print(data['Temperature'].describe())

6. Visualize the Data

For a quick visual inspection, you can plot the data to understand trends, patterns, and
anomalies:

python
Copy code
import matplotlib.pyplot as plt

# Plot the temperature over time


plt.figure(figsize=(10, 6))
plt.plot(data['Date'], data['Temperature'], label='Temperature')
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.title('Temperature Over Time')
plt.legend()
plt.xticks(rotation=45)
plt.show()

Summary of Steps:

1. Load the Data: Use pd.read_csv() to load the dataset.


2. Basic Information: Use info() to get an overview of the dataset.
3. Missing Values: Check for missing data using isnull().sum().
4. Statistical Summary: Use describe() for a quick statistical summary.
5. Inspect Columns: Look at unique values and descriptive statistics for individual
columns.
6. Visualization: Plot the data to get a visual sense of trends.

By following these steps, you can thoroughly inspect and understand the weather dataset
before proceeding with more complex operations like modeling or forecasting.

Parsing data involves processing and transforming raw data into a structured format that's
more suitable for analysis or modeling. In the context of a weather dataset, this often involves
tasks like converting date strings to datetime objects, handling missing values, and extracting
useful features.

Here’s how you can parse and prepare the weather dataset using Python:

1. Convert Date Strings to Datetime Objects

If the Date column is in string format, converting it to a datetime object is essential for time
series analysis.

python
Copy code
import pandas as pd

# Load the dataset


data = pd.read_csv('temperature.csv')

# Convert the 'Date' column to datetime format


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

# Set 'Date' as the index if it's a time series


data.set_index('Date', inplace=True)

# Inspect the first few rows to verify changes


print(data.head())

2. Handle Missing Values

You may need to handle missing data points by filling them in or removing them, depending
on the situation:

python
Copy code
# Fill missing temperature values with the mean (or another method)
data['Temperature'].fillna(data['Temperature'].mean(), inplace=True)

# Alternatively, you can drop rows with missing values


# data.dropna(inplace=True)

# Check again for missing values


print(data.isnull().sum())

3. Extract Features (Optional)

You might want to extract additional features from the date, such as the day of the week,
month, or year, to use in your model:
python
Copy code
# Extract additional features from the 'Date' index
data['Year'] = data.index.year
data['Month'] = data.index.month
data['Day'] = data.index.day
data['DayOfWeek'] = data.index.dayofweek

# Inspect the dataset with new features


print(data.head())

4. Resample or Aggregate Data (Optional)

If your data is at a finer granularity than needed (e.g., hourly data when you need daily data),
you can resample or aggregate it:

python
Copy code
# Resample the data to daily frequency, if not already in daily format
# For example, if the data was hourly, you can resample to daily
data_daily = data.resample('D').mean()

# Inspect the resampled data


print(data_daily.head())

5. Normalize/Scale Data (Optional)

If you're going to use the data for machine learning, especially models like neural networks,
it's often useful to scale the data:

python
Copy code
from sklearn.preprocessing import MinMaxScaler

# Initialize the scaler


scaler = MinMaxScaler()

# Apply the scaler to the 'Temperature' column


data['Temperature'] =
scaler.fit_transform(data['Temperature'].values.reshape(-1, 1))

# Inspect the scaled data


print(data.head())

6. Save the Parsed Data (Optional)

Once you've parsed and prepared the data, you might want to save it back to a CSV for later
use:

python
Copy code
# Save the parsed data to a new CSV file
data.to_csv('parsed_temperature.csv')

print("Parsed data saved to 'parsed_temperature.csv'.")


Summary of Parsing Steps:

1. Convert Dates: Convert the Date column to a datetime format for time series
analysis.
2. Handle Missing Values: Fill or remove missing values to ensure clean data.
3. Feature Extraction: Derive additional features like the year, month, day, or day of
the week.
4. Resampling: Aggregate the data to the desired frequency (e.g., daily).
5. Normalize Data: Scale the data if required for machine learning models.
6. Save Parsed Data: Optionally, save the processed data to a new file.

By following these steps, you'll have a clean and well-structured dataset ready for analysis or
modeling.

import pandas as pd
import matplotlib.pyplot as plt

# Sample data (You can replace this with your actual data)
data = {
'Date': ['2024-08-01', '2024-08-02', '2024-08-03', '2024-08-04', '2024-
08-05'],
'Temperature': [30, 31, 29, 32, 30]
}

# Convert the data to a DataFrame


df = pd.DataFrame(data)

# Convert the 'Date' column to datetime


df['Date'] = pd.to_datetime(df['Date'])

# Plot the time series


plt.figure(figsize=(10, 5))
plt.plot(df['Date'], df['Temperature'], marker='o')

# Add labels and title


plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Temperature Time Series')

# Show grid
plt.grid(True)

# Show the plot


plt.show()

How It Works:

1. Data Preparation: The data is organized into a DataFrame with dates and
corresponding temperature values.
2. Plotting: The plot() function creates the time series graph.
3. Customization: Labels, title, and grid are added to make the plot more informative.

If you have actual data, you can load it into the DataFrame instead of using the sample data
provided in the code.
Here’s how you can modify the code to plot the first 10 days of a temperature time series.
Assuming you have more than 10 days of data, the following example will focus on plotting
just the first 10 days.

python
Copy code
import pandas as pd
import matplotlib.pyplot as plt

# Sample data (Replace this with your actual data)


data = {
'Date': pd.date_range(start='2024-08-01', periods=15, freq='D'),
'Temperature': [30, 31, 29, 32, 30, 33, 34, 35, 31, 29, 28, 27, 26, 30,
32]
}

# Convert the data to a DataFrame


df = pd.DataFrame(data)

# Convert the 'Date' column to datetime if it's not already


df['Date'] = pd.to_datetime(df['Date'])

# Select the first 10 days


df_first_10_days = df.head(10)

# Plot the time series for the first 10 days


plt.figure(figsize=(10, 5))
plt.plot(df_first_10_days['Date'], df_first_10_days['Temperature'],
marker='o')

# Add labels and title


plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Temperature Time Series - First 10 Days')

# Show grid
plt.grid(True)

# Show the plot


plt.show()

How This Works:

1. Data Filtering: The .head(10) function is used to select the first 10 rows from the
DataFrame.
2. Plotting: The time series plot is then generated for only these 10 days.

This will give you a focused plot showing just the temperature data for the first 10 days in
your dataset.

You might also like