Skip to content

Instantly share code, notes, and snippets.

@okanyenigun
Created August 11, 2022 13:19
Show Gist options
  • Save okanyenigun/797c2080e200f5ffb33be43359a77f65 to your computer and use it in GitHub Desktop.
Save okanyenigun/797c2080e200f5ffb33be43359a77f65 to your computer and use it in GitHub Desktop.
xgboost
import xgboost as xgb
import numpy as np
from sklearn.datasets import make_regression, make_gaussian_quantiles
from sklearn.metrics import mean_squared_error, confusion_matrix
from sklearn.model_selection import train_test_split
#REGRESSION
#generate regression data
X, y, _ = make_regression(n_samples=10000,#number of samples
n_features=10,#number of features
n_informative=7,#number of useful features
noise=70,#bias and standard deviation of the guassian noise
coef=True,#true coefficient used to generated the data
random_state=0) #set for same data points for each run
#train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
#train & prediction
model = xgb.XGBRegressor(n_estimators=1000, max_depth=7, eta=0.1)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
#CLASSIFICATION
#generate classification data
X, y = make_gaussian_quantiles(cov=3.,
n_samples=10000, n_features=2,
n_classes=2, random_state=1)
#train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
#train & prediction
model = xgb.XGBClassifier(objective='binary:logistic')
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment