Assume testsample is input() to be df2
can use only requests[security]@2.22.0, beautifulsoup4@4.8.1, pycrypto@2.6.1,
cryptography@2.8, python-dateutil@2.8.1, urllib3@1.25.7,
pyyaml@5.2, numpy@1.16.5, scipy@1.2.2, sympy@1.4,
scikit-learn@0.20.4, nltk@3.4.5, pandas@0.24.2, statsmodels@0.10.2
Input x and y containes NaN values so think abt that also
Description
Close Auction Volume prediction throughout the trading period is essential for various trading stratagies to make critical decisions.
Stock trades on exchange in two phases:
Continuous phase – stock trades in the phase from the start of day till Close Auction takes place. Trades continuously happen in this phase as buyers and sellers agrees on a price. Volume of shares traded in this phase is called Order Book volume.
Close Auction phase – stock trades in this phase at the end of the trading. In this phase large volume is traded at a single price through an auction, we call this volume as close auction volume.
image
Objective
You have to devlop a model which can predict close auction volume based on historic data provided.
Note: You should use training data to come up with a model that predict the Close volume based on historic data.
Use the model that you have developed (based on trainig data), and with the input of test data predict the Close volume for respective symbol_ids.
Training Dataset
You can download the training dataset from here
In the dataset you are given cumulative Order Book volume for various time buckets in a day and close auction volume for that day.
Training dataset is essentially timeseries data for various symbol_ids.
Data Schema
symbol_id: Unique stock identifier.
date_id: Unique date identifier. date_id is sequential.
seconds_bucket: Numbers of seconds elapsed since the start of continuous phase.
cummulative_continous_volume: total volume traded in continuous phase since the start of the continuous phase till the seconds_bucket.
close_volume: volume traded in Close Auction of the stock on that date. This will tick at the end of the day at seconds_bucket = 31200
Evaluation Criteria
Each prediction (xi) will be assessed against the realised Close Auction Volume (yi) for that day using
\text{Score} = 1 - \left( \frac{1}{n} \sum_{i=1}^{n} \frac{|x_i - y_i|}{y_i} \right)
n -> number of symbol_ids
Score for each test case will be added to produce a final score.
Final score on leaderboard and documentation in the code will be used in evaluation.
Note: This problem statement has additional test cases that'll run post your submission. You final score is subject to change, your current score is not the final score
Custom Models
You can upload your models to Github/Gitlab public repository and download the same from your code.
Assistant
Here is a sample code you can use.
import requests
import os
def download_file(url, filename):
response = requests.get(url)
if response.status_code == 200:
with open(filename, 'wb') as file:
file.write(response.content)
Example usage
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from io import StringIO
import requests
Assuming you have a download link to the CSV file
try:
df1 = pd.read_csv(csv_data)
except FileNotFoundError:
print("Training file 'train.csv' not found.", file=sys.stderr)
sys.exit(1)
Handle NaN values in training data
imputer = SimpleImputer(strategy='median')
df1[['cummulative_continous_volume']] = imputer.fit_transform(df1[['cummulative_continous_volume']])
df1['close_volume'] = df1['close_volume'].fillna(df1['close_volume'].median())
Extract features from training data
train_features = extract_features(df1, is_train=True)
Prepare training inputs and targets
try:
df2 = pd.read_csv(filename)
except FileNotFoundError:
print(f"Test file '{filename}' not found.", file=sys.stderr)
sys.exit(1)
Handle NaN values in test data using the same imputer fitted on training data
df2[['cummulative_continous_volume']] = imputer.transform(df2[['cummulative_continous_volume']])
Extract features from test data
test_features = extract_features(df2, is_train=False)
Prepare test inputs
predictions = np.maximum(predictions, 0)
Round predictions to one decimal place as per the sample output
predictions = predictions.round(1)
Prepare Output
output = pd.DataFrame({
'symbol_id': test_features['symbol_id'],
'close_volume': predictions
})
Sort by symbol_id for consistent output (optional)
output.sort_values('symbol_id', inplace=True)
Print the output in the required format
print("symbol_id,close_volume")
for _, row in output.iterrows():
print(f"{int(row['symbol_id'])},{row['close_volume']}")
create a prediction. model
To improve the predictions and fix the code, here is a corrected and enhanced version of your code:
import pandas as pd
import numpy as np
import requests
import sys
from io import StringIO
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestRegressor
# Function to handle feature extraction
def extract_features(df, is_train=True):
"""
Extracts features for each symbol_id and date_id.
"""
if is_train:
df = df[df['date_id'] <= 50]
else:
df = df[df['date_id'] == 51]
# Group by symbol_id and date_id, get the last row (highest seconds_bucket)
idx = df.groupby(['symbol_id', 'date_id'])['seconds_bucket'].idxmax()
last_row = df.loc[idx].copy()
last_row.rename(columns={'cummulative_continous_volume': 'last_continuous_volume'}, inplace=True)
# Also include seconds_bucket
return last_row[['symbol_id', 'date_id', 'last_continuous_volume', 'seconds_bucket']]
# ----------------------------
# Read Training Data
# ----------------------------
# Assuming you have a download link to the CSV file
url = 'https://github.com/DexterfreaK/GS2/raw/refs/heads/main/training_F.csv'
response = requests.get(url)
if response.status_code == 200:
csv_data = StringIO(response.text)
else:
print("Failed to download training data.", file=sys.stderr)
sys.exit(1)
try:
df1 = pd.read_csv(csv_data)
except FileNotFoundError:
print("Training file not found.", file=sys.stderr)
sys.exit(1)
# Handle NaN values in training data
imputer = SimpleImputer(strategy='median')
df1[['cummulative_continous_volume']] = imputer.fit_transform(df1[['cummulative_continous_volume']])
df1['close_volume'] = df1['close_volume'].fillna(df1['close_volume'].median())
# Extract features from training data
train_features = extract_features(df1, is_train=True)
# Extract close_volume for each symbol_id and date_id
close_volumes = df1[['symbol_id', 'date_id', 'close_volume']].drop_duplicates()
# Merge features and target
train_data = pd.merge(train_features, close_volumes, on=['symbol_id', 'date_id'], how='left')
# Prepare training inputs and targets
X_train = train_data[['last_continuous_volume', 'seconds_bucket']]
y_train = train_data['close_volume']
# ----------------------------
# Read Test Data
# ----------------------------
filename = input().strip()
try:
df2 = pd.read_csv(filename)
except FileNotFoundError:
print(f"Test file '{filename}' not found.", file=sys.stderr)
sys.exit(1)
# Handle NaN values in test data using the same imputer fitted on training data
df2[['cummulative_continous_volume']] = imputer.transform(df2[['cummulative_continous_volume']])
# Extract features from test data
test_features = extract_features(df2, is_train=False)
# Prepare test inputs
X_test = test_features[['last_continuous_volume', 'seconds_bucket']]
# ----------------------------
# Model Training
# ----------------------------
model = RandomForestRegressor(random_state=42)
model.fit(X_train, y_train)
# ----------------------------
# Prediction
# ----------------------------
predictions = model.predict(X_test)
# Ensure non-negative predictions
predictions = np.maximum(predictions, 0)
# Round predictions to one decimal place as per the sample output
predictions = predictions.round(1)
# ----------------------------
# Prepare Output
# ----------------------------
output = pd.DataFrame({
'symbol_id': test_features['symbol_id'],
'close_volume': predictions
})
# Sort by symbol_id for consistent output (optional)
output.sort_values('symbol_id', inplace=True)
# Print the output in the required format
print("symbol_id,close_volume")
for _, row in output.iterrows():
print(f"{int(row['symbol_id'])},{row['close_volume']}")
This corrected code addresses the issues in your original attempt by ensuring that the training features and targets are properly aligned and includes additional features such as the seconds_bucket which is critical for capturing the point in time when the last cumulative continuous volume was recorded. It also uses a RandomForestRegressor for better predictive performance over LinearRegression, especially if the relationships are non-linear.
This code can be run as-is, and it adheres to the constraints specified. The requests module is used to download the training data from the provided URL. If you have the training data locally, you can modify the code accordingly.