unifiedbooster 0.2.1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Copyright <2024> <T. Moudiki>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unifiedbooster
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Unified interface for Gradient Boosted Decision Trees
5
5
  Home-page: https://github.com/thierrymoudiki/unifiedbooster
6
6
  Author: T. Moudiki
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.6
16
16
  Classifier: Programming Language :: Python :: 3.7
17
17
  Classifier: Programming Language :: Python :: 3.8
18
18
  Requires-Python: >=3.6
19
+ License-File: LICENSE
19
20
  Requires-Dist: Cython
20
21
  Requires-Dist: numpy
21
22
  Requires-Dist: scikit-learn
@@ -0,0 +1,91 @@
1
+ # unifiedbooster
2
+
3
+ ![PyPI](https://img.shields.io/pypi/v/unifiedbooster) [![PyPI - License](https://img.shields.io/pypi/l/unifiedbooster)](https://github.com/thierrymoudiki/unifiedbooster/blob/master/LICENSE) [![Downloads](https://pepy.tech/badge/unifiedbooster)](https://pepy.tech/project/unifiedbooster)
4
+ [![Documentation](https://img.shields.io/badge/documentation-is_here-green)](https://techtonique.github.io/unifiedbooster/)
5
+
6
+ ## Examples
7
+
8
+ ### classification
9
+
10
+ ```python
11
+ import unifiedbooster as ub
12
+ from sklearn.datasets import load_iris, load_breast_cancer, load_wine
13
+ from sklearn.model_selection import train_test_split
14
+ from sklearn.metrics import accuracy_score
15
+
16
+ datasets = [load_iris(), load_breast_cancer(), load_wine()]
17
+
18
+ for dataset in datasets:
19
+
20
+ X, y = dataset.data, dataset.target
21
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
22
+
23
+ # Initialize the unified regressor (example with XGBoost)
24
+ regressor1 = ub.GBDTClassifier(model_type='xgboost')
25
+ regressor2 = ub.GBDTClassifier(model_type='catboost')
26
+ regressor3 = ub.GBDTClassifier(model_type='lightgbm')
27
+
28
+ # Fit the model
29
+ regressor1.fit(X_train, y_train)
30
+ regressor2.fit(X_train, y_train)
31
+ regressor3.fit(X_train, y_train)
32
+
33
+ # Predict on the test set
34
+ y_pred1 = regressor1.predict(X_test)
35
+ y_pred2 = regressor2.predict(X_test)
36
+ y_pred3 = regressor3.predict(X_test)
37
+
38
+ # Evaluate the model
39
+ accuracy1 = accuracy_score(y_test, y_pred1)
40
+ accuracy2 = accuracy_score(y_test, y_pred2)
41
+ accuracy3 = accuracy_score(y_test, y_pred3)
42
+ print("-------------------------")
43
+ print(f"Classification Accuracy xgboost: {accuracy1:.2f}")
44
+ print(f"Classification Accuracy catboost: {accuracy2:.2f}")
45
+ print(f"Classification Accuracy lightgbm: {accuracy3:.2f}")
46
+ ```
47
+
48
+ ### regression
49
+
50
+ ```python
51
+ import numpy as np
52
+ import unifiedbooster as ub
53
+ from sklearn.datasets import fetch_california_housing, load_diabetes
54
+ from sklearn.model_selection import train_test_split
55
+ from sklearn.metrics import mean_squared_error
56
+
57
+
58
+ datasets = [fetch_california_housing(), load_diabetes()]
59
+
60
+ for dataset in datasets:
61
+
62
+ # Load dataset
63
+ X, y = dataset.data, dataset.target
64
+
65
+ # Split dataset into training and testing sets
66
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
67
+
68
+ # Initialize the unified regressor (example with XGBoost)
69
+ regressor1 = ub.GBDTRegressor(model_type='xgboost')
70
+ regressor2 = ub.GBDTRegressor(model_type='catboost')
71
+ regressor3 = ub.GBDTRegressor(model_type='lightgbm')
72
+
73
+ # Fit the model
74
+ regressor1.fit(X_train, y_train)
75
+ regressor2.fit(X_train, y_train)
76
+ regressor3.fit(X_train, y_train)
77
+
78
+ # Predict on the test set
79
+ y_pred1 = regressor1.predict(X_test)
80
+ y_pred2 = regressor2.predict(X_test)
81
+ y_pred3 = regressor3.predict(X_test)
82
+
83
+ # Evaluate the model
84
+ mse1 = np.sqrt(mean_squared_error(y_test, y_pred1))
85
+ mse2 = np.sqrt(mean_squared_error(y_test, y_pred2))
86
+ mse3 = np.sqrt(mean_squared_error(y_test, y_pred3))
87
+ print("-------------------------")
88
+ print(f"Regression Root Mean Squared Error xgboost: {mse1:.2f}")
89
+ print(f"Regression Root Mean Squared Error catboost: {mse2:.2f}")
90
+ print(f"Regression Root Mean Squared Error lightgbm: {mse3:.2f}")
91
+ ```
@@ -10,7 +10,7 @@ from os import path
10
10
 
11
11
  subprocess.check_call(['pip', 'install', 'Cython'])
12
12
 
13
- __version__ = "0.2.1"
13
+ __version__ = "0.2.2"
14
14
 
15
15
  here = path.abspath(path.dirname(__file__))
16
16
 
@@ -1,4 +1,5 @@
1
+ from .gbdt import GBDT
1
2
  from .gbdt_classification import GBDTClassifier
2
3
  from .gbdt_regression import GBDTRegressor
3
4
 
4
- __all__ = ["GBDTClassifier", "GBDTRegressor"]
5
+ __all__ = ["GBDT", "GBDTClassifier", "GBDTRegressor"]
@@ -0,0 +1,131 @@
1
+ import numpy as np
2
+ from sklearn.base import BaseEstimator
3
+
4
+
5
+ class GBDT(BaseEstimator):
6
+ """Gradient Boosted Decision Trees (GBDT) base class
7
+
8
+ Attributes:
9
+
10
+ n_estimators: int
11
+ maximum number of trees that can be built
12
+
13
+ learning_rate: float
14
+ shrinkage rate; used for reducing the gradient step
15
+
16
+ rowsample: float
17
+ subsample ratio of the training instances
18
+
19
+ colsample: float
20
+ percentage of features to use at each node split
21
+
22
+ verbose: int
23
+ controls verbosity (default=0)
24
+
25
+ seed: int
26
+ reproducibility seed
27
+ """
28
+
29
+ def __init__(
30
+ self,
31
+ model_type="xgboost",
32
+ n_estimators=100,
33
+ learning_rate=0.1,
34
+ max_depth=3,
35
+ rowsample=1.0,
36
+ colsample=1.0,
37
+ verbose=0,
38
+ seed=123,
39
+ **kwargs
40
+ ):
41
+
42
+ self.model_type = model_type
43
+ self.n_estimators = n_estimators
44
+ self.learning_rate = learning_rate
45
+ self.max_depth = max_depth
46
+ self.rowsample = rowsample
47
+ self.colsample = colsample
48
+ self.verbose = verbose
49
+ self.seed = seed
50
+
51
+ if self.model_type == "xgboost":
52
+ self.params = {
53
+ "n_estimators": self.n_estimators,
54
+ "learning_rate": self.learning_rate,
55
+ "subsample": self.rowsample,
56
+ "colsample_bynode": self.colsample,
57
+ "max_depth": self.max_depth,
58
+ "verbosity": self.verbose,
59
+ "seed": self.seed,
60
+ **kwargs,
61
+ }
62
+ elif self.model_type == "lightgbm":
63
+ verbose = self.verbose - 1 if self.verbose == 0 else self.verbose
64
+ self.params = {
65
+ "n_estimators": self.n_estimators,
66
+ "learning_rate": self.learning_rate,
67
+ "subsample": self.rowsample,
68
+ "feature_fraction_bynode": self.colsample,
69
+ "max_depth": self.max_depth,
70
+ "verbose": verbose, # keep this way
71
+ "seed": self.seed,
72
+ **kwargs,
73
+ }
74
+ elif self.model_type == "catboost":
75
+ self.params = {
76
+ "iterations": self.n_estimators,
77
+ "learning_rate": self.learning_rate,
78
+ "subsample": self.rowsample,
79
+ "rsm": self.colsample,
80
+ "depth": self.max_depth,
81
+ "verbose": self.verbose,
82
+ "random_seed": self.seed,
83
+ "bootstrap_type": "Bernoulli",
84
+ **kwargs,
85
+ }
86
+
87
+ def fit(self, X, y, **kwargs):
88
+ """Fit custom model to training data (X, y).
89
+
90
+ Parameters:
91
+
92
+ X: {array-like}, shape = [n_samples, n_features]
93
+ Training vectors, where n_samples is the number
94
+ of samples and n_features is the number of features.
95
+
96
+ y: array-like, shape = [n_samples]
97
+ Target values.
98
+
99
+ **kwargs: additional parameters to be passed to
100
+ self.cook_training_set or self.obj.fit
101
+
102
+ Returns:
103
+
104
+ self: object
105
+ """
106
+
107
+ if getattr(self, "type_fit") == "classification":
108
+ self.classes_ = np.unique(y) # for compatibility with sklearn
109
+ self.n_classes_ = len(
110
+ self.classes_
111
+ ) # for compatibility with sklearn
112
+ return getattr(self, "model").fit(X, y, **kwargs)
113
+
114
+ def predict(self, X):
115
+ """Predict test data X.
116
+
117
+ Parameters:
118
+
119
+ X: {array-like}, shape = [n_samples, n_features]
120
+ Training vectors, where n_samples is the number
121
+ of samples and n_features is the number of features.
122
+
123
+ **kwargs: additional parameters to be passed to
124
+ self.cook_test_set
125
+
126
+ Returns:
127
+
128
+ model predictions: {array-like}
129
+ """
130
+
131
+ return getattr(self, "model").predict(X)
@@ -1,7 +1,11 @@
1
1
  from .gbdt import GBDT
2
2
  from sklearn.base import ClassifierMixin
3
3
  from xgboost import XGBClassifier
4
- from catboost import CatBoostClassifier
4
+
5
+ try:
6
+ from catboost import CatBoostClassifier
7
+ except:
8
+ print("catboost package can't be built")
5
9
  from lightgbm import LGBMClassifier
6
10
 
7
11
 
@@ -11,7 +15,7 @@ class GBDTClassifier(GBDT, ClassifierMixin):
11
15
  Attributes:
12
16
 
13
17
  n_estimators: int
14
- maximum number of trees that can be built
18
+ maximum number of trees that can be built
15
19
 
16
20
  learning_rate: float
17
21
  shrinkage rate; used for reducing the gradient step
@@ -24,9 +28,9 @@ class GBDTClassifier(GBDT, ClassifierMixin):
24
28
 
25
29
  verbose: int
26
30
  controls verbosity (default=0)
27
-
28
- seed: int
29
- reproducibility seed
31
+
32
+ seed: int
33
+ reproducibility seed
30
34
 
31
35
  Examples:
32
36
 
@@ -68,39 +72,57 @@ class GBDTClassifier(GBDT, ClassifierMixin):
68
72
  ```
69
73
  """
70
74
 
71
- def __init__(self,
72
- model_type='xgboost',
73
- n_estimators=100,
74
- learning_rate=0.1,
75
- max_depth=3,
76
- rowsample=1.0,
77
- colsample=1.0,
78
- verbose=0,
79
- seed=123,
80
- **kwargs):
81
-
75
+ def __init__(
76
+ self,
77
+ model_type="xgboost",
78
+ n_estimators=100,
79
+ learning_rate=0.1,
80
+ max_depth=3,
81
+ rowsample=1.0,
82
+ colsample=1.0,
83
+ verbose=0,
84
+ seed=123,
85
+ **kwargs,
86
+ ):
87
+
82
88
  self.type_fit = "classification"
83
-
89
+
84
90
  super().__init__(
85
- model_type=model_type,
86
- n_estimators=n_estimators,
87
- learning_rate=learning_rate,
88
- max_depth=max_depth,
91
+ model_type=model_type,
92
+ n_estimators=n_estimators,
93
+ learning_rate=learning_rate,
94
+ max_depth=max_depth,
89
95
  rowsample=rowsample,
90
- colsample=colsample,
91
- verbose=verbose,
92
- seed=seed,
93
- **kwargs
96
+ colsample=colsample,
97
+ verbose=verbose,
98
+ seed=seed,
99
+ **kwargs,
94
100
  )
95
101
 
96
- if model_type == 'xgboost':
102
+ if model_type == "xgboost":
97
103
  self.model = XGBClassifier(**self.params)
98
- elif model_type == 'catboost':
104
+ elif model_type == "catboost":
99
105
  self.model = CatBoostClassifier(**self.params)
100
- elif model_type == 'lightgbm':
106
+ elif model_type == "lightgbm":
101
107
  self.model = LGBMClassifier(**self.params)
102
108
  else:
103
109
  raise ValueError(f"Unknown model_type: {model_type}")
104
-
110
+
105
111
  def predict_proba(self, X):
106
- return self.model.predict_proba(X)
112
+ """Predict probabilities for test data X.
113
+
114
+ Args:
115
+
116
+ X: {array-like}, shape = [n_samples, n_features]
117
+ Training vectors, where n_samples is the number
118
+ of samples and n_features is the number of features.
119
+
120
+ **kwargs: additional parameters to be passed to
121
+ self.cook_test_set
122
+
123
+ Returns:
124
+
125
+ probability estimates for test data: {array-like}
126
+ """
127
+
128
+ return self.model.predict_proba(X)
@@ -1,7 +1,11 @@
1
1
  from .gbdt import GBDT
2
2
  from sklearn.base import RegressorMixin
3
3
  from xgboost import XGBRegressor
4
- from catboost import CatBoostRegressor
4
+
5
+ try:
6
+ from catboost import CatBoostRegressor
7
+ except:
8
+ print("catboost package can't be built")
5
9
  from lightgbm import LGBMRegressor
6
10
 
7
11
 
@@ -11,7 +15,7 @@ class GBDTRegressor(GBDT, RegressorMixin):
11
15
  Attributes:
12
16
 
13
17
  n_estimators: int
14
- maximum number of trees that can be built
18
+ maximum number of trees that can be built
15
19
 
16
20
  learning_rate: float
17
21
  shrinkage rate; used for reducing the gradient step
@@ -24,9 +28,9 @@ class GBDTRegressor(GBDT, RegressorMixin):
24
28
 
25
29
  verbose: int
26
30
  controls verbosity (default=0)
27
-
28
- seed: int
29
- reproducibility seed
31
+
32
+ seed: int
33
+ reproducibility seed
30
34
 
31
35
  Examples:
32
36
 
@@ -68,36 +72,38 @@ class GBDTRegressor(GBDT, RegressorMixin):
68
72
  ```
69
73
  """
70
74
 
71
- def __init__(self,
72
- model_type='xgboost',
73
- n_estimators=100,
74
- learning_rate=0.1,
75
- max_depth=3,
76
- rowsample=1.0,
77
- colsample=1.0,
78
- verbose=0,
79
- seed=123,
80
- **kwargs):
81
-
75
+ def __init__(
76
+ self,
77
+ model_type="xgboost",
78
+ n_estimators=100,
79
+ learning_rate=0.1,
80
+ max_depth=3,
81
+ rowsample=1.0,
82
+ colsample=1.0,
83
+ verbose=0,
84
+ seed=123,
85
+ **kwargs,
86
+ ):
87
+
82
88
  self.type_fit = "regression"
83
-
89
+
84
90
  super().__init__(
85
- model_type=model_type,
86
- n_estimators=n_estimators,
87
- learning_rate=learning_rate,
88
- max_depth=max_depth,
91
+ model_type=model_type,
92
+ n_estimators=n_estimators,
93
+ learning_rate=learning_rate,
94
+ max_depth=max_depth,
89
95
  rowsample=rowsample,
90
- colsample=colsample,
91
- verbose=verbose,
92
- seed=seed,
93
- **kwargs
96
+ colsample=colsample,
97
+ verbose=verbose,
98
+ seed=seed,
99
+ **kwargs,
94
100
  )
95
101
 
96
- if model_type == 'xgboost':
102
+ if model_type == "xgboost":
97
103
  self.model = XGBRegressor(**self.params)
98
- elif model_type == 'catboost':
104
+ elif model_type == "catboost":
99
105
  self.model = CatBoostRegressor(**self.params)
100
- elif model_type == 'lightgbm':
106
+ elif model_type == "lightgbm":
101
107
  self.model = LGBMRegressor(**self.params)
102
108
  else:
103
109
  raise ValueError(f"Unknown model_type: {model_type}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unifiedbooster
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Unified interface for Gradient Boosted Decision Trees
5
5
  Home-page: https://github.com/thierrymoudiki/unifiedbooster
6
6
  Author: T. Moudiki
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.6
16
16
  Classifier: Programming Language :: Python :: 3.7
17
17
  Classifier: Programming Language :: Python :: 3.8
18
18
  Requires-Python: >=3.6
19
+ License-File: LICENSE
19
20
  Requires-Dist: Cython
20
21
  Requires-Dist: numpy
21
22
  Requires-Dist: scikit-learn
@@ -1,3 +1,4 @@
1
+ LICENSE
1
2
  README.md
2
3
  setup.py
3
4
  unifiedbooster/__init__.py
@@ -1 +0,0 @@
1
- # unifiedbooster
@@ -1,69 +0,0 @@
1
- import numpy as np
2
- from sklearn.base import BaseEstimator
3
-
4
-
5
- class GBDT(BaseEstimator):
6
- def __init__(self,
7
- model_type='xgboost',
8
- n_estimators=100,
9
- learning_rate=0.1,
10
- max_depth=3,
11
- rowsample=1.0,
12
- colsample=1.0,
13
- verbose=0,
14
- seed=123,
15
- **kwargs):
16
-
17
- self.model_type = model_type
18
- self.n_estimators = n_estimators
19
- self.learning_rate = learning_rate
20
- self.max_depth = max_depth
21
- self.rowsample = rowsample
22
- self.colsample = colsample
23
- self.verbose = verbose
24
- self.seed = seed
25
-
26
- if self.model_type == "xgboost":
27
- self.params = {
28
- 'n_estimators': self.n_estimators,
29
- 'learning_rate': self.learning_rate,
30
- 'subsample': self.rowsample,
31
- 'colsample_bynode': self.colsample,
32
- 'max_depth': self.max_depth,
33
- 'verbosity': self.verbose,
34
- 'seed': self.seed,
35
- **kwargs
36
- }
37
- elif self.model_type == "lightgbm":
38
- verbose = self.verbose - 1 if self.verbose==0 else self.verbose
39
- self.params = {
40
- 'n_estimators': self.n_estimators,
41
- 'learning_rate': self.learning_rate,
42
- 'subsample': self.rowsample,
43
- 'feature_fraction_bynode': self.colsample,
44
- 'max_depth': self.max_depth,
45
- 'verbose': verbose, # keep this way
46
- 'seed': self.seed,
47
- **kwargs
48
- }
49
- elif self.model_type == "catboost":
50
- self.params = {
51
- 'iterations': self.n_estimators,
52
- 'learning_rate': self.learning_rate,
53
- 'subsample': self.rowsample,
54
- 'rsm': self.colsample,
55
- 'depth': self.max_depth,
56
- 'verbose': self.verbose,
57
- 'random_seed': self.seed,
58
- 'bootstrap_type': 'Bernoulli',
59
- **kwargs
60
- }
61
-
62
- def fit(self, X, y, **kwargs):
63
- if getattr(self, "type_fit") == "classification":
64
- self.classes_ = np.unique(y) # for compatibility with sklearn
65
- self.n_classes_ = len(self.classes_) # for compatibility with sklearn
66
- return getattr(self, "model").fit(X, y, **kwargs)
67
-
68
- def predict(self, X):
69
- return getattr(self, "model").predict(X)
File without changes