-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcaExample.py
More file actions
33 lines (27 loc) · 914 Bytes
/
Copy pathpcaExample.py
File metadata and controls
33 lines (27 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 1 17:39:47 2021
@author: maxvondanwitz
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import PCA
normalizedData = np.load('normalizedData.npy')
# PCA
pca = PCA(n_components=1).fit(normalizedData)
transformedData = PCA(n_components=1).fit_transform(normalizedData)
transformedCoords = transformedData * pca.components_
# Linear Regression as reference
fit = np.polyfit(normalizedData[:,0], normalizedData[:,1], deg=1)
# Plot data, PCA and lin reg.
plt.scatter(normalizedData[:,0],normalizedData[:,1])
plt.plot(normalizedData[:,0], fit[0] * normalizedData[:,0] + fit[1], color='black')
plt.scatter(transformedCoords[:,0],transformedCoords[:,1])
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('pcaTransformed.pdf')
plt.show()