In [1]:
%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
In [2]:
from jupyterworkflow.data import get_fremont_data
data = get_fremont_data()
In [3]:
pivoted = data.pivot_table('Total', index=data.index.time, columns = data.index.date)
pivoted.plot(legend=False,alpha=0.01)
Out[3]:
In [4]:
X = pivoted.fillna(0).T.values
X.shape
Out[4]:
In [5]:
X2 =PCA(2, svd_solver='full').fit_transform(X)
X2.shape
Out[5]:
In [6]:
import matplotlib.pyplot as plt
plt.scatter(X2[:, 0], X2[:, 1])
Out[6]:
In [7]:
gmm = GaussianMixture(2).fit(X)
labels = gmm.predict(X)
np.unique(labels)
Out[7]:
In [8]:
plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow')
plt.colorbar()
Out[8]:
In [9]:
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')
Out[9]:
In [10]:
pd.DatetimeIndex(pivoted.columns).dayofweek
Out[10]:
In [11]:
dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
In [12]:
plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek, cmap='rainbow')
plt.colorbar()
Out[12]:
In [13]:
dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels==1) & (dayofweek<5)]
Out[13]:
What's up with Feb 6, 2017? [snow storm] (http://www.seattletimes.com/seattle-news/weather/weather-service-predicts-3-to-6-inches-of-snow-in-seattle-area/)
In [ ]: