Picture by Editor
# Introduction
A harsh reality to start with: textbook knowledge science normally turns into a lie in the actual world. Ideas and strategies are taught on finely curated, superbly bell-curved knowledge variables, however as quickly as we enterprise into the wild of actual initiatives, we’re hit with a lot of outliers, unduly skewed distributions, and indomitable variances.
A earlier article on constructing an exploratory knowledge evaluation (EDA) pipeline with Pingouin confirmed methods to detect, by assessments, instances when the information violates quite a lot of assumptions like homoscedasticity and normality. However what if the assessments fail? Throwing the information away is not the answer: turning sturdy is.
This text uncovers the craftsmanship of utilizing sturdy statistics in knowledge science processes. These are mathematical strategies notably constructed to yield dependable and legitimate outcomes even when the information doesn’t meet classical assumptions or is pervaded by outliers and noise. By adopting a “select your individual journey” strategy, we’ll create a trio of eventualities utilizing Python’s Pingouin to handle the ugliest facets inside the knowledge chances are you’ll encounter in your day by day work.
# Preliminary Setup
Let’s begin by putting in (if wanted) and importing Pingouin and Pandas, after which we’ll load the wine high quality dataset out there right here.
!pip set up pingouin pandas
import pandas as pd
import pingouin as pg
# Loading our messy, real-world-like dataset, containing purple and white wine samples
url = “https://uncooked.githubusercontent.com/gakudo-ai/open-datasets/refs/heads/most important/wine-quality-white-and-red.csv”
df = pd.read_csv(url)
# Take a small peek at what we’re about to cope with
df.head()
In case you seemed on the earlier Pingouin article, you already know this can be a notoriously messy dataset that failed to fulfill a number of widespread assumptions. Now we’ll embark on three completely different “adventures”, every highlighting a state of affairs, a core downside, and a proposed sturdy repair to deal with it.
// Journey 1: When the Normality Take a look at Fails
Suppose we run normality assessments on two teams: white wine samples and purple wine samples.
white_wine_alcohol = df[df[‘type’] == ‘white’][‘alcohol’]
red_wine_alcohol = df[df[‘type’] == ‘purple’][‘alcohol’]
print(“Normality take a look at for White Wine Alcohol content material:”)
print(pg.normality(white_wine_alcohol))
print(“nNormality take a look at for Purple Wine Alcohol content material:”)
print(pg.normality(red_wine_alcohol))
You will see that that neither distribution is regular, with extraordinarily low p-values. Though non-normality itself does not straight sign outliers or skewness, a robust deviation from normality usually suggests such traits could also be current within the knowledge. Evaluating means by a t-test on this scenario could be harmful and prone to yield unreliable outcomes.
The sturdy repair for a state of affairs like that is the Mann-Whitney U take a look at. As a substitute of evaluating averages, this take a look at compares the ranks within the knowledge — sorting all wines in a bunch from lowest to highest alcohol content material, as an example. This rank-based strategy is the grasp trick that strips outliers of their generally harmful magnitude. This is how:
# Separating our two teams
red_wine = df[df[‘type’] == ‘purple’][‘alcohol’]
white_wine = df[df[‘type’] == ‘white’][‘alcohol’]
# Working the sturdy Mann-Whitney U take a look at
mwu_results = pg.mwu(x=red_wine, y=white_wine)
print(mwu_results)
Output:
U_val various p_val RBC CLES
MWU 3829043.5 two-sided 0.181845 -0.022193 0.488903
For the reason that p-value shouldn’t be beneath 0.05, there isn’t any statistically vital distinction in alcohol content material between the 2 wine sorts — and this conclusion is assured to be outlier-proof and skewness-proof.
// Journey 2: When the Paired T-Take a look at Fails
Say you now need to evaluate two measurements taken from the identical topic — e.g. a affected person’s sugar degree earlier than and after a drug prototype, or two properties measured in the identical bottle of wine. The main target right here is on how the variations between paired measurements are distributed. When such variations usually are not usually distributed, a typical paired t-test will yield unreliable confidence intervals.
The best repair on this state of affairs is the Wilcoxon Signed-Rank Take a look at: the sturdy sibling of the paired t-test, which works by observing the variations between columns and rating their absolute values. In Pingouin, this take a look at is named utilizing pg.wilcoxon(), passing within the two columns containing the paired measures inside the similar topic — e.g. two kinds of wine acidity.
# Run the sturdy Wilcoxon signed-rank take a look at for paired knowledge
wilcoxon_results = pg.wilcoxon(x=df[‘fixed acidity’], y=df[‘volatile acidity’])
print(wilcoxon_results)
End result:
W_val various p_val RBC CLES
Wilcoxon 0.0 two-sided 0.0 1.0 1.0
The outcome above reveals a statistically vital distinction, or “excellent separation,” between the 2 measurements. Not solely are the 2 wine properties completely different, however in addition they function at completely completely different magnitude tiers throughout the dataset.
// Journey 3: When ANOVA Fails
On this third and ultimate journey, we need to examine whether or not residual sugar ranges in wine differ considerably throughout distinct high quality scores — word that the latter vary between 3 and 9, taking integer values, and might due to this fact be handled as discrete classes.
If Pingouin’s Levene take a look at of homoscedasticity fails dramatically — as an example, as a result of sugar variance in mediocre wines is large however very small in top-quality wines — a classical one-way ANOVA might produce deceptive outcomes, as this take a look at assumes equal variances amongst teams.
The repair is Welch’s ANOVA, which penalizes teams with excessive variance, thereby balancing out scales and making comparisons fairer throughout a number of classes. Right here is methods to run this sturdy various to conventional ANOVA utilizing Pingouin:
# Run Welch’s ANOVA to check sugar throughout high quality scores
welch_results = pg.welch_anova(knowledge=df, dv=’residual sugar’, between=’high quality’)
print(welch_results)
End result:
Supply ddof1 ddof2 F p_unc np2
0 high quality 6 54.507934 10.918282 5.937951e-08 0.008353
Even the place a one-way ANOVA might need struggled as a result of unequal variances, Welch’s ANOVA delivers a strong conclusion. The very small p-value is obvious proof that residual sugar ranges differ considerably throughout wine high quality scores. Keep in mind, nonetheless, that sugar is simply a small piece of the puzzle influencing wine high quality — a degree underscored by the low eta-squared worth of 0.008.
# Wrapping Up
Via three instance eventualities, every pairing a messy-data downside with a strong statistical technique, we now have realized that being a talented knowledge scientist doesn’t suggest having excellent knowledge or tuning it completely — it means understanding what to do when the information will get troublesome for various causes. Pingouin’s features implement quite a lot of sturdy assessments that assist escape the failed-assumptions lure and extract mathematically sound insights with little further effort.
Iván Palomares Carrascosa is a pacesetter, author, speaker, and adviser in AI, machine studying, deep studying & LLMs. He trains and guides others in harnessing AI in the actual world.

