<jupyter_start><jupyter_text>Unsupervised Analysis of Days of WeekTreating crossings each day as features to learn about the relationships between various days.<jupyter_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<jupyter_output><empty_output><jupyter_text>Get data<jupyter_code>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);<jupyter_output><empty_output><jupyter_text>Principal component Analysis<jupyter_code>#pivoted.index # pasikeitė grafikas perkopijavus duomenis, nes 12h, nebe 24h
#data.index
#import numpy as np
#np.unique(data.index.time)
#pivoted.shape # valandos, dienos
#pivoted.T.shape # transponuota
X = pivoted.fillna(0).T.values # missing values are zeros
X.shape
#PCA(2).fit(X)
X2 = PCA(2).fit_transform(X) # buvo klaida, kurią visą video taisė per github
X2.shape # 2D
plt.scatter(X2[:, 0], X2[:, 1])<jupyter_output><empty_output><jupyter_text>Unsupervised Clustering<jupyter_code># identifying clusters
gmm = GaussianMixture(2).fit(X)
labels = gmm.predict(X)
labels
plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow')
plt.colorbar();
# what is happening in these clusters
# portion of data

fig, ax = plt.subplots(1, 2, figsize=(14, 6))

pivoted.T[labels == 1].T.plot(legend=False, alpha=0.1, ax=ax[0]); # working days
pivoted.T[labels == 0].T.plot(legend=False, alpha=0.1, ax=ax[1]); # free days

ax[0].set_title('Red cluster')
ax[1].set_title('Purple cluster');<jupyter_output><empty_output><jupyter_text>Comparing with Day of Week<jupyter_code># convert date do days of week
dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek, cmap='rainbow')
plt.colorbar();<jupyter_output><empty_output><jupyter_text>Analyzing OutliersThe following points are weekdays with holiday-like pattern<jupyter_code>dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels == 0) & (dayofweek < 5)]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13184306569343066, 'fraction_numerical': 0.019616788321167884, 'mean_word_length': 7.5, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '130765', 'license': 'MIT', 'n_tokens_mistral': 860, 'n_tokens_neox': 822, 'n_words': 176, 'path': 'karpickaite5.ipynb', 'repo_name': 'boruzhele/analize5', 'size': '2192'}