Sub topic 13
Checking correlations between numeric columns
This chapter assumes you have loaded train.csv into a DataFrame named df, as in earlier chapters, and that you are comfortable with groupby and basic plotting. A correlation is a single number that summarizes how two numeric columns move together. It is a quick way to decide where to look next. It does not explain why anything happens.
What a correlation coefficient measures
Pandas computes the Pearson correlation by default. It ranges from -1 to 1.
- A value near 1 means that when one column is high, the other tends to be high.
- A value near -1 means that when one column is high, the other tends to be low.
- A value near 0 means there is no straight-line relationship.
Pearson correlation only detects linear patterns. A U-shaped relationship can produce a value near 0 even when the columns are strongly linked. It is also sensitive to outliers, such as the few very large Fare values in the Titanic data.
Computing the correlation matrix
df.corr() compares every pair of columns. Text columns such as Name and Sex cannot be correlated, so pass numeric_only=True. Newer pandas versions raise an error if you leave it out and text columns are present.
corr = df.corr(numeric_only=True)
print(corr["Survived"].sort_values(ascending=False))Selecting the Survived column of the matrix gives one number per column, sorted from most positive to most negative. Every column has a correlation of exactly 1 with itself, so Survived appears at the top.
Expect Fare to be positively correlated with Survived and Pclass to be negatively correlated. Pclass is coded 1, 2, 3, where 1 is first class, so a lower class number goes with higher survival. The negative sign comes from the coding, not from first class being worse.
PassengerId is just a row label. Any correlation it shows is noise, so ignore it.
How missing values are handled
Pandas computes each pair of columns using only the rows where both values are present. The Age column has missing values, so its correlations rest on fewer rows than the correlation between Pclass and Fare. Two numbers in the same matrix can therefore be based on different groups of passengers. Compare this with the missing-value counts from earlier before trusting a small difference.
Common errors
- Reading correlation as cause.
FareandSurvivedmove together, butFareis tied to class, sex, and where passengers boarded. The number does not separate these. - Treating coded categories as quantities.
Pclassis a category stored as a number. Its correlation is a rough guide, and thegroupbytable from the previous chapter is more reliable. - Using
Survivedas if it were continuous. It only takes the values 0 and 1. The correlation is still usable, but the group means are easier to read. - Judging by size alone. A value of 0.1 can look weak but may still matter. A value of 0.3 on a small number of rows may be unstable.
Checking one pair directly
When a coefficient looks interesting, verify it with the grouped view you already know.
print(df.groupby("Pclass")["Fare"].agg(["count", "median"]))The median fare falling as class number rises shows why Fare and Pclass are strongly negatively correlated. They largely carry overlapping information.
Exercise
Why can Age show a correlation with Survived that is based on fewer passengers than the correlation of Fare with Survived, and how would you check how many rows were used?
Show answer
df.corr() drops missing values pair by pair. Age has missing values and Fare has almost none, so the Age pair uses fewer rows. Check it with df[["Age", "Survived"]].dropna().shape[0], which counts the rows where both values are present.