%matplotlib inline
import matplotlib.pyplot as plt

import os
import numpy as np
import pandas as pd
pd.set_option('max_columns', None)
# pd.set_option('display.float_format', lambda x: '%.2f' % x)  

import seaborn as sns
sns.set_style('whitegrid',{'font.sans-serif':['SimHei','Arial']})


from jupyterworkflow.data import get_fremont_data

data = get_fremont_data()

data.head()

data.plot();

data.resample('W').sum().plot();

data.resample('M').sum().plot();

"""
Explanation: IPython & Jupyter
Relevant resources:
Cyrille Rossant 法国神经学家

Learning IPython for Interactive Computing and Data Visualization:Beginner-level introduction to Python for data analysis  
IPython Interactive Computing and Visualization Cookbook:Advanced-level book covers a wide range of methods for data science with Python

Reproducible Data Analysis in Jupyter
Coding time:
- Writing & test 40%
- Refactoring 40%
- Style & doc 20%
Part 1: Loading and Visualizing Data
Relevant resources:


Fremont Bridge Bike Counter: the website where you can explore the data


A Whirlwind Tour of Python: Jake Vanderplas book introducing the Python programming language, aimed at scientists and engineers.


Python Data Science Handbook: Jake Vanderplas book introducing Python's data science tools, including an introduction to the IPython, Pandas, and Matplotlib tools used here.
End of explanation
"""


data.resample('W').sum().plot();

data.resample('D').sum().rolling(365).sum().plot();

ax = data.resample('D').sum().rolling(365).sum().plot();
ax.set_ylim(0, None);

data.groupby(data.index.time).mean().plot();

pivoted = data.pivot_table('Total', index=data.index.time, columns=data.index.date)
pivoted.iloc[:5, :3]

pivoted.plot(legend=False, alpha=0.01);

"""
Explanation: Part 2: Further Data Exploration
I do some slightly more sophisticated visualization with the data, using matplotlib and pandas.
Relevant Resources:

Pivot Tables Section from the Python Data Science Handbook
End of explanation
"""


%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-dark')

import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture

"""
Explanation: Part 9: Further Data Exploration: PCA and GMM
Apply unsupervised learning techniques to the data to explore what we can learn from it
对一周不同时段（工作日、节假日和双休日）的骑车人数进行聚类分析
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: 获取数据
End of explanation
"""


X = pivoted.fillna(0).T.values
X.shape

X2 = PCA(2, svd_solver='full').fit_transform(X)

X2.shape

plt.scatter(X2[:, 0], X2[:, 1]);

"""
Explanation: 主成分分析
End of explanation
"""


gmm = GaussianMixture(2).fit(X)

labels = gmm.predict(X)
labels

plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow');
plt.colorbar();

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

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('Pupple Cluster')
ax[1].set_title('Red Cluster');

"""
Explanation: 无监督分类
End of explanation
"""


dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
dayofweek

"""
Explanation: 工作日 VS 节假日
End of explanation
"""


plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek, cmap='rainbow');
plt.colorbar();

"""
Explanation: 下图是双休日与节假日的自行车车数
End of explanation
"""


dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels==1) & (dayofweek<5)]

"""
Explanation: 异常值分析
End of explanation
"""


