<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 crossing each day as features to learn about the relatinships between various days.
Step1:  Downloading Data
We'll start by downloading the data (available on seattle.gov).
Step2:  Principal Component Analysis
Step3:  Comparing with Day of Week
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import matplotlib.pyplot as plt
Explanation: Unsupervised Analysis of Days of Week
Treating crossing each day as features to learn about the relatinships between various days.
End of explanation
from urllib import request
FREMONT_URL = 'https://data.seattle.gov/api/views/65db-xm6k/rows.csv?accessType=DOWNLOAD'
request.urlretrieve(FREMONT_URL, 'Fremont.csv')
# magic function to show the content of the file
%more Fremont.csv
import pandas as pd
df = pd.read_csv('Fremont.csv')  # use read_csv to load the data into dataframe
df.head()
#  Let's see the type of the data
df.dtypes
# change the Date column to datetime data type
df['Date'] = pd.to_datetime(df['Date'])
df.head()
df.dtypes
# Set the index to Date
df.set_index('Date', inplace=True)
df.head()
df.apply(lambda x: sum(x.isnull()))
# clear the data by delete the non-numeric
df.dropna(inplace=True)
df.apply(lambda x: sum(x.isnull()))
df.columns
df.plot()
df.resample('W').sum().plot()
df.columns=['West', 'East']
df.resample('w').sum().plot()
# To see whether there is any annual trend of the number of rides
df.resample('D').sum().rolling(365).sum().plot()  
# each point is the sum of the number of rides in the previuos 365 days
# The y coordinate is not from 0
ax = df.resample('D').sum().rolling(365).sum().plot()
ax.set_ylim(0, None)
# DateimeIndex.time return numpy array of datetime.time, the time part of the Timestamps
df.groupby(df.index.time).mean().plot()
# plot the average of rides at each hours of the day
# Create the pivoted table to investigate the pattern in each day
df['Total'] = df['West'] + df['East']
pivoted = df.pivot_table(values='Total', index=df.index.time, columns=df.index.date)
pivoted.head()
pivoted.shape
# delete the date with non-numeric
pivoted.dropna(axis=1, inplace=True)
pivoted.shape
pivoted.plot(legend=False)
# add transparent parameter alpha
pivoted.plot(legend=False, alpha=0.01)
Explanation: Downloading Data
We'll start by downloading the data (available on seattle.gov).
End of explanation
# Get X with hours as mearsurement and date as observations
X = pivoted.T.values
X.shape
X
from sklearn.decomposition import PCA
X2 = PCA(2, svd_solver='full').fit_transform(X)
X2
X2.shape
plt.scatter(X2[:, 0], X2[:, 1])
# use cluster algorithm Gaussian mixture model
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(2)
gmm.fit(X)
labels = gmm.predict(X)
labels
# plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow')
# plt.colorbar()
plt.scatter(X2[:, 0], X2[:, 1], c=labels)
plt.colorbar()
labels
# so labels == 1 represents the weekday
pivoted.T[labels == 1].T.plot(legend=False, alpha=0.01)
# labels == 0 represents the weekend or holiday
pivoted.T[labels == 0].T.plot(legend=False, alpha=0.1)
Explanation: Principal Component Analysis
End of explanation
pd.DatetimeIndex(pivoted.columns)
# The DatetimeIndex.dayof week gives the day of the week
dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
dayofweek
# Then we plot the color of the weekday
plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek)
plt.colorbar() 
# grab the day in label 0 which is not weekend
dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels == 0) & (dayofweek < 5)]
Explanation: Comparing with Day of Week
End of explanation
<END_TASK>