-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Catboost regressor #11877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catboost regressor #11877
Changes from 2 commits
ce56390
65048fd
ff79acd
758ae1d
7ddda2c
c9d4aef
8c18770
a60c001
48893a0
7762ab8
dc39bda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
CatBoost Regressor Example. | ||
|
||
This script demonstrates the usage of the CatBoost Regressor for a simple regression task. | ||
Check failure on line 4 in machine_learning/catboost_regressor.py
|
||
CatBoost is a powerful gradient boosting library that handles categorical features automatically | ||
Check failure on line 5 in machine_learning/catboost_regressor.py
|
||
and is highly efficient. | ||
|
||
Make sure to install CatBoost using: | ||
pip install catboost | ||
|
||
Contributed by: @AHuzail | ||
""" | ||
|
||
import numpy as np | ||
from sklearn.datasets import load_boston | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.metrics import mean_squared_error | ||
from catboost import CatBoostRegressor | ||
|
||
|
||
def data_handling() -> tuple: | ||
Check failure on line 21 in machine_learning/catboost_regressor.py
|
||
""" | ||
Loads and handles the dataset, splitting it into features and targets. | ||
|
||
The Boston dataset is used as a regression example. | ||
|
||
Check failure on line 26 in machine_learning/catboost_regressor.py
|
||
Returns: | ||
tuple: A tuple of (features, target), where both are numpy arrays. | ||
|
||
Example: | ||
>>> features, target = data_handling() | ||
>>> features.shape | ||
(506, 13) | ||
>>> target.shape | ||
(506,) | ||
""" | ||
# Load Boston dataset (note: this dataset may be deprecated, replace if needed) | ||
boston = load_boston() | ||
features = boston.data | ||
target = boston.target | ||
return features, target | ||
|
||
|
||
def catboost_regressor(features: np.ndarray, target: np.ndarray) -> CatBoostRegressor: | ||
""" | ||
Trains a CatBoostRegressor using the provided features and target values. | ||
|
||
Args: | ||
features (np.ndarray): The input features for the regression model. | ||
target (np.ndarray): The target values for the regression model. | ||
|
||
Returns: | ||
CatBoostRegressor: A trained CatBoost regressor model. | ||
|
||
Example: | ||
>>> features, target = data_handling() | ||
>>> model = catboost_regressor(features, target) | ||
>>> isinstance(model, CatBoostRegressor) | ||
True | ||
""" | ||
regressor = CatBoostRegressor(iterations=100, learning_rate=0.1, depth=6, verbose=0) | ||
regressor.fit(features, target) | ||
return regressor | ||
|
||
|
||
def main() -> None: | ||
""" | ||
Main function to run the CatBoost Regressor example. | ||
|
||
It loads the data, splits it into training and testing sets, | ||
trains the regressor on the training data, and evaluates its performance | ||
on the test data. | ||
""" | ||
# Load and split the dataset | ||
features, target = data_handling() | ||
x_train, x_test, y_train, y_test = train_test_split( | ||
features, target, test_size=0.25, random_state=42 | ||
) | ||
|
||
# Train CatBoost Regressor | ||
regressor = catboost_regressor(x_train, y_train) | ||
|
||
# Predict on the test set | ||
predictions = regressor.predict(x_test) | ||
|
||
# Evaluate the performance using Mean Squared Error | ||
mse = mean_squared_error(y_test, predictions) | ||
print(f"Mean Squared Error on Test Set: {mse:.4f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod(verbose=True) | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
machine_learning/catboost_regressor.py
, please provide doctest for the functionmain