|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from sklearn.preprocessing import RobustScaler, StandardScaler |
| 4 | +from skrub import SquashingScaler |
| 5 | + |
| 6 | + |
| 7 | +def generate_data_with_outliers(): |
| 8 | + np.random.seed(0) # for reproducibility |
| 9 | + values = np.random.rand(100, 1) |
| 10 | + n_outliers = 15 |
| 11 | + outlier_indices = np.random.choice(values.shape[0], size=n_outliers, replace=False) |
| 12 | + values[outlier_indices] = np.random.rand(n_outliers, 1) * 100 - 50 |
| 13 | + return values |
| 14 | + |
| 15 | + |
| 16 | +def plot_feature_with_outliers(values): |
| 17 | + """Plot a feature with outliers and annotate it.""" |
| 18 | + x = np.arange(values.shape[0]) |
| 19 | + fig, axs = plt.subplots(1, layout="constrained", figsize=(6, 4)) |
| 20 | + |
| 21 | + axs.plot(x, values) |
| 22 | + _ = axs.set(title="Feature with outliers", ylabel="value", xlabel="Sample ID") |
| 23 | + axs.axhspan(-2, 2, color="gray", alpha=0.15) |
| 24 | + |
| 25 | + x_data, y_data = [30, 2] |
| 26 | + desc = "Data is mostly\nin [-2, 2]" |
| 27 | + axs.annotate( |
| 28 | + desc, |
| 29 | + xy=(x_data, y_data), |
| 30 | + xytext=(0.15, 0.8), |
| 31 | + textcoords="axes fraction", |
| 32 | + arrowprops=dict(arrowstyle="->", color="red"), |
| 33 | + ) |
| 34 | + |
| 35 | + x_outlier, y_outlier = np.argmax(values), np.max(values) |
| 36 | + desc = "There are large\noutliers throughout." |
| 37 | + _ = axs.annotate( |
| 38 | + desc, |
| 39 | + xy=(x_outlier, y_outlier), |
| 40 | + xytext=(0.6, 0.85), |
| 41 | + textcoords="axes fraction", |
| 42 | + arrowprops=dict(arrowstyle="->", color="red"), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def scale_feature_and_plot(values): |
| 47 | + |
| 48 | + squash_scaler = SquashingScaler() |
| 49 | + squash_scaled = squash_scaler.fit_transform(values) |
| 50 | + |
| 51 | + robust_scaler = RobustScaler() |
| 52 | + robust_scaled = robust_scaler.fit_transform(values) |
| 53 | + |
| 54 | + standard_scaler = StandardScaler() |
| 55 | + standard_scaled = standard_scaler.fit_transform(values) |
| 56 | + |
| 57 | + x = np.arange(values.shape[0]) |
| 58 | + fig, axs = plt.subplots(1, 2, layout="constrained", figsize=(8, 5)) |
| 59 | + |
| 60 | + ax = axs[0] |
| 61 | + ax.plot(x, sorted(values), label="Original Values", linewidth=2.5) |
| 62 | + ax.plot(x, sorted(squash_scaled), label="SquashingScaler") |
| 63 | + ax.plot(x, sorted(robust_scaled), label="RobustScaler", linestyle="--") |
| 64 | + ax.plot(x, sorted(standard_scaled), label="StandardScaler") |
| 65 | + |
| 66 | + # Add a horizontal band in [-4, +4] |
| 67 | + ax.axhspan(-4, 4, color="gray", alpha=0.15) |
| 68 | + ax.set(title="Original data", xlim=[0, values.shape[0]], xlabel="Percentile") |
| 69 | + ax.legend() |
| 70 | + |
| 71 | + ax = axs[1] |
| 72 | + ax.plot(x, sorted(values), label="Original Values", linewidth=2.5) |
| 73 | + ax.plot(x, sorted(squash_scaled), label="SquashingScaler") |
| 74 | + ax.plot(x, sorted(robust_scaled), label="RobustScaler", linestyle="--") |
| 75 | + ax.plot(x, sorted(standard_scaled), label="StandardScaler") |
| 76 | + |
| 77 | + ax.set(ylim=[-4, 4]) |
| 78 | + ax.set(title="In range [-4, 4]", xlim=[0, values.shape[0]], xlabel="Percentile") |
| 79 | + |
| 80 | + # Highlight the bounds of the SquashingScaler |
| 81 | + ax.axhline(y=3, alpha=0.2) |
| 82 | + ax.axhline(y=-3, alpha=0.2) |
| 83 | + |
| 84 | + fig.suptitle( |
| 85 | + "Comparison of different scalers on sorted data with outliers", fontsize=20 |
| 86 | + ) |
| 87 | + fig.supylabel("Value") |
| 88 | + |
| 89 | + desc = "The RobustScaler is\naffected by outliers" |
| 90 | + axs[0].annotate( |
| 91 | + desc, |
| 92 | + xy=(0, -70), |
| 93 | + xytext=(0.4, 0.2), |
| 94 | + textcoords="axes fraction", |
| 95 | + arrowprops=dict(arrowstyle="->", color="red"), |
| 96 | + ) |
| 97 | + |
| 98 | + desc = "The SquashingScaler is\nclipped to a finite value" |
| 99 | + _ = axs[1].annotate( |
| 100 | + desc, |
| 101 | + xy=(0, -3), |
| 102 | + xytext=(0.4, 0.2), |
| 103 | + textcoords="axes fraction", |
| 104 | + arrowprops=dict(arrowstyle="->", color="red"), |
| 105 | + ) |
0 commit comments