Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (labels is None or
len(labels) * models_per_label != len(predictions)):
sys.exit("Failed to make a multi-label prediction. No"
" valid label info is found.")
prediction_list = []
confidence_list = []
# In the following case, we must vote each label using the models
# in the ensemble and the chosen method
if models_per_label > 1:
label_predictions = [predictions[i: i + models_per_label] for
i in range(0, len(predictions),
models_per_label)]
predictions = []
for label_prediction in label_predictions:
label_multivote = MultiVote(label_prediction)
prediction_info = label_multivote.combine(
method=AGGREGATION, full=True, options=options)
predictions.append({'prediction': prediction_info["prediction"],
'confidence': prediction_info["confidence"]})
for vote_index, vote_prediction in enumerate(predictions):
if ast.literal_eval(vote_prediction['prediction']):
prediction_list.append(labels[vote_index])
confidence = str(vote_prediction['confidence'])
confidence_list.append(confidence)
prediction = [label_separator.join(prediction_list),
label_separator.join(confidence_list)]
return prediction
votes_split = multi_model._generate_votes(
input_data,
missing_strategy=missing_strategy,
unused_fields=unused_fields)
if median:
for prediction in votes_split.predictions:
prediction['prediction'] = prediction['median']
votes.extend(votes_split.predictions)
else:
# When only one group of models is found you use the
# corresponding multimodel to predict
votes_split = self.multi_model._generate_votes(
input_data, missing_strategy=missing_strategy,
unused_fields=unused_fields)
votes = MultiVote(votes_split.predictions,
boosting_offsets=self.boosting_offsets)
if median:
for prediction in votes.predictions:
prediction['prediction'] = prediction['median']
if self.boosting is not None and not self.regression:
categories = [ \
d[0] for d in
self.fields[self.objective_id]["summary"]["categories"]]
options = {"categories": categories}
result = votes.combine(method=method, options=options, full=full)
if full:
unused_fields = set(input_data.keys())
for prediction in votes.predictions:
unused_fields = unused_fields.intersection( \
set(prediction.get("unused_fields", [])))
if not isinstance(result, dict):
def generate_votes(self, input_data,
missing_strategy=LAST_PREDICTION):
""" Generates a MultiVote object that contains the predictions
made by each of the models.
"""
votes = MultiVote([])
for order in range(0, len(self.models)):
model = self.models[order]
prediction_info = model.predict( \
input_data, missing_strategy=missing_strategy, full=True)
if model.boosting is not None:
votes.boosting = True
prediction_info.update( \
{"weight": model.boosting.get("weight")})
if model.boosting.get("objective_class") is not None:
prediction_info.update( \
{"class": model.boosting.get("objective_class")})
votes.append(prediction_info)
return votes
if options["threshold"] > length:
raise Exception("You cannot set a threshold value larger than "
"%s. The ensemble has not enough models to use"
" this threshold value." % length)
if options["threshold"] < 1:
raise Exception("The threshold must be a positive value")
category_predictions = []
rest_of_predictions = []
for prediction in self.predictions:
if prediction['prediction'] == options["category"]:
category_predictions.append(prediction)
else:
rest_of_predictions.append(prediction)
if len(category_predictions) >= options["threshold"]:
return MultiVote(category_predictions)
return MultiVote(rest_of_predictions)
method = 1 if operating_kind == "confidence" else 0
return self.predict( \
input_data, method=method,
options=options, missing_strategy=missing_strategy,
operating_point=None, operating_kind=None, full=full)
else:
prediction = self.predict_operating_kind( \
input_data,
missing_strategy=missing_strategy,
operating_kind=operating_kind)
return prediction
if len(self.models_splits) > 1:
# If there's more than one chunk of models, they must be
# sequentially used to generate the votes for the prediction
votes = MultiVote([], boosting_offsets=self.boosting_offsets)
for models_split in self.models_splits:
models = self._get_models(models_split)
multi_model = MultiModel(models,
api=self.api,
fields=self.fields)
votes_split = multi_model._generate_votes(
input_data,
missing_strategy=missing_strategy,
unused_fields=unused_fields)
if median:
for prediction in votes_split.predictions:
prediction['prediction'] = prediction['median']
votes.extend(votes_split.predictions)
else:
length = len(self.predictions)
if options["threshold"] > length:
raise Exception("You cannot set a threshold value larger than "
"%s. The ensemble has not enough models to use"
" this threshold value." % length)
if options["threshold"] < 1:
raise Exception("The threshold must be a positive value")
category_predictions = []
rest_of_predictions = []
for prediction in self.predictions:
if prediction['prediction'] == options["category"]:
category_predictions.append(prediction)
else:
rest_of_predictions.append(prediction)
if len(category_predictions) >= options["threshold"]:
return MultiVote(category_predictions)
return MultiVote(rest_of_predictions)
2 - probability weighted majority vote / average:
PROBABILITY_CODE
"""
# When only one group of models is found you use the
# corresponding multimodel to predict
votes_split = []
options = None
count = 1
for fun in self.predict_functions:
prediction = fun(input_data)
prediction.update({"order": count, "count": 1})
count += 1
votes_split.append(prediction)
votes = MultiVote(votes_split,
boosting_offsets=self.boosting_offsets)
if self.boosting is not None and not self.regression:
categories = [ \
d[0] for d in
self.fields[self.objective_id]["summary"]["categories"]]
options = {"categories": categories}
result = votes.combine(method=method, options=options, full=full)
if isinstance(result, dict):
del result['count']
return result