'numpy.float64'对象不可迭代-Meanshift聚类 [英] 'numpy.float64' object is not iterable - meanshift clustering

查看:114
本文介绍了'numpy.float64'对象不可迭代-Meanshift聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python新手在这里.我正在尝试运行此代码,但收到错误消息,指出该对象不可迭代.希望对我做错了一些建议.谢谢.

python newbie here. I am trying to run this code but I get the error message that the object is not iterable. Would appreciate some advice on what I am doing wrong. Thanks.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

temp = pd.read_csv("file.csv", encoding='latin-1')
xy = temp.ix[:,2:6]
X = xy.values


X
array([[             nan,              nan],
   [             nan,              nan],
   [  3.92144000e+00,              nan],
   [  4.42382000e+00,              nan],
   [  4.18931000e+00,   5.61562775e+02],
   [             nan,              nan],
   [  4.33025000e+00,   6.73123391e+02],
   [  6.43775000e+00,              nan],
   [  3.12299000e+00,   2.21886627e+03],
   [             nan,              nan],
   [             nan,              nan]])


from itertools import cycle
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')

class Mean_Shift:
    def __init__(self, radius=4):
        self.radius = radius

    def fit(self, data):
        centroids = {}

        for i in range(len(data)):
            centroids[i] = data[i]

        while True:
            new_centroids = []
            for i in centroids:
                in_bandwidth = []
                centroid = centroids[i]
                for featureset in data:
                    if np.linalg.norm(featureset-centroid) < self.radius:
                        in_bandwidth.append(featureset)

                new_centroid = np.average(in_bandwidth, axis=0)
                new_centroids.append(tuple(new_centroid))

            uniques = sorted(list(set(new_centroids)))

            prev_centroids = dict(centroids)

            centroids = {}
            for i in range(len(uniques)):
                centroids[i] = np.array(uniques[i])

            optimized = True

            for i in centroids:
                if not np.array_equal(centroids[i], prev_centroids[i]):
                    optimized = False

                if not optimized:
                    break

            if optimized:
                break

        self.centroids = centroids

    def predict(self,data):
        pass 


clf = Mean_Shift()
clf.fit(X)

centroids = clf.centroids

plt.scatter(X[:,0],X[:,1],s=50)

for c in centroids:
plt.scatter(centroids[c][0], centroids[c][1], color = 'k', marker='*', s=150)

plt.show()

这是我得到的错误代码:

Here is the error code i get:

/Users/carla/anaconda/lib/python3.5/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-e13932b6e72d> in <module>()
 50 
 51 clf = Mean_Shift()
---> 52 clf.fit(X)
 53 
 54 centroids = clf.centroids

<ipython-input-11-e13932b6e72d> in fit(self, data)
 22 
 23                 new_centroid = np.average(in_bandwidth, axis=0)
---> 24                 new_centroids.append(tuple(new_centroid))
 25 
 26             uniques = sorted(list(set(new_centroids)))

TypeError: 'numpy.float64' object is not iterable

推荐答案

new_centroid = np.average(in_bandwidth, axis=0)

正在为new_centroid分配标量,然后您尝试使用tuple(scalar),这会引发错误.

Is assigning a scalar to new_centroid then you are trying to tuple(scalar) which is throwing the error.

tuple(2.)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-4406f9e676cf> in <module>()
----> 1 tuple(2.)

TypeError: 'float' object is not iterable

这篇关于'numpy.float64'对象不可迭代-Meanshift聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆