Skip to content

Commit

Permalink
change y_prob to y_score
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawe committed Dec 22, 2023
1 parent a1caedd commit 1e7d197
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
14 changes: 7 additions & 7 deletions plotsandgraphs/binary_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,16 @@ def plot_calibration_curve(y_prob: np.ndarray, y_true: np.ndarray, n_bins=10, sa
return fig


def plot_y_prob_histogram(y_prob: np.ndarray, y_true: Optional[np.ndarray] = None, save_fig_path=None) -> Figure:
def plot_y_score_histogram(y_true: Optional[np.ndarray], y_score: np.ndarray = None, save_fig_path=None) -> Figure:
"""
Provides a histogram for the predicted probabilities of a binary classifier. If ```y_true``` is provided, it divides the ```y_prob``` values into the two classes and plots them jointly into the same plot with different colors.
Provides a histogram for the predicted probabilities of a binary classifier. If ```y_true``` is provided, it divides the ```y_score``` values into the two classes and plots them jointly into the same plot with different colors.
Parameters
----------
y_prob : np.ndarray
The output probabilities of the classifier. Between 0 and 1.
y_true : Optional[np.ndarray], optional
The true class labels, by default None
y_score : np.ndarray
The output probabilities of the classifier. Between 0 and 1.
save_fig_path : _type_, optional
Path where to save figure, by default None
Expand All @@ -471,13 +471,13 @@ def plot_y_prob_histogram(y_prob: np.ndarray, y_true: Optional[np.ndarray] = Non
ax = fig.add_subplot(111)

if y_true is None:
ax.hist(y_prob, bins=10, alpha=0.9, edgecolor="midnightblue", linewidth=2, rwidth=1)
ax.hist(y_score, bins=10, alpha=0.9, edgecolor="midnightblue", linewidth=2, rwidth=1)
# same histogram as above, but with border lines
# ax.hist(y_prob, bins=10, alpha=0.5, edgecolor='black', linewidth=1.2)
else:
alpha = 0.6
ax.hist(
y_prob[y_true == 0],
y_score[y_true == 0],
bins=10,
alpha=alpha,
edgecolor="midnightblue",
Expand All @@ -486,7 +486,7 @@ def plot_y_prob_histogram(y_prob: np.ndarray, y_true: Optional[np.ndarray] = Non
label="$\\hat{y} = 0$",
)
ax.hist(
y_prob[y_true == 1],
y_score[y_true == 1],
bins=10,
alpha=alpha,
edgecolor="darkred",
Expand Down
42 changes: 21 additions & 21 deletions tests/test_binary_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def random_data_binary_classifier() -> Tuple[np.ndarray, np.ndarray]:
[0, 1], n_samples, p=[0.4, 0.6]
) # the true class labels 0 or 1, with class imbalance 40:60

y_prob = np.zeros(y_true.shape) # a model's probability of class 1 predictions
y_prob[y_true == 1] = np.random.beta(1, 0.6, y_prob[y_true == 1].shape)
y_prob[y_true == 0] = np.random.beta(0.5, 1, y_prob[y_true == 0].shape)
return y_true, y_prob
y_score = np.zeros(y_true.shape) # a model's probability of class 1 predictions
y_score[y_true == 1] = np.random.beta(1, 0.6, y_score[y_true == 1].shape)
y_score[y_true == 0] = np.random.beta(0.5, 1, y_score[y_true == 0].shape)
return y_true, y_score


# Test histogram plot
Expand All @@ -39,10 +39,10 @@ def test_hist_plot(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
y_true, y_score = random_data_binary_classifier
print(TEST_RESULTS_PATH)
binary.plot_y_prob_histogram(y_prob, save_fig_path=TEST_RESULTS_PATH / "histogram.png")
binary.plot_y_prob_histogram(y_prob, y_true, save_fig_path=TEST_RESULTS_PATH / "histogram_2_classes.png")
binary.plot_y_score_histogram(y_true=None, y_score=y_score, save_fig_path=TEST_RESULTS_PATH / "histogram.png")
binary.plot_y_score_histogram(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "histogram_2_classes.png")


# test roc curve without bootstrapping
Expand All @@ -55,8 +55,8 @@ def test_roc_curve(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_roc_curve(y_true, y_prob, save_fig_path=TEST_RESULTS_PATH / "roc_curve.png")
y_true, y_score = random_data_binary_classifier
binary.plot_roc_curve(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "roc_curve.png")


# test roc curve with bootstrapping
Expand All @@ -69,9 +69,9 @@ def test_roc_curve_bootstrap(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
y_true, y_score = random_data_binary_classifier
binary.plot_roc_curve(
y_true, y_prob, n_bootstraps=10000, save_fig_path=TEST_RESULTS_PATH / "roc_curve_bootstrap.png"
y_true, y_score, n_bootstraps=10000, save_fig_path=TEST_RESULTS_PATH / "roc_curve_bootstrap.png"
)


Expand All @@ -85,8 +85,8 @@ def test_pr_curve(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_pr_curve(y_true, y_prob, save_fig_path=TEST_RESULTS_PATH / "pr_curve.png")
y_true, y_score = random_data_binary_classifier
binary.plot_pr_curve(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "pr_curve.png")


# test confusion matrix
Expand All @@ -99,8 +99,8 @@ def test_confusion_matrix(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_confusion_matrix(y_true, y_prob, save_fig_path=TEST_RESULTS_PATH / "confusion_matrix.png")
y_true, y_score = random_data_binary_classifier
binary.plot_confusion_matrix(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "confusion_matrix.png")


# test classification report
Expand All @@ -113,8 +113,8 @@ def test_classification_report(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_classification_report(y_true, y_prob, save_fig_path=TEST_RESULTS_PATH / "classification_report.png")
y_true, y_score = random_data_binary_classifier
binary.plot_classification_report(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "classification_report.png")

# test calibration curve
def test_calibration_curve(random_data_binary_classifier):
Expand All @@ -126,8 +126,8 @@ def test_calibration_curve(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_calibration_curve(y_prob, y_true, save_fig_path=TEST_RESULTS_PATH / "calibration_curve.png")
y_true, y_score = random_data_binary_classifier
binary.plot_calibration_curve(y_score, y_true, save_fig_path=TEST_RESULTS_PATH / "calibration_curve.png")

# test accuracy
def test_accuracy(random_data_binary_classifier):
Expand All @@ -139,5 +139,5 @@ def test_accuracy(random_data_binary_classifier):
random_data_binary_classifier : Tuple[np.ndarray, np.ndarray]
The simulated data.
"""
y_true, y_prob = random_data_binary_classifier
binary.plot_accuracy(y_true, y_prob, save_fig_path=TEST_RESULTS_PATH / "accuracy.png")
y_true, y_score = random_data_binary_classifier
binary.plot_accuracy(y_true, y_score, save_fig_path=TEST_RESULTS_PATH / "accuracy.png")

0 comments on commit 1e7d197

Please sign in to comment.