我不知道这个程序有什么问题(我几周前才开始学习 python)。我编写了这个程序,它可以正常工作,b=np.array([[1,2,2], [3,2,1], [3,1,5]])
但是当我使用不同的数组b = np.array([[1,2,3], [3,2,1], [3,1,5]])
时它会出错。谢谢大家,我修复了第一个错误。
但是,现在当我在课堂上将此文件用作函数时,我遇到了另一个错误,我遇到了下面提到的第二个错误。当我将 dist[s,i] 与 0.0 进行比较时,如果 (dist[s,i] == 0.0): 它需要整个数组。以前它只采用特定的 dist[s,i] 值。
我已经尝试了很多次,并且对此类错误进行了许多解释。如果有人可以提供帮助,我将不胜感激。
该程序计算'a'和'b'中点之间的距离以及点之间的归属(memb)。我知道可能有一个愚蠢的错误,但我在这方面做了很多工作,但仍然空白。
from numpy import *
import numpy as np
a=np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
b = np.array([[1,2,3], [3,2,1], [3,1,5]])
diff = a[newaxis,:,:] - b[:,newaxis,:]
dist=sqrt(np.sum((diff*diff),-1))
(n, d) = shape(a)
k = len(dist)
memb = np.zeros((k, n), dtype=float)
for i in range(n):
count = 0
for s in range(k):
if (dist[s,i] == 0.0):
count = count + 1
if (dist[s,i] != 0.0):
try:
nume = dist[s,i]
temp = 0.0
for j in range(k):
deno = dist[j,i]
temp += (nume / deno)**2
memb[s,i] = 1.0 / temp
except:
deno=0.0
else:
for s in range(k):
if (dist[s,i] == 0.0):
memb[s,i] = 1.0 / count
else:
memb[s,i] = 0.0
print memb
这是错误:
Traceback (most recent call last):
File "C:\python26\ask", line 186, in <module>
main()
File "C:\python26\ask", line 25, in main
memb = membMat(data, k, dist, m)
File "C:\python26\cmean new", line 121, in membMat
if (dist[s][i] == 0.0):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Traceback (most recent call last):
File "C:/Users/ask/kk/pyth", line 25, in <module>
temp += ((nume / deno))
File "C:\python26\lib\warnings.py", line 29, in _show_warning
file.write(formatwarning(message, category, filename, lineno, line))
TypeError: idle_formatwarning_subproc() takes exactly 4 arguments (5 given)