[英]How can I implement my PCA results to my unlabelled data?
我正在努力实施我成功的 PCA。
这就是我的 PCA 图的样子:
我从我观察到的加速度计数据(x,y,z)中检索到这个数据,并用 A、S 和 D 标记。
我可以在互联网上找到很多关于如何执行 PCA 的信息,但现在我想将它应用于我的新数据。 而且我找不到任何有关此的信息,或者我做错了。
这是我的代码:
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()
我的原始数据框如下所示:
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
所以我想在没有标签(A,D,S)的数据上使用这个 PCA。 有谁知道我该怎么做?
亲切的问候,
西蒙
您可以简单地获取您的pca
对象并transform
未标记数据的特征。 就像是:
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.