Skip to content

Commit bc62633

Browse files
authored
1 parent 7ff3c6a commit bc62633

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

machine_learning/xgboostregressor.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
from xgboost import XGBRegressor
66

77

8+
def dataset(datatype:dict) -> tuple:
9+
# Split dataset into train and test data
10+
x = (datatype["data"],datatype["target"]) # features
11+
return x
12+
13+
14+
def xgboost(features:list, target:list,test_features:list) -> list:
15+
xgb = XGBRegressor()
16+
xgb.fit(features, target)
17+
# Predict target for test data
18+
predictions = xgb.predict(test_features)
19+
predictions = predictions.reshape(len(predictions), 1)
20+
return predictions
21+
22+
823
def main() -> None:
924

1025
"""
@@ -15,25 +30,21 @@ def main() -> None:
1530
# Load Boston house price dataset
1631
boston = load_boston()
1732
print(boston.keys())
18-
19-
# Split dataset into train and test data
20-
x = boston["data"] # features
21-
y = boston["target"]
33+
34+
features, target = dataset(boston)
2235
x_train, x_test, y_train, y_test = train_test_split(
23-
x, y, test_size=0.25, random_state=1
36+
features, target, test_size=0.25, random_state=1
2437
)
25-
# XGBoost Regressor
26-
xgb = XGBRegressor()
27-
xgb.fit(x_train, y_train)
28-
29-
# Predict target for test data
30-
predictions = xgb.predict(x_test)
31-
predictions = predictions.reshape(len(predictions), 1)
38+
predictions = xgboost(x_train, y_train, x_test)
3239

3340
# Error printing
3441
print(f"Mean Absolute Error:\t {mean_absolute_error(y_test, predictions)}")
3542
print(f"Mean Square Error :\t {mean_squared_error(y_test, predictions)}")
3643

3744

3845
if __name__ == "__main__":
46+
import doctest
47+
doctest.testmod(name="main", verbose=True)
48+
doctest.testmod(name="dataset", verbose=True)
49+
doctest.testmod(name="xgboost", verbose=True)
3950
main()

0 commit comments

Comments
 (0)