Skip to content

Commit 7446e69

Browse files
abdoulayegkpoyea
andauthored
Gradient Boosting Regressor (#2298)
* Stock market prediction using greadient boosting * To reverse a string using stack * To reverse string using stack * Predict Stock Prices Python & Machine Learning * Gradient boosting regressor on boston dataset * Gradient boosting regressor implementation * Gradient boosting regressor * Gradient boosting regressor * Gradient boosting regressor * Removing files * GradientBoostingRegressor example * Demo Gradient Boosting * Demo Gradient boosting * demo of gradient boosting * gradient boosting demo * Fix spelling mistake * Fix formatting Co-authored-by: John Law <[email protected]>
1 parent 7f48bb8 commit 7446e69

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Implementation of GradientBoostingRegressor in sklearn using the
2+
boston dataset which is very popular for regression problem to
3+
predict house price.
4+
"""
5+
6+
import pandas as pd
7+
import matplotlib.pyplot as plt
8+
from sklearn.datasets import load_boston
9+
from sklearn.metrics import mean_squared_error, r2_score
10+
from sklearn.ensemble import GradientBoostingRegressor
11+
from sklearn.model_selection import train_test_split
12+
13+
14+
def main():
15+
16+
# loading the dataset from the sklearn
17+
df = load_boston()
18+
print(df.keys())
19+
# now let construct a data frame
20+
df_boston = pd.DataFrame(df.data, columns=df.feature_names)
21+
# let add the target to the dataframe
22+
df_boston["Price"] = df.target
23+
# print the first five rows using the head function
24+
print(df_boston.head())
25+
# Summary statistics
26+
print(df_boston.describe().T)
27+
# Feature selection
28+
29+
X = df_boston.iloc[:, :-1]
30+
y = df_boston.iloc[:, -1] # target variable
31+
# split the data with 75% train and 25% test sets.
32+
X_train, X_test, y_train, y_test = train_test_split(
33+
X, y, random_state=0, test_size=0.25
34+
)
35+
36+
model = GradientBoostingRegressor(
37+
n_estimators=500, max_depth=5, min_samples_split=4, learning_rate=0.01
38+
)
39+
# training the model
40+
model.fit(X_train, y_train)
41+
# to see how good the model fit the data
42+
training_score = model.score(X_train, y_train).round(3)
43+
test_score = model.score(X_test, y_test).round(3)
44+
print("Training score of GradientBoosting is :", training_score)
45+
print(
46+
"The test score of GradientBoosting is :",
47+
test_score
48+
)
49+
# Let us evaluation the model by finding the errors
50+
y_pred = model.predict(X_test)
51+
52+
# The mean squared error
53+
print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
54+
# Explained variance score: 1 is perfect prediction
55+
print("Test Variance score: %.2f" % r2_score(y_test, y_pred))
56+
57+
# So let's run the model against the test data
58+
fig, ax = plt.subplots()
59+
ax.scatter(y_test, y_pred, edgecolors=(0, 0, 0))
60+
ax.plot([y_test.min(), y_test.max()],
61+
[y_test.min(), y_test.max()], "k--", lw=4)
62+
ax.set_xlabel("Actual")
63+
ax.set_ylabel("Predicted")
64+
ax.set_title("Truth vs Predicted")
65+
# this show function will display the plotting
66+
plt.show()
67+
68+
69+
if __name__ == "__main__":
70+
main()

0 commit comments

Comments
 (0)