【问题标题】:Data structure for KMeans clustering using Pandas DataFrames使用 Pandas DataFrames 进行 KMeans 聚类的数据结构
【发布时间】:2019-11-22 07:55:03
【问题描述】:

我目前正在处理一些科学数据,我正在尝试对其执行聚类任务,但由于数据格式的原因,我收到了一个值错误。这是 [170 行 x 7 列] 中的两个 Pandas DataFrame。

我尝试过转置数据、格式化为列表以及 numpy 数组。我在代码中显示的格式来自此处找到的解决方案:ValueError: cannot copy sequence with size 5 to array axis with dimension 2

#x is the y distance
x = np.empty(7, dtype = object)
x[:] = [distance_lC, distance_fC]

#y is the speed.
y = np.empty(7, dtype = object)
y[:] = [speed_lC, speed_fC]

cell_kmeans = KMeans(n_clusters = 4).fit_predict(y)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatterplot(cell_kmeans)
plt.show()

输出应该给出集群。但我有以下值错误:“ValueError: setting an array element with a sequence。”

【问题讨论】:

  • 我认为当你这样做时y[:] = [speed_lC, speed_fC] 你的y 将成为一个列表,scikit 不会喜欢它!你能告诉我们speed_lC, speed_fC 是什么吗? (使用type(speed_lC)
  • 嗨!是的,如前所述,我使用的两个变量是 pandas DataFrames。所以使用type 输出:pandas.core.frame.DataFrame

标签: python numpy sklearn-pandas


【解决方案1】:

改用pandas.concat 连接数据帧:

y = pandas.concat([speed_lC, speed_fC])

【讨论】:

  • 谢谢,我之前尝试过使用 pd.concat 但没有使用常规括号......现在效果很好。因此,当拥有这些 DataFrame 大小时,使用 numpy 并不好!
猜你喜欢
  • 1970-01-01
  • 2015-02-05
  • 2019-11-27
  • 2011-12-08
  • 2019-01-06
  • 2012-02-06
  • 1970-01-01
  • 2017-11-23
  • 2017-10-05
相关资源
最近更新 更多