Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标准K-均值算法_Python_K Means - Fatal编程技术网

Python 标准K-均值算法

Python 标准K-均值算法,python,k-means,Python,K Means,我试图用python实现标准的k-means算法,但遇到了一个类型错误(TypeError:for/:'Point'和'int'的操作数类型不受支持)。请帮助我理解我做错了什么。下面是我的代码: from math import sqrt class Point(object): def __init__(self, x, y): self.x=x self.y=y def __add__(self,right):

我试图用python实现标准的k-means算法,但遇到了一个类型错误(TypeError:for/:'Point'和'int'的操作数类型不受支持)。请帮助我理解我做错了什么。下面是我的代码:

from math import sqrt

class Point(object):

    def __init__(self, x, y):
        self.x=x
        self.y=y
        
    def __add__(self,right):
        locx=self.x + right.x
        locy=self.y + right.y
        return Point(locx,locy)
    def __sub__(self,right):
        locx=self.x-right.x
        locy=self.y-right.y
        return Point(locx,locy)

    def __mul__(self,right):
        return self.x*right.x + self.y*right.y 

    def distance(self, other):
        return sqrt((self.x-other.x)**2+(self.y-other.y)**2)
    
    def __repr__(self):
        return "Point(%d,%d)"%(self.x,self.y)



class Cluster(object):
    def __init__(self, x, y):
        self.center = Point(x, y)
        self.points = []
    
    def update(self):
        temp=Point(0, 0)
        #print(len(self.points))
        count=0
        for point in self.points:
            count+=1
            temp += point
        print(len(self.points))
        self.center =  temp/count
        self.points = []
    
    def add_point(self, point):
        self.points.append(point)

def compute_result(points):
    points = [Point(*point) for point in points]
    a = Cluster(1,0)
    b = Cluster(-1,0)
    a_old = []
    for _ in range(10000): # max iterations
        for point in points:
            if point.distance(a.center) < point.distance(b.center):
                # add the right point
                a.add_point(point)
            else:
                # add the right point
                b.add_point(point)
        if a_old == a.points:
            break
        a_old = ...
        a.update()
        b.update()
    return [(x, y)] * 2
从数学导入sqrt
类点(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
定义添加(self,右):
locx=自身x+右侧x
locy=自.y+右.y
返回点(locx、locy)
def _; sub ___;(自身,右侧):
locx=self.x-right.x
locy=self.y-right.y
返回点(locx、locy)
def uuu mul uuu(self,右):
返回self.x*right.x+self.y*right.y
def距离(自身、其他):
返回sqrt((self.x-other.x)**2+(self.y-other.y)**2)
定义报告(自我):
返回“点(%d,%d)”%(self.x,self.y)
类群集(对象):
定义初始化(self,x,y):
自中心=点(x,y)
self.points=[]
def更新(自我):
温度=点(0,0)
#打印(透镜(自身点))
计数=0
对于自我点中的点:
计数+=1
温度+=点
打印(透镜(自身点))
self.center=温度/计数
self.points=[]
def添加点(自身,点):
self.points.append(点)
def计算结果(点数):
点=[点(*点)表示点中点]
a=簇(1,0)
b=簇(-1,0)
a_old=[]
对于范围内的(10000):#最大迭代次数
对于点到点:
如果点距离(a中心)<点距离(b中心):
#添加正确的点
a、 添加点(点)
其他:
#添加正确的点
b、 添加点(点)
如果a_old==a.points:
打破
a_old=。。。
a、 更新()
b、 更新()
返回[(x,y)]*2
在“类集群”中,我定义了一个“更新”方法,在该方法中我尝试执行“self.center=temp/count”。这就是我得到错误的原因。 错误如下所示:

<ipython-input-36-56b3a700600d> in update(self)
     42             temp += point
     43         print(len(self.points))
---> 44         self.center =  temp/count
     45         self.points = []
     46 

TypeError: unsupported operand type(s) for /: 'Point' and 'int'
更新中的
(自我)
42温度+=点
43打印(透镜(自身点))
--->44自中心=温度/计数
45个self.points=[]
46
TypeError:/:“Point”和“int”的操作数类型不受支持

似乎
类没有用于将点除以整数的“/”运算符。
Point
是您编写的类吗?如果是这样,您可以添加适当的操作符(这是一个很好的开始)。否则,必须将
对象的每个部分除以整数,并将其存储回相应的成员变量。比如:

temp=点(0,0)
计数=0
对于自我点中的点:
计数+=1
温度+=点
打印(透镜(自身点))
自中心=点(温度x/计数、温度y/计数)
self.points=[]