import numpy as np
import pandas as pd
from catboost import CatBoostRegressor, CatBoostClassifier
import os
def create_regression_model():
print("Creating sample regression model...")
np.random.seed(42)
n_samples = 1000
n_features = 5
X = np.random.randn(n_samples, n_features)
y = (X[:, 0] * 2 + X[:, 1] * 1.5 + X[:, 2] * 0.5 +
X[:, 0] * X[:, 1] * 0.3 + np.random.normal(0, 0.1, n_samples))
model = CatBoostRegressor(
iterations=100,
depth=4,
learning_rate=0.1,
loss_function='RMSE',
verbose=False
)
model.fit(X, y)
os.makedirs('tmp', exist_ok=True)
model.save_model('tmp/model.bin')
print("Regression model saved to tmp/model.bin")
test_features = [1.0, 2.0, 3.0, 4.0, 5.0]
prediction = model.predict([test_features])[0]
print(f"Test prediction for {test_features}: {prediction:.6}")
return model
def create_classification_model():
print("\nCreating sample classification model...")
np.random.seed(42)
n_samples = 1000
n_features = 5
X = np.random.randn(n_samples, n_features)
categorical_features = np.random.choice(['A', 'B', 'C'], size=(n_samples, 3))
y = (X[:, 0] + X[:, 1] > 0).astype(int)
X_combined = np.column_stack([X, categorical_features])
model = CatBoostClassifier(
iterations=100,
depth=4,
learning_rate=0.1,
loss_function='Logloss',
verbose=False
)
model.fit(X_combined, y, cat_features=[5, 6, 7])
model.save_model('tmp/classification_model.bin')
print("Classification model saved to tmp/classification_model.bin")
test_features = [1.0, 2.0, 3.0, 4.0, 5.0]
test_cat_features = ['A', 'B', 'C']
test_combined = test_features + test_cat_features
prediction = model.predict_proba([test_combined])[0]
print(f"Test prediction for {test_combined}: {prediction}")
return model
def main():
print("CatBoost Sample Model Generator")
print("===============================")
try:
regression_model = create_regression_model()
classification_model = create_classification_model()
print("\n✅ Sample models created successfully!")
print("\nYou can now run the Rust examples:")
print(" cargo run --example basic_usage")
print(" cargo run --example advanced_usage")
except ImportError as e:
print(f"❌ Error: {e}")
print("Please install CatBoost Python package:")
print(" pip install catboost")
except Exception as e:
print(f"❌ Error creating models: {e}")
if __name__ == "__main__":
main()