Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
custom_input1,
custom_input2,
model,
custom_output1,
):
with train as reader:
train_df = reader.read(concat=True)
dtrain_x = xgb.DMatrix(train_df[:-1])
dtrain_y = xgb.DMatrix(train_df[-1])
with validation as reader:
validation_df = reader.read(concat=True)
dvalidation_x = xgb.DMatrix(validation_df[:-1])
dvalidation_y = xgb.DMatrix(validation_df[-1])
my_model = xgb.XGBModel(**static_hyperparameters)
my_model.fit(dtrain_x,
dtrain_y,
eval_set=[(dvalidation_x, dvalidation_y)],
eval_metric=sample_eval_function)
model.set(my_model)
custom_output1.set(my_model.evals_result())
import xgboost as xgb
import numpy as np
from sklearn.datasets import make_hastie_10_2
X, y = make_hastie_10_2(n_samples=2000, random_state=42)
# Map labels from {-1, 1} to {0, 1}
labels, y = np.unique(y, return_inverse=True)
X_train, X_test = X[:1600], X[1600:]
y_train, y_test = y[:1600], y[1600:]
param_dist = {'objective':'binary:logistic', 'n_estimators':2}
clf = xgb.XGBModel(**param_dist)
# Or you can use: clf = xgb.XGBClassifier(**param_dist)
clf.fit(X_train, y_train,
eval_set=[(X_train, y_train), (X_test, y_test)],
eval_metric='logloss',
verbose=True)
# Load evals result by calling the evals_result() function
evals_result = clf.evals_result()
print('Access logloss metric directly from validation_0:')
print(evals_result['validation_0']['logloss'])
print('')
print('Access metrics through a loop:')
for e_name, e_mtrs in evals_result.items():
from xgboost import DMatrix, train
from xgboost import XGBModel
from xgboost.sklearn import _objective_decorator
from xgboostextension.util import _preprare_data_in_groups
from sklearn.utils import check_X_y, check_array
class XGBRanker(XGBModel):
__doc__ = """Implementation of sklearn API for XGBoost Ranking
""" + '\n'.join(XGBModel.__doc__.split('\n')[2:])
def __init__(self, max_depth=3, learning_rate=0.1, n_estimators=100,
silent=True, objective="rank:pairwise", booster='gbtree',
n_jobs=-1, nthread=None, gamma=0, min_child_weight=1, max_delta_step=0,
subsample=1, colsample_bytree=1, colsample_bylevel=1,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
base_score=0.5, random_state=0, seed=None, missing=None, **kwargs):
super(XGBRanker, self).__init__(max_depth, learning_rate,
n_estimators, silent, objective, booster,
n_jobs, nthread, gamma, min_child_weight, max_delta_step,
subsample, colsample_bytree, colsample_bylevel,
reg_alpha, reg_lambda, scale_pos_weight,
base_score, random_state, seed, missing)
def train_with_xgboost(self, x_train, y_train, x_test, y_test):
xgb_model = xgb.XGBModel()
params = {
'booster': ['gblinear'],
'silent': [1],
'learning_rate': [x for x in np.round(np.linspace(0.01, 1, 20), 2)],
'reg_lambda': [lambd for lambd in np.logspace(0, 3, 50)],
'objective': ['reg:linear']
}
print('begin')
clf = GridSearchCV(xgb_model, params,
scoring='neg_mean_squared_error',
refit=True)
clf.fit(x_train, y_train)
preds = clf.predict(x_test)
print('test mse:', self.cal_MSE(preds, y_test))
best_parameters, score, _ = max(clf.grid_scores_, key=lambda x: x[1])
print('Raw RMSE:', score)
def fit(self, X, y):
self.model = XGBModel(n_estimators=self.n_best, **self.params)
self.model.fit(X=X[self.features], y=y, eval_metric='mae', verbose=False)
return self
def interpret_model(self, show=True): # pragma: no cover
"""
Displays a dashboard interpreting your model's performance, behaviour and individual predictions.
If you have run any other `interpret` functions, they will be included in the dashboard, otherwise all the other intrepretable methods will be included in the dashboard.
Examples
--------
>>> m = model.LogisticRegression()
>>> m.interpret_model()
"""
warnings.simplefilter("ignore")
if isinstance(self.model, xgb.XGBModel):
return "Using MSFT interpret is currently unsupported with XGBoost."
if show:
self.interpret.create_dashboard()
import tempfile
class Unpickler(pickle.Unpickler):
def find_class(self, module, name):
if module.startswith("xgboost"):
return pickle.Unpickler.find_class(
self, module.replace(
"xgboost", "xgboost_prev"),
name)
return pickle.Unpickler.find_class(self, module, name)
f.seek(0)
model = Unpickler(f).load()
temp_file = tempfile.NamedTemporaryFile(
prefix='xgboost_migration', suffix='.model')
model.save_model(temp_file.name)
migrated_model = xgboost.XGBModel()
migrated_model.load_model(temp_file.name)
return migrated_model
raise
def interpret_model(self, show=True): # pragma: no cover
"""
Displays a dashboard interpreting your model's performance, behaviour and individual predictions.
If you have run any other `interpret` functions, they will be included in the dashboard, otherwise all the other intrepretable methods will be included in the dashboard.
Examples
--------
>>> m = model.LogisticRegression()
>>> m.interpret_model()
"""
warnings.simplefilter("ignore")
if isinstance(self.model, xgb.XGBModel):
return "Using MSFT interpret is currently unsupported with XGBoost."
if show:
self.interpret.create_dashboard()
def interpret_model(self, show=True): # pragma: no cover
"""
Displays a dashboard interpreting your model's performance, behaviour and individual predictions.
If you have run any other `interpret` functions, they will be included in the dashboard, otherwise all the other intrepretable methods will be included in the dashboard.
Examples
--------
>>> m = model.LogisticRegression()
>>> m.interpret_model()
"""
warnings.simplefilter("ignore")
if isinstance(self.model, xgb.XGBModel):
return "Using MSFT interpret is currently unsupported with XGBoost."
if show:
self.interpret.create_dashboard()
model_kwargs : dict
Model parameters
metrics : dict
Metrics for the model
"""
mlflow.set_tracking_uri(EXP_DIR)
mlflow.set_experiment(exp_name)
with mlflow.start_run(run_name=model_name) as run:
mlflow.log_params(model_kwargs)
mlflow.set_tag("name", model_name)
if isinstance(model, xgb.XGBModel):
mlflow.xgboost.log_model(model, model_name)
else:
mlflow.sklearn.log_model(model, model_name)
mlflow.log_metrics(metrics)
mlflow.log_artifacts(os.path.join(IMAGE_DIR, model_name))
run_id = run.info.run_uuid
mlflow.end_run()
return run_id