简体   繁体   English

如何将我的 PCA 结果应用于未标记的数据?

[英]How can I implement my PCA results to my unlabelled data?

I am struggling to implement my succesfull PCA.我正在努力实施我成功的 PCA。

This is how my PCA plot looks like:这就是我的 PCA 图的样子:

在此处输入图像描述

I retrieved this from accelerometer data (x, y, z) which I have observed and labeled with A, S and D.我从我观察到的加速度计数据(x,y,z)中检索到这个数据,并用 A、S 和 D 标记。

I can find a lot of information on the internet in how to perform a PCA but now I would like to implement it to my new data.我可以在互联网上找到很多关于如何执行 PCA 的信息,但现在我想将它应用于我的新数据。 And I cant find any information about that, or I am doing it all wrong.而且我找不到任何有关此的信息,或者我做错了。

This is my code:这是我的代码:

import pandas as pd
import os
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

os.chdir(r'C:\Users\##\OneDrive - ##\##\Pyth\data\runFlume1')
os.getcwd()


## read csv
df = pd.read_csv('dataframe_0.csv', delimiter=',', names = ['x','y','z','gradient_x','gradient_y','gradient_z','target'])


features = ['x', 'y', 'z']

# Separating out the features
x = df.loc[:, features].values

# Separating out the target
y = df.loc[:,['target']].values

# Standardizing the features
x = StandardScaler().fit_transform(x)


pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])

finalDf = pd.concat([principalDf, df[['target']]], axis = 1)

fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)

targets = ['A','S','D']
colors = ['r', 'g', 'b']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['target'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()

And my raw dataframe looks like this:我的原始数据框如下所示:

       x      y      z  gradient_x  gradient_y  gradient_z target
0     -0.875 -0.143  0.516      0.0310      0.0000       0.032      A
1     -0.844 -0.143  0.548      0.0155      0.0000       0.000      A
2     -0.844 -0.143  0.516      0.0000      0.0000       0.000      A
3     -0.844 -0.143  0.548      0.0000      0.0000       0.016      A
4     -0.844 -0.143  0.548      0.0000      0.0000       0.016      A
     ...    ...    ...         ...         ...         ...    ...
17947  0.969 -0.079  0.161      0.0000      0.0475       0.016      D
17948  1.000 -0.079  0.161      0.0000      0.0000       0.000      D
17949  0.969 -0.079  0.161      0.0155      0.0000       0.000      D
17950  0.969 -0.079  0.161      0.0000      0.0000       0.000      D
17951  0.969 -0.079  0.161      0.0000      0.0000       0.000      D

So I would to like to use this PCA on data with no label (A,D,S).所以我想在没有标签(A,D,S)的数据上使用这个 PCA。 Does anyone know how I can do this?有谁知道我该怎么做?

Kind regards,亲切的问候,

Simon西蒙

You can simply take your pca object and transform the features of your unlabelled data.您可以简单地获取您的pca对象并transform未标记数据的特征。 Something like:就像是:

unlabelled_df = pd.read_csv('dataframe_unlabeled.csv', 
                delimiter=',', names = ['x','y','z','gradient_x','gradient_y','gradient_z'])


features = ['x', 'y', 'z']

# Separating out the features
x = df.loc[:, features].values

# Standardizing the features
# You need to retain your previous scaler and only `transform` here to avoid leakage
x = scaler.transform(x)  



principalComponents = pca.transform(x)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM