I'm trying to isolate which features of a dataset (i.e. the columns of a Pandas DataFrame) to use for linear regression, and I want to choose those that are not strongly correlated (under the assumption that the independent variables need to be uncorrelated with each other, so we want to remove any which appear to be strongly correlated).
I have isolated the initial list of features that are correlated with my target variable, like so:
# get the absolute correlations for the target variable
correlations_target = abs(df.corr()[target_variable_name])
# filter out those that are below our threshold
correlated_features = correlations_target[correlations_target >= correlation_threshold]
# drop the target variable's column
correlated_features.drop(target_variable_name, inplace=True)
# get the column names for later use
correlated_feature_variable_names = correlated_features.index
I'd now like to go through each of these relevant feature variables and make sure that none of them have strong correlations, and if they do then drop the one with the weakest correlation to the target variable. Here's what I've cooked up for this:
# the collection of feature variable names we'll drop due to their being correlated to other features
correlated_feature_variable_names_to_drop = []
# loop over the feature combinations
for name_1 in correlated_feature_variable_names:
for name_2 in correlated_feature_variable_names:
# only look at correlations between separate feature variables
if name_1 != name_2:
# drop one of the feature variables if there's a strong correlation
if abs(df[[name_1, name_2]].corr()[name_1][name_2]) > 0.6:
# only worry about it if neither of the variables have been added to the drop list
if (name_1 not in correlated_feature_variable_names_to_drop) and \
(name_2 not in correlated_feature_variable_names_to_drop):
# drop the one which has the least correlation to the target variable
if correlated_features[name_1] >= correlated_features[name_2]:
correlated_feature_variable_names_to_drop.append(name_2)
else:
correlated_feature_variable_names_to_drop.append(name_1)
# drop the variables we've found that qualify
correlated_features.drop(correlated_feature_variable_names_to_drop, inplace=True)
# get the remaining variables' column names for later use
filtered_feature_variable_names = correlated_features.index
The filtered feature set will be used as input for simple regression models. For example:
# fit a simple ordinary least squares model to the features
X = df[filtered_feature_variable_names]
y = df[target_variable_name]
estimate = sm.OLS(y, np.asarray(X)).fit()
# display the regression results
estimate.summary()
Because this is the first time I've tried this I'm unsure if this is the correct way to go about it, and if it is then there might be a more clever or efficient (or "Pythonic") way to perform the filtering than the looping approach I've used above.