<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Unsupervised Analysis of Days of Week
Treating crossings each day of features to learn about the relationships between various days
Step1:  Get Data
Step2:  Principal Components Analysis
Step3:  Unsupervised Clustering
Step4:  Comparing with Day of Week
Step5:  0-4 weekdays
5, 6 weekend
Analyzing Outliers
The following points are weekdays with a holiday-like pattern
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn')
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
Explanation: Unsupervised Analysis of Days of Week
Treating crossings each day of features to learn about the relationships between various days
End of explanation
from jupyterworkflow.data import get_fremont_data
data = get_fremont_data()
pivoted = data.pivot_table('Total', index=data.index.time, columns = data.index.date)
pivoted.plot(legend=False,alpha=0.01)
Explanation: Get Data
End of explanation
X = pivoted.fillna(0).T.values
X.shape
X2 =PCA(2, svd_solver='full').fit_transform(X)
X2.shape
import matplotlib.pyplot as plt
plt.scatter(X2[:, 0], X2[:, 1])
Explanation: Principal Components Analysis
End of explanation
gmm = GaussianMixture(2).fit(X)
labels = gmm.predict(X)
np.unique(labels)
plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow')
plt.colorbar()
fig, ax = plt.subplots(1, 2, figsize=(14, 6))
pivoted.T[labels == 0].T.plot(legend=False, alpha=0.1, ax=ax[0])
pivoted.T[labels == 1].T.plot(legend=False, alpha=0.1, ax=ax[1])
ax[0].set_title('Purple Cluster')
ax[1].set_title('Red Cluster')
Explanation: Unsupervised Clustering
End of explanation
pd.DatetimeIndex(pivoted.columns).dayofweek
dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek, cmap='rainbow')
plt.colorbar()
Explanation: Comparing with Day of Week
End of explanation
dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels==1) & (dayofweek<5)]
Explanation: 0-4 weekdays
5, 6 weekend
Analyzing Outliers
The following points are weekdays with a holiday-like pattern
End of explanation
<END_TASK>