# PCA



```python
import pandas
# For lots of great things.
import numpy as np
# To make our plots.
import matplotlib.pyplot as plt
%matplotlib inline
# Because sympy and LaTeX make
# everything look wonderful!
from sympy import *
init_printing(use_latex=True)
from IPython.display import display
# We will use this to check our implementation...
from sklearn.decomposition import PCA
import keras
from sklearn.preprocessing import StandardScaler
```


```python
## load in data and split into x train and y train
## data = np.array(pandas.read_csv("./comp_new_trainingdata.csv", header=0))
data = np.array(pandas.read_csv("./training_noavg.csv", header=0))
## bring in loc 0 -1 test data for PCA
data1 = np.array(pandas.read_csv("./test1.csv", header=0))
data2 = np.array(pandas.read_csv("./test2.csv", header=0))
data3 = np.array(pandas.read_csv("./test3.csv", header=0))
## Have to drop all teh rows that have nan values because they will not help with net
## clean out rows with nan values
data = data[~np.isnan(data).any(axis=1)]
data1 = data1[~np.isnan(data1).any(axis=1)]
data2 = data2[~np.isnan(data2).any(axis=1)]
data3 = data3[~np.isnan(data3).any(axis=1)]

print(data[:8])
print(data.shape)

data = np.vstack((data,data1,data2,data3))
print(data[:8])
data.shape
```


```python
# vectors AND class labels...
X = data[:,0:8] # 0 thru 30
Y = data[:,8] # 30

scaler = StandardScaler()
# standardize X .. will mean center data
X = scaler.fit_transform(X)

# Pretty-print with display()!
display(X.shape)
display(Y.shape)
display(Matrix(np.unique(Y)).T)
display(X[0:8])
```


```python
U,S,V = np.linalg.svd(X,full_matrices=True)

# Percent variance accounted for
plt.plot(100.0*S/np.sum(S))
plt.ylabel('% Var')
plt.xlabel('Singular Value')
plt.show()
```


```python
# Variance accounted for in the first two principal components
100.0*(S[0]+S[1])/np.sum(S)
```


```python
# Scale the singular vectors, resulting in a rotated form of our mean-centered data
D = np.zeros([X.shape[0],X.shape[1]])
np.fill_diagonal(D,S)
Xrotated = np.dot(U,D)
# Extract just the first two principal components!
PCs = Xrotated[:,0:2]
PCs.shape
```


```python
# The x and y values come from the two
# Principal Components and the colors for
# each point are selected based on the
# corresponding class for each point...
plt.scatter(PCs[:,0],PCs[:,1],
color=[['red','green','blue','orange'][i] for i in Y.astype(int)])
plt.xlabel("PC1")
plt.ylabel("PC2")
plt.show()
```

The data suggest that we have some clear descision boundries.  The orange represents the testing dat from all three locations, we can see that it fits the class groupings.  A simple MLP with linear activation functions will not work for our data, we should use a RELU or sigmoid instead along with a large hidden layer.  It remains to be seen how well the network will be able to generalize.

123434b29481ce681ec0f01dd3095167d06a8959
ipynb
Jupyter Notebook
PCA.ipynb
holypolarpanda7/S19-team2-project
09b51f07849e3288dfa4ba91cf5d8d13909e35e2
MIT
PCA.ipynb
holypolarpanda7/S19-team2-project
09b51f07849e3288dfa4ba91cf5d8d13909e35e2
MIT
PCA.ipynb
holypolarpanda7/S19-team2-project
09b51f07849e3288dfa4ba91cf5d8d13909e35e2
MIT
Qwen/Qwen-72B
1. YES
2. YES