Here is the Python code to preprocess the data, train a model to predict the 90% interval of DBWT using CatBoost and MAPIE libraries:
import pandas as pd
from catboost import CatBoostRegressor, Pool
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from mapie.metrics import pinball_loss
# Load train and test data
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
# Concatenate data
data = pd.concat([train_df, test_df])
# Preprocess categorical variables
le = LabelEncoder()
cat_cols = ['var1', 'var2', 'var3'] # list of categorical variables
for col in cat_cols:
data[col] = le.fit_transform(data[col])
# Separate features and target
X = data.drop('DBWT', axis=1)
y = data['DBWT']
# Split data
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=42)
# Train CatBoost model
model = CatBoostRegressor(iterations=500, learning_rate=0.1, depth=6)
model.fit(X_train, y_train)
# Make predictions
preds = model.predict(X_valid)
# Evaluate model
lower = mapie.metrics.empirical_quantile(y_valid, 0.05)
upper = mapie.metrics.empirical_quantile(y_valid, 0.95)
coverage = (lower < preds) & (upper > preds)
coverage_rate = coverage.mean()
pinball = pinball_loss(y_valid, preds, 0.05) + pinball_loss(y_valid, preds, 0.95)
print(f'Coverage Rate: {coverage_rate:.3f}')
print(f'Pinball Loss: {pinball:.3f}')
This loads the data, preprocesses categorical variables, trains a CatBoost model, makes predictions, and evaluates the 90% coverage rate and pinball loss of the predictions