0
$\begingroup$

I reduce data with PCA already from 9 to 3 feature. If I have real data new row which I want to use with pre-train model (.h5). Can I change data 9 feature to PCA 3 feature only one row for test with model ?

import numpy
from pandas import read_csv
from sklearn.decomposition import PCA
# load data
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
# feature extraction
pca = PCA(n_components=3)
fit = pca.fit(X)
# summarize components
print("Explained Variance: %s" % fit.explained_variance_ratio_)
print(fit.components_)
$\endgroup$

1 Answer 1

0
$\begingroup$

Simply use pca.transform(test_data). That is:

test = X[22].reshape(1, -1)
pca.transform(test)

array([[-72.73967494, -86.24860793,  -5.94958303]])

I used a random index of your own train set to illustrate the use case but you can input any array of size 8 here.

Can I change data 9 feature to PCA 3 feature only one row for test with model ?

You have to input 8 features (9 features except your target value).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.