Skip to content

Interface

Saman .E edited this page May 8, 2023 · 1 revision

To run the MTGBClassifier and MTGBRegressor models on a toy dataset, we need to first import the necessary libraries and create a sample dataset. Here is an example code to demonstrate the usage of the model on the sample dataset:

import numpy as np
from sklearn.datasets import make_classification, make_regression
from sklearn.model_selection import train_test_split
from mtgb import MTGBClassifier
from mtgb import MTGBRegressor

# create a classification dataset
X_clf, y_clf = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)
X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X_clf, y_clf, test_size=0.2, random_state=42)

# create a regression dataset
X_reg, y_reg = make_regression(n_samples=1000, n_features=10, random_state=42)
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.2, random_state=42)

# create MTGBClassifier and MTGBRegressor instances
clf = MTGBClassifier(n_estimators=100, max_depth=3, random_state=42)
reg = MTGBRegressor(n_estimators=100, max_depth=3, random_state=42)

# fit the models on the datasets
clf.fit(X_train_clf, y_train_clf)
reg.fit(X_train_reg, y_train_reg)

# predict on the test data
y_pred_clf = clf.predict(X_test_clf)
y_pred_reg = reg.predict(X_test_reg)

# evaluate the models
clf_score = clf.score(X_test_clf, y_test_clf)
reg_score = reg.score(X_test_reg, y_test_reg)

print("MTGBClassifier accuracy:", clf_score)
print("MTGBRegressor RMSE:", reg_score)

In this example, we first created a classification dataset and a regression dataset using the make_classification and make_regression functions from sklearn.datasets. We then split the datasets into training and testing sets using the train_test_split function.

Next, we created instances of the MTGBClassifier and MTGBRegressor classes and set the desired hyperparameters. We then fit the models on the training datasets using the fit method.

Finally, we predicted on the testing datasets using the prediction method and evaluated the models using the scoring method. For the MTGBClassifier, we used the default scoring metric which is accuracy, and for the MTGBRegressor, we used the root mean squared error (RMSE) metric.

Clone this wiki locally