import pandas as pd
data = pd.read_csv ('Fremont.csv', index_col='Date', parse_dates=True)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a9c2cfa74ab4f9264b0b248b8eca8512.png)
%matplotlib inline
data.resample('w').sum().plot();
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d563bc57cd6f1895cc9f0bf176799a6f.png)
import matplotlib.pyplot as plt
data.groupby(data.index.time).mean().plot();
plt.xticks(rotation=45)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/53bcf66868170d79acbe709a6e816fed.png)
data.columns =['West', 'East']
data ['Total'] =data['West']+data['East']
pivoted = data.pivot_table('Total', index=data.index.time, columns=data.index.date)
pivoted.iloc[:5,:5]
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0b2c725efd0f78d106f5f300b775f7cd.png)
pivoted.plot(legend=False, alpha =0.01);
plt.xticks(rotation=45)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6c5fe62a867f57ba45482a04ce6fbad2.png)
X = pivoted.fillna(0).T.values
from sklearn.decomposition import PCA
X2 = PCA(2).fit_transform(X)
plt.scatter(X2[:,0],X2[:,1])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0ea8f90778133016a59d6de32d0ad3de.png)
from sklearn.mixture import GaussianMixture
gmm =GaussianMixture(2)
gmm.fit(X)
labels = gmm.predict(X)
plt.scatter(X2[:,0],X2[:,1], c=labels, cmap='rainbow')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e1f213998090415d435aed8ca6cf2818.png)
先生成一些数据 ,对比Kmeans和GMM效果
from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs(n_samples=800, centers=4, random_state=11)
plt.scatter(X[:, 0], X[:, 1]);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e4d8819193f4dbafb50ead3f8c7bc0d.png)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bcd487ad744db3686c453a9c1fce991a.png)
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=4).fit(X)
labels = gmm.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis');
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/65d38bb177c67eb6695ca089690ab338.png)
rng = np.random.RandomState(13)
X_stretched = np.dot(X, rng.randn(2, 2))
kmeans = KMeans(n_clusters=4, random_state=1)
kmeans.fit(X_stretched)
y_kmeans = kmeans.predict(X_stretched)
plt.scatter(X_stretched[:, 0], X_stretched[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f489fef927185f809a3f33ec55bc99dc.png)
gmm = GaussianMixture(n_components=4)
gmm.fit(X_stretched)
y_gmm = gmm.predict(X_stretched)
plt.scatter(X_stretched[:, 0], X_stretched[:, 1], c=y_gmm, s=50, cmap='viridis')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/002be53db70f097093c41b6cbdf9bd64.png)