File size: 1,822 Bytes
5cbc1e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score

# Load the data
train_data = pd.read_csv("./input/train.csv")
test_data = pd.read_csv("./input/test.csv")

# Separate features and target
X = train_data.drop(["id", "target"], axis=1)
y = train_data["target"]
X_test = test_data.drop(["id"], axis=1)

# Identify categorical features
cat_features = [col for col in X.columns if X[col].dtype == "object"]

# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the CatBoostClassifier with a smaller number of iterations for faster grid search
model = CatBoostClassifier(
    iterations=100,  # Reduced number of iterations for grid search
    learning_rate=0.1,
    depth=4,
    loss_function="Logloss",
    early_stopping_rounds=10,
    verbose=False,
)

# Fit the model on the training data
model.fit(X_train, y_train, cat_features=cat_features)

# Predict on the validation set
val_pred = model.predict_proba(X_val)[:, 1]

# Calculate the ROC AUC score
val_auc = roc_auc_score(y_val, val_pred)
print(f"Validation AUC: {val_auc}")

# Train the final model on the full dataset with more iterations
final_model = CatBoostClassifier(
    iterations=1000,  # Increased number of iterations for final training
    learning_rate=0.1,
    depth=4,
    loss_function="Logloss",
    early_stopping_rounds=10,
    verbose=False,
)

final_model.fit(X, y, cat_features=cat_features)

# Predict on the test set
test_pred = final_model.predict_proba(X_test)[:, 1]

# Save the predictions to a CSV file
submission = pd.DataFrame({"id": test_data["id"], "target": test_pred})
submission.to_csv("./working/submission.csv", index=False)