简体   繁体   English

无法在 python function 内使用 Kmeans 集群

[英]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.

相关问题 使用kmeans python进行集群和分区的字典 - Dict of cluster and partition with kmeans python Python Kmeans聚类顺序进行分类 - Python Kmeans cluster order for classification 在Python中使用Kmeans后确定集群的大小 - Determining the size of cluster after Kmeans in Python 为 Python 中的每次迭代绘制 KMeans 聚类中心 - Plotting the KMeans Cluster Centers for every iteration in Python 在 dataframe 中使用 groupby 进行 Kmeans,并在 python 中获取集群 - Kmeans with groupby in dataframe and get cluster in python 如何在python中使用KMeans对时间序列进行聚类 - How to cluster a time series using KMeans in python 在python中使用kmeans sklearn集群数据点 - Cluster datapoints using kmeans sklearn in python python中'sklearn.cluster'包中'KMeans'函数中的术语'随机状态'是什么意思 - What is meant by the term ‘random-state’ in 'KMeans' function in package 'sklearn.cluster' in python python / sklearn-在执行kmeans之后如何获取集群和集群名称 - python/sklearn - how to get clusters and cluster names after doing kmeans Python Kmeans 打印每个簇中单词的绝对频率 - Python Kmeans Print absolute frequency of words in each cluster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM