Created
October 7, 2023 15:59
-
-
Save saimadhu-polamuri/9904f13ef627d0e236baae7fb70ce3d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import xgboost as xgb | |
| from sklearn.datasets import load_boston | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import mean_squared_error | |
| # load the Boston housing dataset | |
| boston = load_boston() | |
| X, y = boston.data, boston.target | |
| # split the data into training and testing sets | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # define the model parameters | |
| params = { | |
| 'objective': 'reg:squarederror', | |
| 'learning_rate': 0.1, | |
| 'max_depth': 3, | |
| 'n_estimators': 100, | |
| 'subsample': 0.8, | |
| 'colsample_bytree': 0.8, | |
| 'reg_alpha': 0.1, | |
| 'reg_lambda': 0.1 | |
| } | |
| # train the model | |
| model = xgb.XGBRegressor(**params) | |
| model.fit(X_train, y_train) | |
| # make predictions on the testing set | |
| y_pred = model.predict(X_test) | |
| # calculate the mean squared error | |
| mse = mean_squared_error(y_test, y_pred) | |
| print("Mean squared error: %.2f" % mse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example of xgboost on the boston housing dataset!