本文整理汇总了Python中lightgbm.Booster方法的典型用法代码示例。如果您正苦于以下问题:Python lightgbm.Booster方法的具体用法?Python lightgbm.Booster怎么用?Python lightgbm.Booster使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lightgbm
的用法示例。
在下文中一共展示了lightgbm.Booster方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train_and_predict
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def train_and_predict(self, train, valid, weight, categorical_features: List[str], target: str, params: dict) \
-> Tuple[Booster, dict]:
if type(train) != pd.DataFrame or type(valid) != pd.DataFrame:
raise ValueError('Parameter train and valid must be pandas.DataFrame')
if list(train.columns) != list(valid.columns):
raise ValueError('Train and valid must have a same column list')
predictors = train.columns.drop(target)
if weight is None:
d_train = lgb.Dataset(train[predictors], label=train[target].values)
else:
print(weight)
d_train = lgb.Dataset(train[predictors], label=train[target].values, weight=weight)
d_valid = lgb.Dataset(valid[predictors], label=valid[target].values)
eval_results = {}
model: Booster = lgb.train(params['model_params'],
d_train,
categorical_feature=categorical_features,
valid_sets=[d_train, d_valid],
valid_names=['train', 'valid'],
evals_result=eval_results,
**params['train_params'])
return model, eval_results
示例2: __init__
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def __init__(self, spec, model):
super(_LightGBMModelArtifactWrapper, self).__init__(spec)
try:
import lightgbm as lgb
except ImportError:
raise MissingDependencyException(
"lightgbm package is required to use LightGBMModelArtifact"
)
if not isinstance(model, lgb.Booster):
raise InvalidArgument(
"Expect `model` argument to be a `lightgbm.Booster` instance"
)
self._model = model
示例3: load_model
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def load_model(model_uri):
"""
Load a LightGBM model from a local file or a run.
:param model_uri: The location, in URI format, of the MLflow model. For example:
- ``/Users/me/path/to/local/model``
- ``relative/path/to/local/model``
- ``s3://my_bucket/path/to/model``
- ``runs:/<mlflow_run_id>/run-relative/path/to/model``
For more information about supported URI schemes, see
`Referencing Artifacts <https://www.mlflow.org/docs/latest/tracking.html#
artifact-locations>`_.
:return: A LightGBM model (an instance of `lightgbm.Booster`_).
"""
local_model_path = _download_artifact_from_uri(artifact_uri=model_uri)
flavor_conf = _get_flavor_configuration(model_path=local_model_path, flavor_name=FLAVOR_NAME)
lgb_model_file_path = os.path.join(local_model_path, flavor_conf.get("data", "model.lgb"))
return _load_model(path=lgb_model_file_path)
示例4: _get_booster_best_score
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def _get_booster_best_score(self, booster: "lgb.Booster") -> float:
metric = self._get_metric_for_objective()
valid_sets = self.lgbm_kwargs.get("valid_sets") # type: Optional[VALID_SET_TYPE]
if self.lgbm_kwargs.get("valid_names") is not None:
if type(self.lgbm_kwargs["valid_names"]) is str:
valid_name = self.lgbm_kwargs["valid_names"]
elif type(self.lgbm_kwargs["valid_names"]) in [list, tuple]:
valid_name = self.lgbm_kwargs["valid_names"][-1]
else:
raise NotImplementedError
elif type(valid_sets) is lgb.Dataset:
valid_name = "valid_0"
elif isinstance(valid_sets, (list, tuple)) and len(valid_sets) > 0:
valid_set_idx = len(valid_sets) - 1
valid_name = "valid_{}".format(valid_set_idx)
else:
raise NotImplementedError
val_score = booster.best_score[valid_name][metric]
return val_score
示例5: __init__
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def __init__(self):
scalingModelData = json.loads(pkg_resources.resource_string(__name__, "../atpe_models/scaling_model.json"))
self.featureScalingModels = {}
for key in self.atpeModelFeatureKeys:
self.featureScalingModels[key] = sklearn.preprocessing.StandardScaler()
self.featureScalingModels[key].scale_ = numpy.array(scalingModelData[key]['scales'])
self.featureScalingModels[key].mean_ = numpy.array(scalingModelData[key]['means'])
self.featureScalingModels[key].var_ = numpy.array(scalingModelData[key]['variances'])
self.parameterModels = {}
self.parameterModelConfigurations = {}
for param in self.atpeParameters:
modelData = pkg_resources.resource_string(__name__, "../atpe_models/model-" + param + '.txt')
with hypermax.file_utils.ClosedNamedTempFile(modelData) as model_file_name:
self.parameterModels[param] = lightgbm.Booster(model_file=model_file_name)
configString = pkg_resources.resource_string(__name__, "../atpe_models/model-" + param + '-configuration.json')
data = json.loads(configString)
self.parameterModelConfigurations[param] = data
self.lastATPEParameters = None
self.lastLockedParameters = []
self.atpeParamDetails = None
示例6: image_predict
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def image_predict(self, X, **kwargs):
"""
Predicts class labels for the entire image.
:param X: Array of images to be classified.
:type X: numpy array, shape = [n_images, n_pixels_y, n_pixels_x, n_bands]
:param kwargs: Any keyword arguments that will be passed to the classifier's prediction method
:return: raster classification map
:rtype: numpy array, [n_samples, n_pixels_y, n_pixels_x]
"""
pixels = self.extract_pixels(X)
if isinstance(self.classifier, Booster):
raise NotImplementedError('An instance of lightgbm.Booster can only return prediction probabilities, '
'use PixelClassifier.image_predict_proba instead')
predictions = self.classifier.predict(pixels, **kwargs)
return predictions.reshape(X.shape[0], X.shape[1], X.shape[2])
示例7: image_predict_proba
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def image_predict_proba(self, X, **kwargs):
"""
Predicts class probabilities for the entire image.
:param X: Array of images to be classified.
:type X: numpy array, shape = [n_images, n_pixels_y, n_pixels_x, n_bands]
:param kwargs: Any keyword arguments that will be passed to the classifier's prediction method
:return: classification probability map
:rtype: numpy array, [n_samples, n_pixels_y, n_pixels_x]
"""
pixels = self.extract_pixels(X)
if isinstance(self.classifier, Booster):
probabilities = self.classifier.predict(pixels, **kwargs)
probabilities = np.vstack((1. - probabilities, probabilities)).transpose()
else:
probabilities = self.classifier.predict_proba(pixels, **kwargs)
return probabilities.reshape(X.shape[0], X.shape[1], X.shape[2], probabilities.shape[1])
示例8: check
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def check(conf=DEFAULTCONF):
if not conf['ENABLED']:
return False
if not has_ember:
return False
if not Path(conf['path-to-model']).is_file():
print("'{}' does not exist. Check config.ini for model location.".format(conf['path-to-model']))
return False
try:
global LGBM_MODEL
LGBM_MODEL = lgb.Booster(model_file=conf['path-to-model'])
except lgb.LightGBMError as e:
print("Unable to load model, {}. ({})".format(conf['path-to-model'], e))
return False
return True
示例9: load
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def load(self, model_file_path):
logger.debug("LightgbmAlgorithm load model from %s" % model_file_path)
self.model = lgb.Booster(model_file=model_file_path)
示例10: __init__
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def __init__(self, model_path=EMBER_MODEL_PATH, thresh=0.8336, name='ember'):
# load lightgbm model
self.model = lgb.Booster(model_file=model_path)
self.thresh = thresh
self.__name__ = 'ember'
示例11: dump
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def dump(self, model: lgb.Booster) -> FilesContextManager:
with tempfile.TemporaryDirectory(prefix='ebonite_lightgbm_dump') as f:
path = os.path.join(f, self.model_path)
model.save_model(path)
yield Blobs({self.model_path: LocalFileBlob(path)})
示例12: load
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def load(self, path):
model_file = os.path.join(path, self.model_path)
return lgb.Booster(model_file=model_file)
示例13: load
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def load(self, path):
try:
import lightgbm as lgb
except ImportError:
raise MissingDependencyException(
"lightgbm package is required to use LightGBMModelArtifact"
)
bst = lgb.Booster(model_file=self._model_file_path(path))
return self.pack(bst)
示例14: load_model
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def load_model(path,name):
root = jhkaggle.jhkaggle_config['PATH']
model_path = os.path.join(root,path)
meta_filename = os.path.join(model_path,"meta.json")
with open(meta_filename, 'r') as fp:
meta = json.load(fp)
result = TrainLightGBM(meta['data_source'],meta['params'],False)
result.model = lgb.Booster(model_file=os.path.join(model_path,name+".txt"))
return result
示例15: _load_model
# 需要导入模块: import lightgbm [as 别名]
# 或者: from lightgbm import Booster [as 别名]
def _load_model(path):
import lightgbm as lgb
return lgb.Booster(model_file=path)