类型错误:不支持的操作数类型(s) /:“列表”和“long”

时间:2022-09-10 22:10:32

I am clustering the data into two clusters then I am going to calculate the mean vector but I got the following error. However, when I change [3,7] to [3,1], the problem will be solved!!! anyone can help me? Thank you in advance

我将数据聚成两个簇,然后我要计算均值向量,但我得到了下面的误差。然而,当我改变[3,7]到[3,1]时,问题就解决了!!!任何人都可以帮我吗?提前谢谢你

Traceback (most recent call last):
  File "ao.py", line 36, in <module>
    (r,d,u)=update(clusters)
  File "ao.py", line 30, in update
    c=np.mean(d, axis=0)
  File "E:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 2716, in
mean
    out=out, keepdims=keepdims)
  File "E:\Python27\lib\site-packages\numpy\core\_methods.py", line 69, in _mean

    ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'list' and 'long'

import numpy as np
clusters = {}
X = np.array([[1,1],[1.5,2],[3,4],[5,7],[3.5,5],[3,7],[3.5,4.5],[5,8],[5,1],[2,3]])
centroid = ([[2.6,2],[4,6.3]])

def a(X):  
    for x in X:     
        z= min([(i[0], np.linalg.norm(x-centroid[i[0]]))  for i in enumerate(centroid)], key=lambda t:t[1])

        try:
            clusters[z].append(x)
        except KeyError:
            clusters[z]=[x]
    return clusters

def update (clusters):
    d=[]
    r=[]
    c=[]
    keys = sorted(clusters.keys())
    for k in keys:
        if k[0]==0:
            r.append(clusters[(k[0],k[1])])       
        else:
            d.append(clusters[(k[0],k[1])])

    c=np.mean(d, axis=0)
    u=np.mean(r,axis=0) 
    return(u,c)


clusters=a(X)
(u,c)=update(clusters)

print "c:",c
print "u:",u

1 个解决方案

#1


2  

np.mean does work with arrays represented as lists, for example:

np。平均值与数组表示为列表,例如:

np.mean([[1],[4],[6]])

However it doesn't work with arrays that do not have a regular shape, e.g.:

然而,它与没有规则形状的数组不兼容,例如:

np.mean([[1, 2],[4],[6]])

That's the problem that you have, because variable d inside function a looks like:

这就是问题所在,因为变量d内函数a是这样的

[[array([ 5. ,  7. ]), array([ 3.,  7.])],
 [array([ 3.5,  5. ])                   ],
 [array([ 3.5,  4.5])                   ],
 [array([ 5. ,  8. ])                   ]]

I think you need to find a better way to organize your data.

我认为你需要找到一个更好的方法来组织你的数据。

#1


2  

np.mean does work with arrays represented as lists, for example:

np。平均值与数组表示为列表,例如:

np.mean([[1],[4],[6]])

However it doesn't work with arrays that do not have a regular shape, e.g.:

然而,它与没有规则形状的数组不兼容,例如:

np.mean([[1, 2],[4],[6]])

That's the problem that you have, because variable d inside function a looks like:

这就是问题所在,因为变量d内函数a是这样的

[[array([ 5. ,  7. ]), array([ 3.,  7.])],
 [array([ 3.5,  5. ])                   ],
 [array([ 3.5,  4.5])                   ],
 [array([ 5. ,  8. ])                   ]]

I think you need to find a better way to organize your data.

我认为你需要找到一个更好的方法来组织你的数据。