2015-07-09 15 views
1

Я хочу визуализировать данные csv в кластере.Как я могу визуализировать свои данные csv в кластере

Это мои данные в формате CSV. (https://github.com/soma11soma11/EnergyDataSimulationChallenge/blob/challenge2/soma11soma/challenge2/analysis/Soma/total_watt.csv)

Для вашего Infomation. Я мог визуализировать данные csv в 3D-графике.

И это мой код.

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d 



MY_FILE = 'total_watt.csv' 

df = pd.read_csv(MY_FILE, parse_dates=[0], header=None, names=['datetime', 'consumption']) 

df['date'] = [x.date() for x in df['datetime']] 
df['time'] = [x.time() for x in df['datetime']] 

pv = df.pivot(index='time', columns='date', values='consumption') 

# to avoid holes in the surface 
pv = pv.fillna(0.0) 

xx, yy = np.mgrid[0:len(pv),0:len(pv.columns)] 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

surf=ax.plot_surface(xx, yy, pv.values, cmap='jet', cstride=1, rstride=1) 

fig.colorbar(surf, shrink=0.5, aspect=10) 

dates = [x.strftime('%m-%d') for x in pv.columns] 
times = [x.strftime('%H:%M') for x in pv.index] 

ax.set_title('Energy consumptions Clusters', color='lightseagreen') 
ax.set_xlabel('time', color='darkturquoise') 
ax.set_ylabel('date(year 2011)', color='darkturquoise') 
ax.set_zlabel('energy consumption', color='darkturquoise') 
ax.set_xticks(xx[::10,0]) 
ax.set_xticklabels(times[::10], color='lightseagreen') 
ax.set_yticks(yy[0,::10]) 
ax.set_yticklabels(dates[::10], color='lightseagreen') 

ax.set_axis_bgcolor('black') 

plt.show() 


#Thanks for reading! Looking forward to the Skype Interview. 

И это график, который я получил от этого кода.

enter image description here

Я думаю, что я должен изменить некоторые пункты этого кода, чтобы сгруппировать данные в три группы: с высоким, средним и низким потреблением энергии.

Изображение Я хочу получить от кластеризации данных, как это. (2D, 3colours.)

enter image description here

к-средства ????? следует ли использовать? ..

+1

Пожалуйста, измените название на что-то более значимое, а не комментарии – EdChum

+0

Спасибо Я редактировал! –

+0

Я тоже хотел бы иметь такие приятные кластеры моих данных ... – Moritz

ответ

2

Вот результат использования KMeans.

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d 
from sklearn.cluster import KMeans 


MY_FILE = '/home/Jian/Downloads/total_watt.csv' 

df = pd.read_csv(MY_FILE, parse_dates=[0], header=None, names=['datetime', 'consumption']) 

df['date'] = [x.date() for x in df['datetime']] 
df['time'] = [x.time() for x in df['datetime']] 

stacked = df.pivot(index='time', columns='date', values='consumption').fillna(0).stack() 


# do unsupervised clustering 
# ============================================= 
estimator = KMeans(n_clusters=3, random_state=0) 
X = stacked.values.reshape(len(stacked), 1) 
cluster = estimator.fit_predict(X) 

# check the mean value of each cluster 
X[cluster==0].mean() # Out[53]: 324.73175293698534 
X[cluster==1].mean() # Out[54]: 6320.8504071851467 
X[cluster==2].mean() # Out[55]: 1831.1473140192766 


# plotting 
# ============================================= 

fig, ax = plt.subplots(figsize=(10, 8)) 
x = stacked.index.labels[0] 
y = stacked.index.labels[1] 
ax.scatter(x[cluster==0], y[cluster==0], label='mean: {}'.format(X[cluster==0].mean()), c='g', alpha=0.8) 
ax.scatter(x[cluster==1], y[cluster==1], label='mean: {}'.format(X[cluster==1].mean()), c='r', alpha=0.8) 
ax.scatter(x[cluster==2], y[cluster==2], label='mean: {}'.format(X[cluster==2].mean()), c='b', alpha=0.8) 
ax.legend(loc='best') 

enter image description here

+0

Большое спасибо! Но это не работает Сообщение об ошибке; (DataVizProj) Сома-Suzuki: Сома Suzuki $ питон 4.clusters.py Traceback (самый последний вызов последнего): Файл "4.clusters.py", строка 5, в из sklearn.cluster импортных KMeans ImportError: Нет модуля с именем sklearn.cluster –

+0

какая у вас версия sklearn? мой 0.16.1 –

+0

Я заново установил sklean, и он работал успешно! Большое спасибо!! –

Смежные вопросы