Question: def my _ pca ( X , n _ components ) : ' ' ' Performs PCA on X to reduce it to ` n

def my_pca(X, n_components):
'''
Performs PCA on X to reduce it to `n_components`, using the method
described in lecture but with the 'centering' of X before SVD is done.
Parameters
----------
X: np.darray
An `m * n` matrix where `m` is the number of samples and `n` is the
number of features which each sample has. In other words, the `i`th sample
is given by `X[i]`.
n_components: int
No. of components that the reduced space has.
Returns
-------
The tuple `(components, singular_values)`. Here, `components` is an
`n_components * n` matrix such that `components[i]` returns the `i`th
principal axis that has the `i`th largest singular value. In addition,
`singular_values` is a 1D numpy array with `n_components` entries such that
`singular_values[i]` returns the `i`th singular value.
Note
----
'centering' here refers to subtracting the mean from X such that the resulting
X' has a mean of 0 for each feature.
'''
# TODO: add your solution here and remove `raise NotImplementedError`
# no loop allowed
X_centered = X - np.mean(X, axis=0)
# Performing SVD
U, S, Vt = np.linalg.svd(X_centered.T)
# Extracting the first n_components principal components
components = Vt[:n_components, :]
# Extracting the corresponding singular values
singular_values = S[:n_components]
return components, singular_values
X_test_1= np.array([[0.16550775,-16.57982642,-50.13603715,-20.68348877,8.80989065],[27.74489674,17.10507978,18.71626655,32.02624683,20.99010911],[18.05469443,-43.28464778,31.50703811,-18.97769479,0.44841138],[-45.96509892,42.75939443,-0.08726752,7.63493673,-30.24841115]])
expected_pca = my_pca(X_test_1,2)
# Public test case 1
assert(not np.all(expected_pca[1]== np.array([7282.76644566,4817.22025846])))
# Public test case 2
assert(not np.all(expected_pca[1]== np.array([2427.58881522,1605.74008615])))
# Public test case 3
diff_singular_values = np.abs(expected_pca[1]- np.array([1820.69161142,1204.30506462]))
print("Difference in singular values:", diff_singular_values)
assert(np.all(diff_singular_values <0.000001))
# Public test case 4
X_test_4= np.array([[63.69616873,26.97867138,4.09735239,1.65276355,81.32702392],[91.27555773,60.66357758,72.9496561,54.36249915,93.50724238],[81.58535541,0.27385002,85.74042766,3.35855753,72.96554464],[17.56556206,86.31789223,54.14612202,29.97118905,42.26872212]])
diff_singular_values_4= np.abs(my_pca(X_test_4,2)[1]- np.array([1820.69161142,1204.30506462]))
assert(np.all(diff_singular_values_4<0.000001))
# Public test case 5
X_test_5= np.array([[0.22733602,0.31675834,0.79736546,0.67625467,0.39110955,0.33281393,0.59830875],[0.18673419,0.67275604,0.94180287,0.24824571,0.94888115,0.66723745,0.09589794],[0.44183967,0.88647992,0.6974535,0.32647286,0.73392816,0.22013496,0.08159457],[0.1598956,0.34010018,0.46519315,0.26642103,0.8157764,0.19329439,0.12946908]])
diff_singular_values_5= np.abs(my_pca(X_test_5,3)[1]- np.array([0.14205335,0.06453807,0.0472502]))
assert(np.all(diff_singular_values_5<0.000001))
# Public test case 6
X_test_6= np.array([[0.22733602,0.31675834,0.79736546,0.67625467,0.39110955,0.33281393,0.59830875],[0.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!