I am struggling to implement my succesfull PCA.
This is how my PCA plot looks like:
I retrieved this from accelerometer data (x, y, z) which I have observed and labeled with A, S and 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. 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). 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. 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'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.