Skip to content

Commit b092f99

Browse files
Moddy2024pre-commit-ci[bot]cclauss
authored
XGB Regressor (TheAlgorithms#7107)
* Fixes: #{6551} * Fixes: #{6551} * Update xgboostclassifier.py * Delete xgboostclassifier.py * Update xgboostregressor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixes: #{6551} * Fixes : {TheAlgorithms#6551} * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixes: {TheAlgorithms#6551] * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xgboostregressor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xgboostregressor.py * Update xgboostregressor.py * Fixes: { TheAlgorithms#6551} * Update xgboostregressor.py * Fixes: { TheAlgorithms#6551} * Fixes: { TheAlgorithms#6551} * Update and rename xgboostregressor.py to xgboost_regressor.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 1bbb009 commit b092f99

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Diff for: machine_learning/xgboost_regressor.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# XGBoost Regressor Example
2+
import numpy as np
3+
from sklearn.datasets import fetch_california_housing
4+
from sklearn.metrics import mean_absolute_error, mean_squared_error
5+
from sklearn.model_selection import train_test_split
6+
from xgboost import XGBRegressor
7+
8+
9+
def data_handling(data: dict) -> tuple:
10+
# Split dataset into features and target. Data is features.
11+
"""
12+
>>> data_handling((
13+
... {'data':'[ 8.3252 41. 6.9841269 1.02380952 322. 2.55555556 37.88 -122.23 ]'
14+
... ,'target':([4.526])}))
15+
('[ 8.3252 41. 6.9841269 1.02380952 322. 2.55555556 37.88 -122.23 ]', [4.526])
16+
"""
17+
return (data["data"], data["target"])
18+
19+
20+
def xgboost(
21+
features: np.ndarray, target: np.ndarray, test_features: np.ndarray
22+
) -> np.ndarray:
23+
"""
24+
>>> xgboost(np.array([[ 2.3571 , 52. , 6.00813008, 1.06775068,
25+
... 907. , 2.45799458, 40.58 , -124.26]]),np.array([1.114]),
26+
... np.array([[1.97840000e+00, 3.70000000e+01, 4.98858447e+00, 1.03881279e+00,
27+
... 1.14300000e+03, 2.60958904e+00, 3.67800000e+01, -1.19780000e+02]]))
28+
array([[1.1139996]], dtype=float32)
29+
"""
30+
xgb = XGBRegressor(verbosity=0, random_state=42)
31+
xgb.fit(features, target)
32+
# Predict target for test data
33+
predictions = xgb.predict(test_features)
34+
predictions = predictions.reshape(len(predictions), 1)
35+
return predictions
36+
37+
38+
def main() -> None:
39+
"""
40+
>>> main()
41+
Mean Absolute Error : 0.30957163379906033
42+
Mean Square Error : 0.22611560196662744
43+
44+
The URL for this algorithm
45+
https://xgboost.readthedocs.io/en/stable/
46+
California house price dataset is used to demonstrate the algorithm.
47+
"""
48+
# Load California house price dataset
49+
california = fetch_california_housing()
50+
data, target = data_handling(california)
51+
x_train, x_test, y_train, y_test = train_test_split(
52+
data, target, test_size=0.25, random_state=1
53+
)
54+
predictions = xgboost(x_train, y_train, x_test)
55+
# Error printing
56+
print(f"Mean Absolute Error : {mean_absolute_error(y_test, predictions)}")
57+
print(f"Mean Square Error : {mean_squared_error(y_test, predictions)}")
58+
59+
60+
if __name__ == "__main__":
61+
import doctest
62+
63+
doctest.testmod(verbose=True)
64+
main()

0 commit comments

Comments
 (0)