Skip to main content

Exploratory Data Analysis with Auto MPG Dataset

· 9 min read
Ross Bulat
Full Stack Engineer

Google Colab notebook: Open in Google Colab · Raw Notebook: Download .ipynb

Introduction

I used exploratory data analysis (EDA) to assess whether the Auto MPG dataset was suitable for machine learning and to identify key uncertainties before modelling.

I followed a standard EDA workflow: importing the analysis libraries, loading the data, inspecting its structure and summary statistics, separating numeric and categorical variables, checking for missing values, and using visualisations (including heat maps and scatter plots) to examine relationships.

The dataset contains 398 rows and 9 original columns. The main variables are fuel economy (mpg), engine size (displacement), engine power (horsepower), vehicle weight, acceleration, model year, origin, and car name.

1. Loading and inspecting the data

I began by loading auto-mpg.csv into pandas and reviewing the first and last rows, dataset shape, and inferred data types. This initial check exposed an important quality issue: horsepower had been read as an object column instead of a numeric variable.

This was because missing values were represented by a question mark (?) rather than standard NaN. It was a useful reminder that missing data is not always immediately visible through isna() on raw input, especially when pandas has inferred a text type.

2. Identifying missing values

After converting horsepower using pd.to_numeric(..., errors='coerce'), the question marks were correctly converted to missing values. The missing-value summary was:

missing_countmissing_percent
horsepower61.51
mpg00
cylinders00
displacement00
weight00
acceleration00
model year00
origin00
car name00
horsepower_imputed00
origin_label00
origin_America00
origin_Europe00
origin_Japan00

Bar chart of missing values by column, showing six for horsepower and zero for the other original columns

The chart confirms that the missing values are confined to horsepower. This left a small but non-trivial missing-data issue: six missing horsepower values (approximately 1.51% of records). To keep the workflow transparent, I retained the original horsepower column and created a separate horsepower_imputed feature using median imputation for analysis that required complete numeric inputs.

I selected median imputation because it is straightforward, reproducible, and generally more robust to skew than mean imputation.

3. Encoding categorical values

The task required categorical values to be represented numerically. In this dataset, origin was already stored as integer codes (America = 1, Europe = 2, Japan = 3). However, integer encoding (1, 2, 3) implies an artificial order and distance between what are actually nominal categories—there is no meaningful sense in which Japan is "three times" America, or in which Europe sits "between" the other two.

For this reason, I first mapped the integer codes to readable labels and then applied one-hot encoding using pd.get_dummies(...), which produced three binary indicator columns:

Original originorigin_Americaorigin_Europeorigin_Japan
America100
Europe010
Japan001

One-hot encoding avoids the false ordinal assumption that integer encoding can introduce, and it is generally the more appropriate choice for nominal categorical features in machine learning pipelines. Each indicator column is itself a valid numeric feature (a Bernoulli 0/1 variable), so unlike a single arbitrary integer code, the one-hot columns can legitimately appear in a Pearson correlation matrix against mpg and other continuous variables.

The origin counts were:

origin_labelcount
America249
Japan79
Europe70

4. Skewness and kurtosis

I calculated skewness and kurtosis for the continuous numeric variables only. Skewness indicates asymmetry in a distribution, while kurtosis indicates tail behaviour relative to a normal distribution. The one-hot origin_* columns are excluded from this analysis because they are binary indicator variables. Distribution shape statistics like skewness and kurtosis are not informative for 0/1 features (their shape is fully described by their proportion).

skewnesskurtosis
mpg0.457-0.511
cylinders0.527-1.377
displacement0.72-0.747
horsepower1.0870.697
horsepower_imputed1.1060.764
weight0.531-0.786
acceleration0.2790.419
model year0.012-1.181

Histogram and density curve of the right-skewed imputed horsepower distribution

The histogram makes the right skew in horsepower_imputed clear. Most vehicles sit in the low-to-mid power range, with a smaller number of high-powered vehicles forming the right tail. displacement, weight, and cylinders also showed positive skew, which aligns with a dataset dominated by moderate vehicles and fewer extreme large-engine observations.

Most kurtosis values were close to zero or negative, so the distributions were not generally dominated by heavy tails. Since skew and outliers can affect algorithm performance and interpretation, later stages may require transformation, scaling, or more robust estimators depending on the model chosen.

5. Correlation heat map

I used a correlation heat map to examine linear relationships between numeric features. Because origin is represented as three binary one-hot columns, each can be included in the correlation matrix as a point-biserial-style relationship with mpg. This is a valid use of Pearson correlation between a continuous variable and a 0/1 indicator.

The correlations with mpg for the continuous variables were:

mpg
mpg1
model year0.579
acceleration0.42
horsepower_imputed-0.773
cylinders-0.775
displacement-0.804
weight-0.832

Correlation heat map for the continuous variables and one-hot encoded origin columns

The strongest negative relationship was between mpg and weight, which is intuitive: heavier vehicles tend to consume more fuel. displacement, horsepower, and cylinders were also strongly negatively associated with mpg.

The heat map also shows strong correlations between several mechanical variables. displacement and weight have a correlation of 0.93, while cylinders and displacement have a correlation of 0.95. This degree of multicollinearity would need to be considered when selecting and interpreting a model, even if the main aim were simply to predict mpg.

model year showed a positive relationship with mpg, suggesting that newer vehicles in this historical sample were generally more fuel efficient. I treated this carefully in interpretation, since correlation alone does not establish causation and may reflect broader historical factors such as regulation, design shifts, or market priorities.

The one-hot origin_* columns showed the direction expected from the grouped summaries in Section 7: origin_America was negatively correlated with mpg, while origin_Japan and origin_Europe were positively correlated with mpg. Because the three indicators are mutually exclusive and sum to 1 for every row, they are not independent of each other, so they should be interpreted together rather than as fully separate features.

6. Scatter plots

The scatter plots focused on pairwise relationships involving mpg, particularly:

  • mpg vs weight
  • mpg vs horsepower_imputed
  • mpg vs displacement
  • mpg vs acceleration
  • mpg vs model year

Scatter plot of MPG against vehicle weight, coloured by vehicle origin

The weight plot shows this pattern most clearly: mpg falls as vehicles become heavier, and the fitted line highlights the inverse relationship. The same downward trend appeared as horsepower_imputed and displacement increased. Colouring the points by origin also revealed clusters in different parts of the feature space. In this dataset, American-origin vehicles appeared more frequently in heavier, larger-displacement regions, while Japanese and European vehicles were more common in lighter, higher-mpg regions.

These patterns are useful for modelling, but need to be interpreted with caution. Origin may be acting as a proxy for other factors (vehicle size, production era, market segment, or design choices), so it should not be treated as a standalone causal explanation.

7. Grouped summaries

I also compared averages by origin:

origin_labelcountavg_mpgavg_weightavg_horsepower
Japan7930.452221.2379.84
Europe7027.892423.380.93
America24920.083361.93118.64

Box plots of MPG grouped by vehicle origin

The box plots provide more context. American vehicles had the lowest median MPG and Japanese vehicles the highest, although the distributions overlapped and there was variation within every group. In this sample, vehicles from Japan and Europe also tended to have lower average weight and horsepower. This was a useful example of how EDA methods work together: correlation gives a quantitative overview, while grouped summaries and distributions improve interpretability.

8. Reflection against the learning outcomes

At face value, Auto MPG appears straightforward, but the horsepower issue demonstrated why EDA is essential before any model development. Hidden missingness in a nominally numeric feature can directly undermine model reliability.

The EDA highlighted several issues to address before selecting a model, including missing values, skewed feature distributions, multicollinearity among mechanical variables, and encoding decisions for categorical data. Each has implications for model validity, interpretability, and defensibility.

Although this dataset is less sensitive than personal or clinical data, there are still professional and ethical considerations. It is historical data, so findings should not be overgeneralised to contemporary vehicle populations. Similarly, origin-based comparisons should be framed carefully to avoid simplistic conclusions, since origin may co-vary with broader engineering and market factors.

I also treated reproducibility as part of professional practice by documenting cleaning decisions, preserving raw and transformed columns, and structuring the analysis so another team member could review the assumptions and continue the workflow into modelling.

Conclusion

Overall, this EDA suggests that the Auto MPG dataset is suitable for introductory machine learning tasks, particularly MPG prediction, but only after data preprocessing. The hidden missing values in horsepower, skew in engine-related variables, strong inter-feature correlations, and categorical encoding choices all require explicit handling.

The exercise reinforced the value of EDA as a way to reduce risk before modelling. It helps identify data quality problems early, supports transparent methodological decisions, and provides a stronger foundation for responsible model development.