gpt4 book ai didi

python - 如何将我的 csv 数据可视化到集群中

转载 作者:行者123 更新时间:2023-12-01 04:38:21 25 4
gpt4 key购买 nike

我想将我的 csv 数据可视化到集群中。

这是我的 csv 数据。( https://github.com/soma11soma11/EnergyDataSimulationChallenge/blob/challenge2/soma11soma/challenge2/analysis/Soma/total_watt.csv )

供您引用。我可以将 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

k-意思是???我应该使用吗?..

最佳答案

这是使用 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

关于python - 如何将我的 csv 数据可视化到集群中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31315088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com