Created
December 18, 2017 15:08
-
-
Save codez266/2cb9eeebe50b55f68e6a875bb447042d to your computer and use it in GitHub Desktop.
function to score a bunch of instances together for multilabel revscoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score_many(self, feature_values): | |
# Re-vectorize features -- this expands/flattens sub-FeatureVectors | |
fv_vectors = [vectorize_values(fv) for fv in feature_values] | |
# Scale and transform (if applicable) | |
scaled_fv_vectors = self.fit_scaler_and_transform(fv_vectors) | |
predictions = self.estimator.predict(scaled_fv_vectors) | |
#probas = self.estimator.predict_proba(scaled_fv_vectors) | |
probabilities = [] | |
docs = [] | |
if self.multilabel: | |
#probas = self.estimator.predict_proba(scaled_fv_vectors) | |
prob_matrix = np.empty((0, len(feature_values))) | |
probas = self.estimator.predict_proba(scaled_fv_vectors) | |
for label_prob in probas: | |
curr_label_prob = [] | |
for prob in label_prob: | |
curr_label_prob.append(prob[1]) | |
prob_matrix = np.append(prob_matrix, [curr_label_prob], axis=0) | |
# This converts probability matrix to [n_samples, n_labels] for ease | |
# of iteration | |
prob_matrix = np.transpose(prob_matrix) | |
for pr in prob_matrix: | |
probabilities.append({label: proba | |
for label, proba in zip(self.labels, pr)}) | |
else: | |
labels = self.estimator.classes_ | |
probas = self.estimator.predict_proba(scaled_fv_vectors) | |
for prob in probas: | |
probabilities.append({label: prob | |
for label, prob in zip(labels, prob)}) | |
for pred, prob in zip(predictions, probabilities): | |
preds = self.label_normalizer.denormalize(pred) | |
doc = {'prediction': preds, 'probability': prob} | |
docs.append(util.normalize_json(doc)) | |
return docs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment