In [1]:
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
from jupyterworkflow.data import get_fremont_data
%matplotlib inline
In [2]:
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
In [3]:
data = get_fremont_data()
pivoted = data.pivot_table("Total", index = data.index.time, columns = data.index.date)
In [4]:
pivoted.plot(legend=False, alpha=0.01);
In [5]:
X = pivoted.values.T
In [6]:
X2 = PCA(2, svd_solver='full').fit_transform(X)
In [7]:
plt.scatter(X2[:,0], X2[:,1]);
In [8]:
gm = GaussianMixture(2)
In [10]:
gm.fit(X)
Out[10]:
In [11]:
labels = gm.predict(X)
In [12]:
plt.scatter(X2[:,0], X2[:,1], c=labels,cmap='cool')
plt.colorbar();
In [13]:
fig, ax = plt.subplots(1,2, figsize =[14,6])
pivoted.T[labels==1].T.plot(legend=False, alpha=0.01, ax=ax[0])
pivoted.T[labels==0].T.plot(legend=False, alpha=0.01, ax=ax[1])
ax[0].set_title("Commuter pattern")
ax[1].set_title("Holiday pattern")
fig.suptitle("Patterns comparison");
In [14]:
day_of_week = pd.to_datetime(pivoted.columns).dayofweek
In [15]:
pivoted.columns[(labels==0) & (day_of_week<5)]
Out[15]:
In [16]:
plt.scatter(X2[:,0], X2[:,1], c=day_of_week, cmap='GnBu')
plt.colorbar();
In [ ]: