Gearing Up

This lesson will focus on tackling redundant or correlated features in a dataset. These features provide similar, overlapping information that can potentially affect the performance of machine learning models. Learning to handle such features is critical to data cleaning and preprocessing.

Why is handling redundant or correlated features necessary, you ask? Here's the reason: Machine learning models are grounded in mathematics, and we need to ensure that the input data doesn't contain multicollinearity, meaning predictors are not independent, as it may cause issues with mathematical calculations. By identifying and eliminating redundant or correlated features, we can ensure that each feature in our dataset offers unique and valuable information that improves the predictive model's performance.

Correlation: A Quick Introduction

In statistics, correlation is a term that indicates the degree to which two variables move in relation to each other. If two features are highly correlated, they carry similar information.

In the context of our Titanic dataset, let's consider the pclass (passenger class) and fare (ticket cost) columns. Intuitively, passengers belonging to higher class (1st) would have paid a higher fare. Therefore, these two columns are likely to be strongly correlated.

To quantify this relationship, we use the correlation coefficient, a value between -1 and 1. If the correlation coefficient is close to 1, it indicates a strong positive correlation. Conversely, a coefficient near -1 indicates a strong negative correlation. A coefficient close to zero suggests no correlation.

To calculate the correlation between features in our dataset, we use the corr() function from the Pandas library. Let's see how it's done:

Python
import seaborn as sns

# Load the dataset
titanic_df = sns.load_dataset('titanic')

# Calculate and print the correlation matrix
corr_matrix = titanic_df.corr(numeric_only=True)
print(corr_matrix)
"""
            survived    pclass       age  ...      fare  adult_male     alone
survived    1.000000 -0.338481 -0.077221  ...  0.257307   -0.557080 -0.203367
pclass     -0.338481  1.000000 -0.369226  ... -0.549500    0.094035  0.135207
age        -0.077221 -0.369226  1.000000  ...  0.096067    0.280328  0.198270
sibsp      -0.035322  0.083081 -0.308247  ...  0.159651   -0.253586 -0.584471
parch       0.081629  0.018443 -0.189119  ...  0.216225   -0.349943 -0.583398
fare        0.257307 -0.549500  0.096067  ...  1.000000   -0.182024 -0.271832
adult_male -0.557080  0.094035  0.280328  ... -0.182024    1.000000  0.404744
alone      -0.203367  0.135207  0.198270  ... -0.271832    0.404744  1.000000

[8 rows x 8 columns]
"""

Here, titanic_df.corr(numeric_only=True) returns a DataFrame with the correlation coefficients between all pairs of numeric columns in titanic_df.

This correlation matrix displays the relationship between each pair of numerical columns. For instance, the correlation between fare and pclass is -0.549500. This negative sign indicates a negative correlation, meaning that passenger class decreases as the fare increases, which is consistent with our initial assumption.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal