K-Means 聚类 - 但出现不受支持的操作数类型错误
K-Means Clustering - but getting an unsupported operand type(s) error
我有一个来自三个不同来源的两个变量之间相关性的数据框。所以我试图用三个质心执行 k-means 聚类。我没有在此代码中包含数据框,因此假设我已将两列数据添加到变量 cdf 中。但是我不断收到错误消息。你能发现吗?
def dis(v,w):
#Sum of square of distances from x and y to ensure positive values, then square root to find actual value
return ((w[1]-v[1])**2 + (w[0]-v[0])**2)**.5
def assign(p1,p2,p3,d):
gps={1:[],2:[],3:[]} #Three empty arrays.
for i in dt:
if dis(i,p1)<dis(i,p2) and dis(i,p1)<dis(i,p3): #If closest to first point, put in first group.
gps[1].append(i)
elif dis(i,p2)<dis(i,p1) and dis(i,p2)<dis(i,p3): #If closest to second point, put in second group.
gps[2].append(i)
else: #If closest to third point, put in third group.
gps[3].append(i)
return gps
p1=[3,3]
p2=[4,4]
p3=[5,5]
gps=assign(p1,p2,p3,cdf)
最后一行代码给我错误。
TypeError: unsupported operand type(s) for -: 'int' and 'str'
它指向我的距离函数的 return 语句。但我找不到问题所在。提前致谢。
编辑以添加整个回溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-42-d4f398e2f10f> in <module>()
----> 1 gps=assign(p1,p2,p3,cdf)
1 frames
<ipython-input-40-1132f55271a6> in dis(v, w)
2 def dis(v,w):
3 #Sum of square of distances from x and y to ensure positive values, then square root to find actual value
----> 4 return ((w[1]-v[1])**2 + (w[0]-v[0])**2)**.5
5
6 #Average array of points function
TypeError: unsupported operand type(s) for -: 'int' and 'str'
看起来您的数组 w
和 v
可能有字符串值(其中之一或两者)。如果你有像 ["1", "2" ...]
或 ["3.4", "2.1", ...]
这样的值,你可以这样做:
w1 = float(w[1])
v1 = float(v[1])
w0 = float(w[0])
v0 = float(v[0])
((w1-v1)**2 + (w0-v0)**2)**.5
我有一个来自三个不同来源的两个变量之间相关性的数据框。所以我试图用三个质心执行 k-means 聚类。我没有在此代码中包含数据框,因此假设我已将两列数据添加到变量 cdf 中。但是我不断收到错误消息。你能发现吗?
def dis(v,w):
#Sum of square of distances from x and y to ensure positive values, then square root to find actual value
return ((w[1]-v[1])**2 + (w[0]-v[0])**2)**.5
def assign(p1,p2,p3,d):
gps={1:[],2:[],3:[]} #Three empty arrays.
for i in dt:
if dis(i,p1)<dis(i,p2) and dis(i,p1)<dis(i,p3): #If closest to first point, put in first group.
gps[1].append(i)
elif dis(i,p2)<dis(i,p1) and dis(i,p2)<dis(i,p3): #If closest to second point, put in second group.
gps[2].append(i)
else: #If closest to third point, put in third group.
gps[3].append(i)
return gps
p1=[3,3]
p2=[4,4]
p3=[5,5]
gps=assign(p1,p2,p3,cdf)
最后一行代码给我错误。
TypeError: unsupported operand type(s) for -: 'int' and 'str'
它指向我的距离函数的 return 语句。但我找不到问题所在。提前致谢。
编辑以添加整个回溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-42-d4f398e2f10f> in <module>()
----> 1 gps=assign(p1,p2,p3,cdf)
1 frames
<ipython-input-40-1132f55271a6> in dis(v, w)
2 def dis(v,w):
3 #Sum of square of distances from x and y to ensure positive values, then square root to find actual value
----> 4 return ((w[1]-v[1])**2 + (w[0]-v[0])**2)**.5
5
6 #Average array of points function
TypeError: unsupported operand type(s) for -: 'int' and 'str'
看起来您的数组 w
和 v
可能有字符串值(其中之一或两者)。如果你有像 ["1", "2" ...]
或 ["3.4", "2.1", ...]
这样的值,你可以这样做:
w1 = float(w[1])
v1 = float(v[1])
w0 = float(w[0])
v0 = float(v[0])
((w1-v1)**2 + (w0-v0)**2)**.5