Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-044

Projection onto a Principal Component

Problem

Project X onto the unit principal component vector v: subtract the column means, then compute the dot product of each centered row with v.

Return the list of 1D projected coordinates. Empty X returns [].

Hint

Center first, then dot each row with v.

Test cases

case 1
in: [[[1,2],[3,4],[5,6]],[0.7071067811865476,0.7071067811865476]]
expected: [-2.8284271247461903,0,2.8284271247461903]
case 2
in: [[[1,2],[3,4],[5,6]],[1,0]]
expected: [-2,0,2]
case 3
in: [[[0,0],[1,0],[2,0]],[0,1]]
expected: [0,0,0]
case 4
in: [[[1,1],[2,2]],[0.6,0.8]]
expected: [-0.7,0.7]
Starter codePython
def project_onto_pc(X, v):
    # Your code here
    pass

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