[英]Cannot use Kmeans Cluster inside a python function
As above - I'm trying to create a function for clustering specific data types and displaying them.如上所述 - 我正在尝试创建一个 function 用于聚类特定数据类型并显示它们。
The function looks a bit like this at the moment, function目前看起来有点像这样,
def cluster(inputData):
variable_s= inputData.groupby(['x','z', 'c'])['w'].sum().unstack()
## 4 Clusters
model = cluster.MiniBatchKMeans(n_clusters=5)
model.fit(variable_s.fillna(0))
variable_s['kmeans_4'] = model.predict(variable_s.fillna(0))
## 8 Clusters
model = cluster.KMeans(n_clusters=8)
model.fit(variable_s.fillna(0))
variable_s['kmeans_8'] = model.predict(variable_s.fillna(0))
## Looking at hourly distribution.
variable_s_Hourly = variable_s.reset_index(1, inplace=True)
variable_s_Hourly['hour'] = variable_s_Hourly.index.hour
return variable_s, variable_s_Hourly
it uses它用
from sklearn import cluster
to do the clustering, and it's giving me an error like this,进行聚类,它给了我这样的错误,
AttributeError: 'function' object has no attribute 'MiniBatchKMeans'
Any clues on solving this issue?关于解决这个问题的任何线索? I would have thought the function would be fine as long as the library is imported into the file itself - this is in jupyter notebook:)
我原以为 function 只要将库导入文件本身就可以了-这是在jupyter笔记本中:)
Cheers!干杯!
The function name ("cluster") shadows the import. function 名称(“集群”)会影响导入。 Change the function name to solve it.
更改function名称即可解决。
Alternatively, you can give the import an alias:或者,您可以为导入指定别名:
from sklearn import cluster as clstr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.