Ad
Unable To Run PCA On A Dataset
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.
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad