0% found this document useful (0 votes)
7 views2 pages

Catboost Code

The document outlines a Python script that loads stock data, preprocesses it, and uses a CatBoostRegressor to predict the percentage change in stock prices for the 20th day. It calculates the percentage change, prepares the data for training and testing, and visualizes both actual and predicted changes. The script includes data loading, preprocessing, model training, prediction, and plotting functionalities.

Uploaded by

Nani Kumar
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)
7 views2 pages

Catboost Code

The document outlines a Python script that loads stock data, preprocesses it, and uses a CatBoostRegressor to predict the percentage change in stock prices for the 20th day. It calculates the percentage change, prepares the data for training and testing, and visualizes both actual and predicted changes. The script includes data loading, preprocessing, model training, prediction, and plotting functionalities.

Uploaded by

Nani Kumar
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/ 2

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from catboost import CatBoostRegressor

# Load the stock data


file_path = '/content/BAJAJ-AUTO.NS_stock_data_inr.csv'
stock_data = pd.read_csv(file_path)

# Preprocess the data


stock_data['Date'] = pd.to_datetime(stock_data['Date'])
stock_data.sort_values('Date', inplace=True)
close_prices = stock_data['Adj Close'].values

# Calculate percentage change


stock_data['Pct_Change'] = stock_data['Adj Close'].pct_change() * 100
stock_data.dropna(inplace=True)

# Define features and target


window_size = 20
X, y = [], []
for i in range(window_size, len(stock_data)):
X.append(stock_data['Pct_Change'].iloc[i-window_size:i].values)
y.append(stock_data['Pct_Change'].iloc[i])

X, y = np.array(X), np.array(y)

# Split data into training and testing sets


split_index = int(0.8 * len(X))
X_train, X_test = X[:split_index], X[split_index:]
y_train, y_test = y[:split_index], y[split_index:]

# Train CatBoost model


catboost_model = CatBoostRegressor(iterations=1000, learning_rate=0.1, depth=6,
verbose=0)
catboost_model.fit(X_train, y_train)

# Predict for the next 20th day


last_sequence = stock_data['Pct_Change'].iloc[-window_size:].values.reshape(1, -1)
predicted_change = catboost_model.predict(last_sequence)

# Output the predicted percentage change


print(f"Predicted percentage change for the 20th day: {predicted_change[0]:.2f}%")

# Plotting
plt.figure(figsize=(14, 7))

# Plot the actual percentage change


plt.plot(stock_data['Date'].iloc[window_size:],
stock_data['Pct_Change'].iloc[window_size:], label='Actual Percentage Change',
color='blue')

# Plot the predicted percentage change for the next day (forecast)
predicted_dates = [stock_data['Date'].iloc[-1] + pd.Timedelta(days=1)]
plt.plot(predicted_dates, predicted_change, label='Predicted Change (20th Day)',
marker='o', color='red')

plt.title('Stock Percentage Change Prediction')


plt.xlabel('Date')
plt.ylabel('Percentage Change (%)')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()

plt.show()

You might also like