Currency exchange rate prediction using Decision Tree Algorithm in Python

This tutorial will discuss currency exchange rate prediction using a decision tree algorithm using Python programming.

Currency exchange is one of the most important financial markets. currently, 1 dollar is equal to 81.45 rupees.

We will discuss the currency exchange rate between us dollars and Indian rupees.

You can learn more details about Decision Tree Algorithm.

You can download the dataset from here dataset.

So let’s try to implement the code

Import the necessary Python libraries.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

The first step is Data Collection,

1. Load data,

currency_data=pd.read_csv("C:\\Users\\usersdrive\\OneDrive\\Desktop\\dataset.csv")

To check whether the data set is loaded or not print the first five rows by using the head method.

currency_data.head()

OUTPUT:-

Now print the last five rows by using the tail method.

currency_data.tail()

OUTPUT:-

Remember this point whenever u are working on machine learning problems that need data exploration.

DATASET INFORMATION

currency_data.info()

OUTPUT:-

By seeing the above output all the columns are in float values it is easy to understand the machines.

Let’s check whether there are any missing values present in the given dataset.

currency_data.isnull().sum()

OUTPUT:-

No Null values are present in the given data set.

So this dataset is better for the prediction.

To check how many rows and columns are present in the given dataset.

currency_data.shape

OUTPUT:-

(262, 7)

Now let’s check the basic information for the data.

currency_data.describe()

OUTPUT:-

DATA VISUALIZATION

In this dataset, the values in the “Close” column are the target values that we need to predict. So let’s take a closer look at these values.

Let’s try the visualization with the graph.

plt.figure(figsize=(15, 7))
plt.title("INR - USD Exchange Rate")
plt.xlabel("Date")
plt.ylabel("Close")
plt.plot(currency_data["Close"])
plt.show()

OUTPUT:-

Now, let’s check the corelation of the data.

Now let’s have a look at the correlation between the features before training the currency exchange rate prediction model.

print(currency_data.corr())

OUTPUT:-

Let’s try to implement the heat map.

sns.heatmap(currency_data.corr())
plt.show()

OUTPUT:-

Now the next step is to prepare the dataset by storing the most relevant features in the variable x and storing the target
column in the variable y.

SPLIT THE TRAIN AND TEST DATA

x = currency_data[["Open", "High", "Low"]]
y = currency_data["Close"]
x = x.to_numpy()
y = y.to_numpy()
y = y.reshape(-1, 1)

Now let’s split the dataset and train a currency exchange prediction model using the Decision Tree Regression model.

from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)

Let’s check the trained values.

print(x_train)

OUTPUT:-

.

..

.

.

Now, let’s check the predicted values of y.

print(y_test)

OUTPUT:-

MODEL EVALUATION

from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor()
model.fit(xtrain, ytrain)
ypred = model.predict(xtest)

Now let’s have a look at the predicted values of currency exchange rates of Indian Rupees for the next 5 days.

data = pd.DataFrame(data={"Predicted Rate": ypred.flatten()})
print(data.head())

OUTPUT:-

Now, let’s check for last.

data = pd.DataFrame(data={"Predicted Rate": ypred.flatten()})
print(data.tail())

OUTPUT:-

This is about the currency exchange rate prediction.

Leave a Reply

Your email address will not be published. Required fields are marked *