5
5
from xgboost import XGBRegressor
6
6
7
7
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
+
8
23
def main () -> None :
9
24
10
25
"""
@@ -15,25 +30,21 @@ def main() -> None:
15
30
# Load Boston house price dataset
16
31
boston = load_boston ()
17
32
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 )
22
35
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
24
37
)
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 )
32
39
33
40
# Error printing
34
41
print (f"Mean Absolute Error:\t { mean_absolute_error (y_test , predictions )} " )
35
42
print (f"Mean Square Error :\t { mean_squared_error (y_test , predictions )} " )
36
43
37
44
38
45
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 )
39
50
main ()
0 commit comments