# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

"""
HIGH DIMENSIONAL ANALYSIS
Programming Exercise: 01
Name: <NAME>
Student Code: 18110150
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('room-temperature.csv')

data

series = pd.read_csv('room-temperature.csv',header=0, index_col=0, parse_dates=True, squeeze=True)
series.plot(subplots=True, figsize=(6,6))
plt.show()

date,FL,FR = data["Date"].values,data["FrontLeft"].values,data["FrontRight"].values
BL,BR = data["BackLeft"].values,data["BackRight"].values
date.shape,FL.shape,FR.shape,BL.shape,BR.shape

# # Implementing PCA

# We have PCA step by step:
#
# 1. Read dataset & preprocessing mean/variance.
# 2. Compute the covariance matrix
# 3. Compute eigenvals & eigenvectors of cov_mat
# 4. Sort from biggest to smallest and choosing k eigenvectors with the largest eigenvalues
# 5. Project onto the new vectors.

# **Step 1: Computing the d-dimensional mean vector**
#
# (Because, We have "Date" column representing time, so we don't need data to reduce the dimension, we will use "Date" column when we plot So we need to reduce 4 dimensions into 1 dimension)

import statistics as stat
all_feature = np.stack([FL,FR,BL,BR],axis=1)
all_feature.shape

# +
mean_FT=stat.mean(FL)     #data["FrontLeft"].values
mean_FR=stat.mean(FR)     #data["FrontRight"].values
mean_BL=stat.mean(BL)     #data["BackLeft"].values
mean_BR=stat.mean(BR)     #data["BackRight"].values


mean_vector = np.array([[mean_FT],[mean_FR],[mean_BL],[mean_BR]])
print('Mean Vector:\n', mean_vector)
# -

# **Step 2: Compute the covariance Matrix**
#
# (using the in-built numpy.cov() function)
#
# $$Cov(X, Y) = Σ ( X_i - Xm) ( Y_i - Ym ) / N$$
#
# $$Cov (X) = E (E(X - Xm)’ * E(X - Xm))$$
#

cov_mat = np.cov([FL,FR,BL,BR])
print('Covariance Matrix:\n', cov_mat)

# **Step 3: Computing eigenvectors and corresponding eigenvalues**

# +
#eig_val = eigenvalues , eig_vec = eigenvectors
eig_val, eig_vec = np.linalg.eig(cov_mat)

#Print eigenvectors and eigenvalues
for i in range(len(eig_val)):
    eigvec = eig_vec[:,i].reshape(1,4).T
    
    print('Eigenvector {}: \n{}'.format(i+1, eigvec))
    print('Eigenvalue {} from covariance matrix: {}'.format(i+1, eig_val[i]))
    
    print(40 * '-')
# -

# **Step 4:**
#
# **Step 4.1: Sorting the eigenvectors by decreasing eigenvalues**
#
# We started with the goal to reduce the dimensionality of our feature space, i.e., projecting the feature space via PCA onto a smaller subspace, where the eigenvectors will form the axes of this new feature subspace. 

for ev in eig_vec:
    np.testing.assert_array_almost_equal(1.0, np.linalg.norm(ev))
    #instead of 'assert' because of rounding errors

# Rank the eigenvectors from highest to lowest corresponding eigenvalue and choose the top k eigenvectors.

# +
# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = [(np.abs(eig_val[i]), eig_vec[:,i]) for i in range(len(eig_val))]

# Sort the (eigenvalue, eigenvector) tuples from high to low
eig_pairs.sort(key=lambda x: x[0], reverse=True)

# Visually confirm that the list is correctly sorted by decreasing eigenvalues
for i in eig_pairs:
    print(i[0])
# -

# **Step 4.2: Choosing k eigenvectors with the largest eigenvalues **
#
# For our simple example, where we are reducing a 4-dimensional feature space to a 1-dimensional feature subspace, we are combining the two eigenvectors with the highest eigenvalues to construct our d×k-dimensional eigenvector matrix W

matrix_w = np.hstack((eig_pairs[0][1].reshape(4,1)))
print('Matrix W:\n', matrix_w)
print(matrix_w.shape)
matrix_w=matrix_w.reshape(4,1)
print(matrix_w.shape)

# **Step 5: Transforming the samples onto the new subspace**
#
# In the last step, we use the $1×4$-dimensional matrix $W$ that we just computed to transform our samples onto the new subspace via the equation 
# $$y=W^T×x$$

print(all_feature.shape)
print(matrix_w.shape)
transformed = np.matmul(all_feature,matrix_w)
transformed=np.absolute(transformed)
print(transformed.shape)

transformedDF = pd.DataFrame(data = transformed, columns = ['Transformed'])
transformedDF.head()

FinalDf = pd.concat([data[['Date']],transformedDF], axis = 1)
FinalDf.head()

PC = FinalDf["Transformed"].values
x=date
y=PC
plt.scatter(x,y)
fig = plt.figure()
fig.suptitle('Time series plot for transformed of temperature data')
plt.xlabel('time')
plt.ylabel('')
plt.plot(x,y)
plt.show()

#
# 3.After observing these time series plot in (1.), how many latent variables should we use for the PCA algorithm to keep the most meaningful information? Apply the PCA for the temperature data by your chosen number of latent variables.
# ---

# I think, we need keep all variables should we use for the PCA algorithm. Because, in the room, the temperature in each corner is not the same, although there are similar timelines but there are still changes. Or you can choose from 2 to 3 random components.
#
# The content below I choose 2 feature FrontLeft and BackLeft, indicating the left side of the room
#
# You can change the feature, and run again under program.

#
# Implementing PCA with Scikit-Learn
# ---

import numpy as np  
import pandas as pd
from sklearn.preprocessing import StandardScaler

# **1. Preprocessing data **
#
# Because PCA performs best with a normalized feature set. I will perform standard scalar normalization to normalize our feature set.

# features=['FrontLeft','FrontRight','BackLeft','BackRight']
features=['FrontLeft','BackLeft']

# +
# Separating out the features
x_pca = data.loc[:, features].values

# Separating out the target
y_pca = data.loc[:,['Date']].values

# Standardizing the features
x_pca = StandardScaler().fit_transform(x_pca)
# -

# Data after standard:

print(features)
print(x_pca)

# **PCA projection to 2D**
#
# x_pca has 2 columns, So I  reduce from 2 dimensions into 1 dimensions.
# The new components are just the two main dimensions of variation.

from sklearn.decomposition import PCA
pca = PCA(n_components=1)
principalComponents = pca.fit_transform(x_pca)
# print(principalComponents)
principalDf = pd.DataFrame(data = principalComponents, columns = ['PComponent'])
principalDf.head()

finalDf = pd.concat([data[['Date']],principalDf], axis = 1)
finalDf.head()

PC = finalDf["PComponent"].values
x=date
y=PC
plt.scatter(x,y)
fig = plt.figure()
fig.suptitle('Time series plot for PrincipalComponents of temperature data')
plt.xlabel('time')
plt.ylabel('Principal Components')
plt.plot(x,y)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered