I am trying to run PCA on the loan dataset - find test here and train.
The code snippet is as follows,
from sklearn.decomposition import PCA
pca = PCA(n_components = 2)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
explained_variance = pca.explained_variance_ratio_
However, on running the same, I get the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-102-829bdba63de3> in <module>
3 pca = PCA(n_components = 2)
4 X_train = pca.fit_transform(X_train)
----> 5 X_test = pca.transform(X_test)
6 explained_variance = pca.explained_variance_ratio_
C:\Anaconda\lib\site-packages\sklearn\decomposition\base.py in transform(self, X)
127 X = check_array(X)
128 if self.mean_ is not None:
--> 129 X = X - self.mean_
130 X_transformed = np.dot(X, self.components_.T)
131 if self.whiten:
ValueError: operands could not be broadcast together with shapes (185,112) (2,)
Can someone help me on this? I don't where I am going wrong.
Doing a PCA only require :
import numpy as np
from sklearn.decomposition import PCA
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)
pca.fit(X)
Maybe you should drop labels on train, join test and train then do PCA.