Пример #1
0
    x_test /= 255.

    # getting model predictions
    test_predictions = model.predict(x_test)

    labels_ohe_names = pd.get_dummies(target_labels, sparse=True)
    predictions = pd.DataFrame(test_predictions,
                               columns=labels_ohe_names.columns)
    predictions = list(predictions.idxmax(axis=1))
    test_labels = list(y_test)

    #evaluate model performance
    meu.get_metrics(true_labels=test_labels, predicted_labels=predictions)

    meu.display_classification_report(true_labels=test_labels,
                                      predicted_labels=predictions,
                                      classes=list(labels_ohe_names.columns))

    # print(meu.display_confusion_matrix_pretty(true_labels=test_labels,
    #                                 predicted_labels=predictions,
    #                                 classes=list(labels_ohe_names.columns)))

    font = {'family': 'Times New Roman', 'size': 12}
    matplotlib.rc('font', **font)
    mat = confusion_matrix(test_labels, predictions)
    plot_confusion_matrix(conf_mat=mat,
                          figsize=(4, 4),
                          class_names=list(labels_ohe_names.columns),
                          show_normed=False)

    grid_width = 5
# test for a given comment's best features 
eli5.show_prediction(svm_best[1], test_corpus[8], vec=svm_best[0], top=10)

# ### model performance evaluation with Linear SVM

# +
import model_evaluation_utils as meu

svm_predictions = gs_svm.predict(test_corpus)
unique_classes = list(set(test_label_nums))
meu.get_metrics(true_labels=test_label_nums, predicted_labels=svm_predictions)
# -

meu.display_classification_report(true_labels=test_label_nums,
                                  predicted_labels=svm_predictions,
                                  classes=unique_classes)

from confusion_matrices import confusion_matrices

confusion_matrices(test_label_nums, svm_predictions)

# ### checking mismatched values

# Extract test document row numbers
train_idx, test_idx = train_test_split(np.array(range(len(data_df['review']))), test_size=0.33, random_state=42)
test_idx

svm_predictions = gs_svm.predict(test_corpus)
test_df = data_df.iloc[test_idx]
test_df['predicted label'] = svm_predictions