Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save saimadhu-polamuri/9904f13ef627d0e236baae7fb70ce3d6 to your computer and use it in GitHub Desktop.

Select an option

Save saimadhu-polamuri/9904f13ef627d0e236baae7fb70ce3d6 to your computer and use it in GitHub Desktop.
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)
@Jason2Brownlee

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment