Image by AuthorExploratory Data Analysis usually means writing dozens of plotting commands, calculating summary statistics by hand, and creating comparison visualizations to understand your data. Sweetviz cuts through this work by generating comprehensive HTML reports with a single line of code. This tutorial shows you how to use Sweetviz for quick dataset profiling, group comparisons, and target variable analysis using the Titanic dataset. You’ll learn to automate the repetitive parts of EDA while keeping the depth of analysis you need for feature engineering and modeling decisions.
Prerequisites and Setup
Sweetviz requires Python 3.6 or higher along with pandas and seaborn for dataset access. Sweetviz currently has compatibility issues with NumPy 2.0 and above, so you’ll need NumPy version 1.x.
Install the required packages with NumPy version control:
pip install numpy<2.0 pip install sweetviz pandas seaborn
This installs Sweetviz with its dependencies including matplotlib and scipy. The NumPy version constraint prevents compatibility errors with the newer 2.x releases. After installation, restart your kernel before you start analyzing.
Creating Your First Automated Report
Sweetviz’s analyze() function generates a complete exploratory report covering distributions, missing values, correlations, and data types for every variable in your dataset. The Titanic dataset works well as a test case with its mix of numerical, categorical, and missing data.
import sweetviz as sv
import seaborn as sns
df = sns.load_dataset('titanic')
report = sv.analyze(df)
report.show_html('titanic_analysis.html')
The generated HTML report opens automatically in your browser and displays interactive visualizations for all 15 columns. The report organizes variables into clear cards showing key information at a glance.
For categorical variables like survived, pclass, and sex, you get frequency distributions with bar charts. The survived variable shows 2 distinct values with no missing data. Passenger class (pclass) displays three categories with third class being the most common at about 55% of passengers.
Numerical variables like age show histograms with comprehensive statistics. The age distribution reveals 714 valid entries (80%) with 177 missing values (20%), a mean of 29.7 years, and a median of 28.0. The report displays quartiles, range, standard deviation, and even skewness, giving you instant insight into the data shape.
This single function call replaces what would normally require separate pandas profiling commands, multiple matplotlib figures, and manual missing value checks. Within seconds, you have a complete picture of data types, distributions, and quality issues.
Analyzing Target Variable Relationships
For supervised learning projects, knowing which features correlate with your target variable guides feature selection and engineering efforts. Sweetviz automates this analysis by calculating association metrics between every feature and a specified target.
df = sns.load_dataset('titanic')
report = sv.analyze(df, target_feat='survived')
report.show_html('titanic_survival_analysis.html')
The target analysis report now includes association scores for each variable’s relationship with survival. When you enable target analysis, each variable card displays its correlation strength with the outcome you’re trying to predict.
The survived variable card shows the distribution split between the two outcomes: 0 (did not survive) at about 62% and 1 (survived) at 38%. This immediately tells you that survival was the minority class, an important consideration for modeling.
The associations matrix provides a visual heatmap showing relationships between all variables. Squares represent categorical associations using uncertainty coefficients and correlation ratios, ranging from 0 to 1. Circles show symmetrical numerical correlations (Pearson’s) from -1 to 1. Looking at the top row for survived, you can quickly spot that sex and fare show the strongest associations (larger, darker squares), while age shows a weaker relationship.
For categorical features like sex and passenger class, Sweetviz computes uncertainty coefficients. For numerical features like age and fare, standard correlation coefficients are displayed. These metrics appear directly on each variable’s card, so you don’t need to manually calculate chi-square tests, ANOVA, or correlation matrices.
Comparing Passenger Classes
Dataset comparisons reveal how distributions and statistics differ between groups, which helps you understand subpopulation behavior and detect sampling biases. Comparing first class and third class passengers shows how dramatically experiences varied by ticket class.
df = sns.load_dataset('titanic')
first_class = df[df["pclass"] == 1].copy()
third_class = df[df["pclass"] == 3].copy()
config = sv.FeatureConfig(skip=['pclass'])
comparison = sv.compare([first_class, "First Class"],
[third_class, "Third Class"],
feat_cfg=config)
comparison.show_html('class_comparison.html')
The comparison report displays side-by-side distributions for every variable across both groups.
The comparison report displays side-by-side distributions for every variable across both groups. At the top, you see 216 first class passengers with 2 duplicates compared to 491 third class passengers with 86 duplicates.
The survival comparison is stark. First class passengers achieved 63% survival (the longer blue bar for value 1), while third class passengers managed only 24% survival (the much shorter orange bar for value 1). The visual makes the class disparity in survival outcomes immediately clear.
The sex distribution also shows interesting patterns between the two groups, with both classes having more male than female passengers but in different proportions. We skip the pclass column itself since that’s our filtering criterion and including it would cause type conflicts. This comparison approach works well for train-test splits, A/B testing, or any scenario where you need group-level insights.
Examining Gender Differences in Survival
Gender played a decisive role in Titanic survival outcomes. Sweetviz’s comparison functionality quantifies these differences across all passenger characteristics while filtering by sex.
import sweetviz as sv
import seaborn as sns
df = sns.load_dataset('titanic')
male_df = df[df["sex"] == "male"].copy()
female_df = df[df["sex"] == "female"].copy()
config = sv.FeatureConfig(skip=['sex', 'adult_male'])
male_female = sv.compare([male_df, "Male"],
[female_df, "Female"],
feat_cfg=config)
male_female.show_html('gender_survival.html')
The gender comparison report displays side-by-side statistics for 577 male and 314 female passengers. At the top, you see summary information showing males had 86 duplicates while females had 21, with RAM usage of 212.1 kb and 117.1 kb respectively.
The survival comparison is striking. Looking at the bar chart, male passengers achieved only 19% survival (109 out of 577), while female passengers achieved 74% survival (233 out of 314). The visual makes this disparity immediately obvious with the dramatically different bar lengths.
Age distributions tell an interesting story. Males had 453 valid age entries (79% complete) with a mean of 30.7 years and median of 29.0. Females had 261 valid ages (83% complete) with a mean of 27.9 years and median of 27.0. The distributions are similar, with both groups showing comparable ranges and standard deviations.
Fare data is also comparable between groups. Males and females both show maximum fares around 512, with similar interquartile ranges. The distributions look nearly identical in the histograms, indicating that pricing didn’t differ significantly by gender. These insights confirm that sex should be a primary feature in predictive models and suggest that interaction terms between sex and passenger class might capture additional survival patterns.
Understanding Report Components
Sweetviz reports organize information into several key sections. The overview panel at the top summarizes dataset shape, duplicate rows, and overall missing value percentages. Individual variable cards display distribution visualizations alongside descriptive statistics.
The associations section presents a matrix showing relationships between variables. For the Titanic dataset, you’ll see negative correlation between passenger class and fare, which makes sense since lower class numbers indicate higher ticket classes with premium pricing. High associations can indicate multicollinearity issues or reveal interesting relationships worth investigating.
The interactive HTML reports let you drill into specific variables, toggle between different bin sizes for histograms, and explore exact values by hovering over visualizations.
When to Use Sweetviz
Sweetviz works best during initial data exploration when you need quick insights into a new dataset. The automated reports are helpful when onboarding new team members or documenting data quality for stakeholders who may not be comfortable reading code.
The comparison features make Sweetviz useful for detecting data drift in production systems. Generate regular comparison reports between new data batches and your training set to catch distribution changes before they affect model performance. The group comparison functionality also supports cohort analysis and A/B testing evaluation.
For reproducible research, the HTML reports serve as archivable evidence of data characteristics at specific project stages. Unlike code-based notebooks, Sweetviz reports are self-contained and can be shared with non-technical stakeholders.
Conclusion
Sweetviz automates the mechanical aspects of exploratory data analysis, generating comprehensive profiling reports that would take hours to create manually. The Titanic analysis shows how quickly you can identify data quality issues, quantify feature-target relationships, and compare subgroup distributions. For your next dataset, consider using Sweetviz to speed up your initial exploration phase, then dig deeper into the specific patterns and relationships the automated report surfaces.
