Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-059

Correlation Feature Selection

Problem

Select the feature most correlated with y by absolute Pearson correlation.

Return the index of the feature with the largest |corr|, breaking ties by the smallest index; a constant feature or constant y gives correlation 0.0. Empty X returns -1.

Hint

Pearson correlation is the covariance of the column and y divided by the product of their standard deviations.

Test cases

case 1
in: [[[1,10],[2,9],[3,8],[4,7]],[2,4,6,8]]
expected: 0
case 2
in: [[[1,1],[2,3],[3,2],[4,5]],[1,2,3,4]]
expected: 0
case 3
in: [[[2,2],[2,2]],[1,2]]
expected: 0
case 4
in: [[],[]]
expected: -1
Starter codePython
def select_feature_by_correlation(X, y):
    # Your code here
    pass

4 test cases · run your code against them in the editor