Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

We now work through the first steps of exploratory data analysis – as much of it as we have learned so far – on a few examples.

Wine

We begin with an old dataset, which can be downloaded from the UC Irvine Machine Learning Repository, among other places:

Wine. (1991). UCI Machine Learning Repository.

According to the documentation the dataset has 178 rows (called instances here, as they often are) and 14 variables. The first variable is categorical, encoded as an int from {1, 2, 3}; the others are continuous real variables (hence float). The licence is CC-BY-4.0, so we may use the dataset freely, modify it and share our results, provided we name the original source:

Riccardo Leardi, riclea@anchem.unige.it

The dataset comes as two files:

  • wine.data

  • wine.names

Here wine.data is the dataset proper, without any metadata, and wine.names holds the metadata in human-readable form.

From wine.names we take the following in particular:

These data are the results of a chemical analysis of wines grown in the same region in Italy but derived from three different cultivars. The analysis determined the quantities of 13 constituents found in each of the three types of wines.

  • The attributes are

    1. Alcohol

    2. Malic acid

    3. Ash

    4. Alcalinity of ash

    5. Magnesium

    6. Total phenols

    7. Flavanoids

    8. Nonflavanoid phenols

    9. Proanthocyanins

    10. Color intensity

    11. Hue

    12. OD280/OD315 of diluted wines

    13. Proline

  • Number of Instances

    • class 1 59

    • class 2 71

    • class 3 48

Now that we have successfully downloaded the data and read through the metadata that come with them, we can ask what we actually want to do with them.

One obvious task is to find out whether the three classes (that is, the three producers of the wine) can be told apart by the wine’s chemical composition. In statistical terms: do the distributions of the 13 variables differ enough from class to class for the individual data points (the instances) to be assigned to a class on that basis?

We can set this up as a game: if we delete the first variable, the class, can it be reconstructed from the rest? In the simple version we do not delete the class yet, but use it to estimate three different distributions for each of the 13 attributes. Then, for every instance and ignoring its class, we judge which distribution it most likely belongs to, and check whether we were right.

In the advanced version we split the data into training and test data, or form the three (or more) classes without using the variable “class” at all. First, though, let us take a look at the data.

Quite apart from the classes, one thing is interesting straight away: how are the data distributed? We ask that now for each variable on its own.

<class 'numpy.ndarray'> (178, 14)
[1.000e+00 1.423e+01 1.710e+00 2.430e+00 1.560e+01 1.270e+02 2.800e+00
 3.060e+00 2.800e-01 2.290e+00 5.640e+00 1.040e+00 3.920e+00 1.065e+03]
{np.int64(1): np.int64(59), np.int64(2): np.int64(71), np.int64(3): np.int64(48)}
[ True  True  True]
min 11.03 max 14.83 range 3.8000000000000007 std 0.8095429145285168 n 178
Histogram of the alcohol content in the wine data set, values between 11 and 15 per cent in coarse bins. The bars rise to a maximum of 30 observations at about 13.5 per cent and fall away on both sides; the shape is approximately, but not exactly, symmetric.
The same feature in a finer division and normalised to a density, overlaid with the blue density curve of the fitted normal distribution. The curve lies markedly above the bars at the peak and below them on the flanks — the normal distribution describes the data only roughly.

We have to admit that this distribution does not look particularly normal. You could almost make out a bimodal distribution – a mixture, that is, of two normal distributions with expected values near 12 and 13.5. Rather than go on guessing from the picture, let us note that we shall need a method for testing such guesses rigorously.

In our example we can press the class into service as an auxiliary variable:

Three histograms of the alcohol content laid over one another, one per wine class, in orange, green and blue and without transparency. The three distributions are shifted against each other: orange at low values around 12, green in the middle, blue at high ones around 13.5 to 14. Because the bars hide one another, the overlap cannot be seen.

And indeed, clear differences show at once. The ‘blue’ wine (class 1) has more alcohol on average than the others, the ‘orange’ wine (class 2) less. Let us draw in the corresponding normal densities:

The same three class histograms, with the three fitted normal densities added as curves in violet, brown and red. Each curve sits over its class; the three peaks lie at about 12.2, 13.0 and 13.8 per cent and their flanks overlap considerably.

Unfortunately it is just as clear that the overlaps are so large that between 12 and 14.5 per cent alcohol by volume no sensible prediction of the class is possible.

For completeness we also model the mixture distribution:

The combined histogram of the alcohol content with the blue curve of the weighted mixture of the three class densities. This mixture curve follows the bars noticeably better than the single normal distribution before: it has a shoulder at the top instead of one sharp peak.

Having seen that the first attribute, the alcohol content, depends on the class, it is natural to examine all the other attributes the same way. So we rewrite the code generically and look at all the variables at once:

The three class histograms of the alcohol content again, this time drawn semi-transparently. That makes visible where the classes cover one another — especially between 12.5 and 14 per cent, where all three have a share.
The same triple view for the second feature, malic acid. Two classes crowd narrowly between 1 and 2, the third is spread widely over the range from 2 to 6 — here the classes differ above all in spread, not in position.
The same for the ash content between 1.5 and 3.25. All three classes lie almost on top of one another around 2.2 to 2.6; only one has an additional small share at low values around 1.9. This feature barely separates the classes at all.
The same for the alcalinity of ash between 10 and 30. One class lies clearly to the left with its peak at about 17, the other two further right between 19 and 25 and overlap strongly.
The same for the magnesium content between 70 and 160. All three classes bunch between 85 and 110; one of them has a long tail to the right out to 160. The feature is of little use for separation.
The same for the total phenols between 1 and 4. Here the classes are clearly staggered: one peaking at about 1.5, one at about 2.4 and one at about 3.0. This feature separates the classes well.
The same for the flavanoids between 0 and 5. The staggering is even clearer than for the total phenols: one class crowds narrowly at 0.5 to 1, the second lies around 2, the third around 3. This is one of the most discriminating features of the data set.
The same for the nonflavanoid phenols between 0.1 and 0.65. One class bunches at low values around 0.28, another is spread broadly over 0.4 to 0.6; the third lies between them. The overlap is large.
The same for the proanthocyanins between 0.5 and 3.5. One class lies to the left with peaks around 0.9 to 1.1, the other two around 1.5 to 2.0; all three tail off flatly to the right.
The same for the colour intensity between 1 and 13. One class crowds narrowly between 2 and 4, the second lies around 5, and the third stretches far to the right out to 13 — here the classes differ strongly both in position and in spread.
The same for the hue between 0.5 and 1.7. One class has its sharp peak at about 0.6, the second at 1.05, the third lies broadly between 0.9 and 1.4. Low values belong unambiguously to one class.
The same for the ratio OD280 to OD315 between 1.2 and 4. One class lies compactly between 1.3 and 2.0, the other two overlap between 2.5 and 3.6 — the feature separates one class cleanly, the other two not at all.
The same for the proline content between 300 and 1700. Two classes lie together below 900, the third reaches from 1000 to beyond 1600 and is alone there. Like the flavanoids, this feature serves well for separation.

The pictures suggest that, besides attribute 1 (alcohol), attributes 2 (malic acid), 12 (OD280/OD315 of diluted wines, the protein content) and 13 (proline, an amino acid) might also serve to keep the classes apart.

Histogram of 'alcohol' by class, with the three fitted normal densities as curves over it. The three peaks are staggered at about 12.2, 13.2 and 13.8 per cent; the curves overlap considerably on their flanks.
The same for 'malic acid'. The curve of one class is narrow and tall at 1.8, those of the other two flat and drawn far to the right — the normal distribution fits poorly here, because the data are skewed to the right.
The same for 'protein'. One class lies with a narrow, tall curve at about 1.7, the other two flatter at 2.8 and 3.2. The left-hand class is cleanly separated.
The same for 'proline'. Two classes lie with their curves to the left below 900, the third far to the right with its peak at about 1100 and a long tail out to 1600.

From this we see that malic acid is a rather poor indicator of the class, whereas protein and proline content together look considerably better suited to prediction than, say, the alcohol content. More precisely: protein content separates class 3 from classes 1 and 2, and proline content separates class 1 from classes 2 and 3. Neither separation is really clean, however, as the plot already shows.

Finally, we look at the relationship between individual attributes with a scatter plot:

Scatter plot 'Alcohol content vs. malic acid', all points in one colour. The cloud fills the area with no recognisable direction; at the bottom a dense band forms at low malic acid, above it the points scatter widely. No relationship between the two features can be seen.
Scatter plot 'Protein vs. proline', likewise in a single colour. Here the cloud visibly falls into two regions: at the bottom left a group with low protein and low proline, at the top right one with high values in both, and a more thinly occupied zone between them.

Whereas alcohol content and malic acid show no obvious relationship, the protein-proline plot has a pronounced white patch: a high proline content goes together with a high protein content. It would now be interesting to compute measures of this relationship, and also to search the specialist literature for an explanation of how it comes about.

To see whether the relationship is only apparent, and perhaps really arises from a dependence on the class, we look at the scatter plot once more, with the classes distinguished by colour:

The same scatter plot of protein against proline, now coloured by the three wine classes. The two-part split seen before dissolves: at the top right lies a class with high proline, on the left one with low protein, and the third fills the lower middle region — only the colour makes the structure legible.

And indeed it is quite plain, as it was in the histograms before, that these two attributes taken together separate the classes reasonably well.

What we would like, of course, is a coordinate-independent procedure for separating the classes, so that we do not have to pick two variables by hand. Later, principal component analysis (PCA) will give us one.

Heavy rainfall events (CatRaRE)

The German weather service (DWD) makes data on heavy rainfall events available:

Catalogues of radar-based heavy precipitation events, as a basis for analysing heavy rainfall activity and its damage potential in Germany since 2001

np.void((1, b'W3_Eta_v20', b'W3_Eta_1', 1, 200101020350, 200101020750, b'2001-01-02T03:49:59.000', b'2001-01-02T07:50:00.000', 4, 51, 44.6, 51.0, 100.0, 1, 286, 183, -157462, -4575645, 8315037, b'Baden-W\xfcrttemberg', b'Breisgau-Hochschwarzwal', b'Feldberg (Schwarzwald)', 2.6, 51.5, 38.0, b'28', b'4', 5, 3, 0, 19, b'SWAZF', 19, b'SWAZF', 19, 12, 16.5, 20, 29, 19, 24.8, 30, 6, 620, 21, 0.1, 0.5, 64.9, 0, 0.4, 85, 312, 312, 1187, 585, 1102.3, 1462, -26.1, -311.9, -8.6, 223.5), dtype=[('OBJECTID', '<i8'), ('CatVersion', 'S10'), ('KEY_FIELD', 'S10'), ('Event_ID', '<i8'), ('Start_Time', '<i8'), ('End_Time', '<i8'), ('Date_START', 'S23'), ('Date_END', 'S23'), ('Duration', '<i8'), ('Area', '<i8'), ('Area_LAEA', '<f8'), ('Area_in_G', '<f8'), ('AShare_in_G', '<f8'), ('RRmax_in_G', '<i8'), ('x_RRmax', '<i8'), ('y_RRmax', '<i8'), ('x_RRmaxPRJ', '<i8'), ('y_RRmaxPRJ', '<i8'), ('AGS_RRmax', '<i8'), ('BDL_RRmax', 'S23'), ('LKS_RRmax', 'S23'), ('GMD_RRmax', 'S23'), ('Eta', '<f8'), ('RRmax', '<f8'), ('RRmean', '<f8'), ('Tmax', 'S12'), ('Tmean', 'S12'), ('SRImax', '<i8'), ('SRImean', '<i8'), ('MPP_flag', '<i8'), ('WL_START', '<i8'), ('WLc_START', 'S6'), ('WL_END', '<i8'), ('WLc_END', 'S6'), ('V3_RRmax', '<i8'), ('V3_MIN', '<i8'), ('V3_AVG', '<f8'), ('V3_MAX', '<i8'), ('V4_RRmax', '<i8'), ('V4_MIN', '<i8'), ('V4_AVG', '<f8'), ('V4_MAX', '<i8'), ('BVLK_RRmax', '<i8'), ('BVLK_SUMME', '<i8'), ('BVLK_D1km2', '<i8'), ('SDL_RRmax', '<f8'), ('SDL_GRAD', '<f8'), ('SDL_MAX', '<f8'), ('VSGL_RRmax', '<i8'), ('VSGL_GRAD', '<f8'), ('VSGL_MAX', '<i8'), ('CLC_RRmax', '<i8'), ('CLC_DOMIN', '<i8'), ('SRTM_RRmax', '<i8'), ('SRTM_MIN', '<i8'), ('SRTM_AVG', '<f8'), ('SRTM_MAX', '<i8'), ('TPI_RRmax', '<f8'), ('TPI_MIN', '<f8'), ('TPI_AVG', '<f8'), ('TPI_MAX', '<f8')])
462.0 25.6
5.051172893136403

Now we have to settle on a research question. One question is always obvious: what distribution do the variables have? We can once again look at the bare frequency, that is, at how the points in time are distributed. We could follow it over the years, or average over the years and analyse how the month influences the frequency. The latter suggests itself: heavy rain is a weather phenomenon and so depends on the seasons, and we can expect a clear relationship.

At the moment the start time of a heavy rainfall event is encoded as an int. We turn that to our advantage: make a string out of the int 200101020350 and extract the month from it:

Bar chart 'Share of the month in all heavy rainfall events, in per cent' across the twelve months. The distribution is heavily concentrated on the summer: June and July stand at about 26 and 28 per cent, each winter month below one per cent.

So the expected relationship shows clearly: in July and June heavy rainfall events are considerably more frequent than in winter.

Next we can look at the location, say at the variable GMD_RRmax and the place "Düsseldorf":

0
b'D\xfcsseldorf'
7
The same bar chart for Düsseldorf alone. Two bars remain, two events in June and five in July — at numbers that small the monthly distribution of a single city carries no statement any more.

We see that in Düsseldorf, June and July have so far been the only months with heavy rainfall events.

Seven events are too few for looking at further correlations, so there is little to be gained from staying with Düsseldorf.

To get an impression of the spatial distribution, we look at a scatter plot of the locations of maximum precipitation in the RADKLIM grid (x_RRmax, y_RRmax):

Scatter plot of the grid coordinates of all heavy rainfall events, drawn without transparency. The outline of Germany can be guessed at, but the points cover one another completely into a single blue area in which no differences in density can be made out. The vertical axis has also not yet been converted to the grid.

You can almost make out Germany – and, above all, that the y axis needs rescaling. So we read up on the scaling of the RADKLIM grid and learn that every grid point corresponds to exactly 1×1 km21 \times 1\,\mathrm{km}^2. The plot therefore merely has to be square. To see a little more of the distribution, we also make every point partly transparent. Large clusters then become visible:

The same scatter plot with transparent points. Now the outline of Germany is clearly recognisable, and within it differences in density become visible: in the south and south-west the points lie noticeably denser than in the north.

This shows that heavy rainfall events are more frequent in the south than in the north. We might also suspect a relationship with the duration (Duration) of the events:

Scatter plot of event duration against the north-south coordinate, with very transparent points. Because the duration takes only whole-number values, the picture falls into vertical stripes; the first stripes up to a duration of about 10 are densely occupied, after which it thins out quickly, with isolated stripes out to 72.

Instead of the suspected relationship we see something else entirely: the duration is mostly a single-digit number, and longer events are rather rare. We can bring in the spatial relationship as well by distinguishing by colour:

Map of the heavy rainfall events over Germany, the points coloured by event duration. The area is almost uniformly green, that is short duration; only isolated blue and light points mark the rare long events. The colour scale runs to 72 but is hardly used.

Since there is not much to see here (apart from the rarity of longer events), we look at the analogous plot for the mean precipitation:

The same map, coloured by mean precipitation. Here too almost everything is green, because the few extreme events stretch the scale to 145 and all the other values collapse into the lowest band of colour.

Here we begin to suspect that the scale is hard to read because the more extreme events (in light blue) take up so much of it. Excluding those events shows more:

The same map after the events with mean precipitation above 60 have been removed. Now the scale uses only the range up to about 57, and differences become visible across the whole area: the points range from green through dark blue to light blue.

But we get the same effect by adjusting the colormap instead:

All events again, but with a colour scale put together by hand: green and blue for the lower range of values, orange for the upper. The effect matches that of dropping the extreme values — the mass of the points is distinguishable again, and the few outliers stay visible as orange points instead of disappearing.

That will do on heavy rainfall events for now.

Titanic

Now we get to know matplotlib’s object-oriented interface, using the Titanic dataset. On Kaggle it is part of a ‘competition’.

We download the training dataset as titanic.csv.

Problem: ',' in the field 'Name'
array([(0, 3, b'male', 22, 1, 0), (1, 1, b'female', 38, 1, 0), (1, 3, b'female', 26, 0, 0), (1, 1, b'female', 35, 1, 0)], dtype=[('Survived', '<i8'), ('Pclass', '<i8'), ('Sex', 'S10'), ('Age', '<i8'), ('SibSp', '<i8'), ('Parch', '<i8')])
Total survivors: 342
Total passengers: 891
Survival rate: 0.3838383838383838
array([22, 38, 26, 35, 35, -1, 54, 2, 27, 14])
Wrong mean: 22.85409652076319
Correct mean: 29.847605224963715
29.01418439716312 30.425061425061426
Two age histograms of the Titanic passengers laid over one another, survivors and non-survivors, in steps of one year from 0 to 99. Both distributions have their centre of gravity between 18 and 36 years; among the very small children one group lies noticeably above the other. The individual bars jag strongly, because every year of life is its own bin.
The same view, this time split by the three passenger classes. Third class provides the youngest passengers and peaks around 20 years; first class is markedly older on average and is the only one to extend appreciably beyond 60 years.

It is hardly surprising that in class 3 (the cheapest, in green) the ages tend to be lower than in the other classes. In passing we also notice that class 3 is more frequent than the others (which should perhaps not surprise us either).

To see whether passenger class and survival are related, a scatter plot is enough:

Scatter plot of passenger class against survived-yes-or-no, with very transparent points. Because both variables take only a few values, exactly six points remain; their differing darkness shows how many passengers stand behind each. The darkest is the point at the bottom right: non-surviving passengers of third class.

The scatter plot shows a correlation: passengers in class 3 more often did not survive, and passengers who survived were more often in class 1. It was only visible, however, because we set alpha = 0.005, and it suffers from ignoring the absolute frequencies of the classes (a base rate fallacy).

The correct thing to do here is to compute the probability that a passenger in class 3 survived:

P(Survived=1∣Pclass=3)=P(Survived=1,Pclass=3)P(Pclass=3)P(Survived = 1 | Pclass = 3) = \dfrac{P(Survived = 1, Pclass = 3)}{P(Pclass = 3)}
P( Survived = 1 | Pclass = 1 ) = 0.6296296296296297
P( Survived = 1 | Pclass = 2 ) = 0.47282608695652173
P( Survived = 1 | Pclass = 3 ) = 0.24236252545824846

We see that a first-class passenger was considerably more likely to survive than to die. For second-class passengers the odds are already worse than 50/50, and fewer than a quarter of third-class passengers survived.

It should not surprise us that these probabilities do not sum to 1: they refer to different populations. For the reverse conditional probability, on the other hand, we do expect a sum of 1:

P(Pclass=3∣Survived=1)=P(Survived=1,Pclass=3)P(Survived=1)P(Pclass = 3 | Survived = 1) = \dfrac{P(Survived = 1, Pclass = 3)}{P(Survived = 1)}
P( Pclass = 1 | Survived = 1 ) = 0.39766081871345027
P( Pclass = 2 | Survived = 1 ) = 0.2543859649122807
P( Pclass = 3 | Survived = 1 ) = 0.347953216374269

This shows that survival status certainly does not tell us which class someone travelled in (because the passengers are not spread evenly over the classes). The two directions P(Survived∣Pclass)P(\text{Survived} \mid \text{Pclass}) and P(Pclass∣Survived)P(\text{Pclass} \mid \text{Survived}) are tied together by the class frequencies; exactly how is the business of Bayes’ theorem in the chapter Covariance and dependence, and the chapter Titanic EDA works the same thing through for the variable Sex.

It is always interesting how representative a sample is, because that tells us how far the relationships we have found might generalise. So we now look at whether the variable Sex is correlated with other variables. Admittedly we do not yet know what we are doing, but let that be motivation for learning later what it was we did:

could not convert string to float: np.bytes_(b'male')
-0.003542250865523883 0.9159102368544852

The second return value, pvalue, is a p-value: the probability, for two variables that really are uncorrelated, of seeing a correlation coefficient at least as far from 0 as ours. Here it is 0.92, so an rr like this would be perfectly ordinary for uncorrelated data: the sample cannot tell the tiny deviation from 0 apart from sampling error. That is all the p-value says – in particular, it does not say how likely it is that sex and age are uncorrelated. What a p-value is and is not is settled in the chapter Statistical tests.

PearsonRResult(statistic=np.float64(0.5433513806577556), pvalue=np.float64(1.4060661308798955e-69))

Here we clearly see r>0.5r > 0.5, and a vanishingly small probability pp of observing such an rr for data that are in fact uncorrelated.

PearsonRResult(statistic=np.float64(-0.1319004906047393), pvalue=np.float64(7.850524283810112e-05))
PearsonRResult(statistic=np.float64(-0.003542250865523883), pvalue=np.float64(0.9159102368544852))
PearsonRResult(statistic=np.float64(0.1146308103894224), pvalue=np.float64(0.0006076214735498901))
PearsonRResult(statistic=np.float64(0.24548896009069918), pvalue=np.float64(1.0739326016045421e-13))

So, naively (following the Scipy Stats documentation), we expect the variable SibSp to be correlated with sex. According to the metadata, SibSp is the number of siblings and spouses travelling with the passenger. Age, too, shows a correlation with SibSp:

PearsonRResult(statistic=np.float64(-0.16434928321340586), pvalue=np.float64(8.120291402857697e-07))
Scatter plot 'Siblings and spouses on board, by age'. Both quantities are whole numbers, so the points lie on a grid. The value 0 runs as a dense line across the whole age range out to 80, the value 1 to about 65; values from 3 upwards occur almost only among children under 20, and the maximum of 8 only among infants.