我有一个名为age-average的csv文件。 它有4列userId,平均值,年龄,计数。我想取平均值和年龄来集中我的用户。这是我的代码:
import csv
import pandas as pd
import numpy as np
from sklearn.cluster import MeanShift
import matplotlib.pyplot as plt
df = pd.read_csv('/age-average.csv')
csv_file = open('/age-average.csv')
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
for row in csv_reader:
userID,average,age,count = row
plt.scatter(age,average)
plt.show()
它显示了图表。到目前为止,每个方面都可以。但是,当我必须使用ms.fit()函数时,我总是会收到错误:
ms = MeanShift()
ms.fit(df[['average','age']])
labels = ms.labels
cluster_centers = ms.cluster_centers_
print(cluster_centers)
n_clusters_ = len(np.unique(labels))
print("The number of estimated clusters ", n_clusters_)
colors = 10*['r.','g.','c.','k.','y.','m.']
print(colors)
for i in range(len(age,average)):
plt.plot(int(age[i]), float(average[i]), colors[labels[i]], markersize = 10)
plt.scatter(cluster_centers[:,0],cluster_centers[:,1],
marker="x", s= 150, linewidths = 5, zorder=10)
plt.show()
我应该在ms.fit()内写什么代替df [[' average',#39; age']]。任何人都可以有想法吗?我有点迷惑不解了。 谢谢!
我收到了像
这样的错误回溯(最近一次呼叫最后):文件" example.py",第20行,in labels = ms.labels AttributeError:' MeanShift'对象没有属性'标签'