Пример #1
0
with open(path, 'rb') as file:
    unbalanced_model = pickle.load(file)

# unpack balanced model
path = '1) classification algorithms/random forest/credit card fraud/model_forest_balanced_ori.pkl'
with open(path, 'rb') as file:
    balanced_model = pickle.load(file)

# predict labels
unbalanced_predictions = unbalanced_model.predict(X_test_unbalanced)
unbalanced_predictions = [int(round(x)) for x in unbalanced_predictions]
balanced_predictions = balanced_model.predict(X_test_balanced)
balanced_predictions = [int(round(x)) for x in balanced_predictions]

# print the confusion matrix, precision, recall, etc.
get_model_performance(unbalanced_model, 'unbalanced', X_test_unbalanced,
                      y_test_unbalanced, 'RF', 'fraud dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/credit card fraud/figures/PRcurve_rf_unbalanced_fraud.png'
)
plt.close()
get_model_performance(balanced_model, 'balanced', X_test_balanced,
                      y_test_balanced, 'RF', 'fraud dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/credit card fraud/figures/PRcurve_rf_balanced_fraud.png'
)
plt.close()

cm_analysis(
    y_test_balanced,
    balanced_predictions,
    filename=
Пример #2
0
# unpack unbalanced model
path = '1) classification algorithms/SVM/credit card fraud/model_SVM_balanced.pkl'
with open(path, 'rb') as file:
    unbalanced_model = pickle.load(file)

# unpack balanced model
path = '1) classification algorithms/SVM/credit card fraud/model_SVM_balanced.pkl'
with open(path, 'rb') as file:
    balanced_model = pickle.load(file)

# predict labels
unbalanced_predictions = unbalanced_model.predict(X_test_unbalanced)
balanced_predictions = balanced_model.predict(X_test_balanced)

# print the confusion matrix, precision, recall, etc.
get_model_performance(unbalanced_model, 'unbalanced', X_test_unbalanced,
                      y_test_unbalanced)
get_model_performance(balanced_model, 'balanced', X_test_balanced,
                      y_test_balanced)

# plot confusion matrix graph
plot_confusion_matrix(y_test_balanced,
                      balanced_predictions,
                      classes=['normal', 'fraud'],
                      normalize=True,
                      title='Confusion matrix balanced')
plt.show()

plot_confusion_matrix(y_test_unbalanced,
                      unbalanced_predictions,
                      classes=['normal', 'fraud'],
                      normalize=True,
Пример #3
0
with open(path, 'rb') as file:
    unbalanced_model = pickle.load(file)

# unpack balanced model
path = '1) classification algorithms/random forest/bioresponse/model_forest_balanced_bio.pkl'
with open(path, 'rb') as file:
    balanced_model = pickle.load(file)

# predict labels
unbalanced_predictions = unbalanced_model.predict(X_test_unbalanced)
unbalanced_predictions = [int(round(x)) for x in unbalanced_predictions]
balanced_predictions = balanced_model.predict(X_test_balanced)
balanced_predictions = [int(round(x)) for x in balanced_predictions]

# print the confusion matrix, precision, recall, etc.
get_model_performance(unbalanced_model, 'unbalanced', X_test_unbalanced,
                      y_test_unbalanced, 'RF', 'bioresponse dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/bioresponse/figures/PRcurve_rf_unbalanced_bio.png'
)
plt.close()
get_model_performance(balanced_model, 'balanced', X_test_balanced,
                      y_test_balanced, 'RF', 'bioresponse dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/bioresponse/figures/PRcurve_rf_balanced_bio.png'
)
plt.close()

cm_analysis(
    y_test_balanced,
    balanced_predictions,
    filename=
path2 = '1) classification algorithms/neural networks/customer churn/model_NN_balanced_churn.pkl'
# with open(path, 'rb') as file:
#     balanced_model = pickle.load(file)
balanced_model = load_model(path2)

# predict labels
unbalanced_predictions = unbalanced_model.predict(X_test_unbalanced)
unbalanced_predictions = unbalanced_predictions[:, 1]
unbalanced_predictions = [int(round(x)) for x in unbalanced_predictions]

balanced_predictions = balanced_model.predict(X_test_balanced)
balanced_predictions = balanced_predictions[:, 1]
balanced_predictions = [int(round(x)) for x in balanced_predictions]

# print the confusion matrix, precision, recall, etc.
get_model_performance(unbalanced_model, 'unbalanced', X_test_unbalanced,
                      y_test_unbalanced, 'NN', 'churn dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/customer churn/figures/PRcurve_nn_unbalanced_churn.png'
)
plt.close()
get_model_performance(balanced_model, 'balanced', X_test_balanced,
                      y_test_balanced, 'NN', 'churn dataset')
plt.savefig(
    '1) classification algorithms/assess model performance/customer churn/figures/PRcurve_nn_balanced_churn.png'
)
plt.close()

cm_analysis(
    y_test_balanced,
    balanced_predictions,
    filename=