我有两个2D点集A和B 我想在B为每个点找到A的第一个最近邻居。 但是,我正处理不确定点(即一个点有一个均值(二维向量)和一个2 * 2协方差矩阵)。 因此,我想使用Mahalanobis距离,但在scikit-learn (例如)中,我不能为每个点传递协方差矩阵,因为它期望单个协方差 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
在Dijkstra的算法中,我对最近邻居的决心不知所措。 我得到如下奇怪的结果首先这是我的网络文件的内容,代表7个节点之间的距离:
(不包括第一列中的数字1-7)
现在我的代码:
> infinity = 1000000 invalid_node = -1
> startNode = 0
>
> #Values to assign to each node class Node:
> distFromSource = infinity
> previous = invalid_node
> visited = False
>
> #read in all network nodes
> #node = the distance values between nodes def network():
> f = open ('network.txt', 'r')
> theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
> #print theNetwork
>
> return theNetwork
>
> #for each node assign default values
> #populate table with default values def populateNodeTable():
> nodeTable = []
> index = 0
> f = open('network.txt', 'r')
> for line in f:
> node = map(int, line.split(','))
> nodeTable.append(Node())
>
> #print "The previous node is " ,nodeTable[index].previous
> #print "The distance from source is " ,nodeTable[index].distFromSource
> index +=1
> nodeTable[startNode].distFromSource =
> 0
>
> return nodeTable
>
> #find the nearest neighbour to a particular node def
> nearestNeighbour(nodeTable,
> theNetwork):
> nearestNeighbour = []
> nodeIndex = 0
> for node in nodeTable:
> if node != 0 and Node.visited == False:
> nearestNeighbour.append(nodeIndex)
> nodeIndex +=1
> print nearestNeighbour
>
> return nearestNeighbour
>
> def tentativeDistance (theNetwork,
> nodeTable, nearestNeighbour):
> shortestPath = []
> for nodeIndex in nearestNeighbour:
> currentDistance = nearestNeighbour[] + startNode
> print currentDistance
> ## if currentDistance < Node.distFromSource:
> ## theNetwork[Node].previous = nodeIndex
> ## theNetwork[Node].length = nodeIndex
> ## theNetwork[Node].visited = True;
> ## shortestPath.append(indexNode)
> ## nodeIndex +=1
> ## print shortestPath
>
> currentNode = startNode
>
> if __name__ == "__main__":
> nodeTable = populateNodeTable()
> theNetwork = network()
> nearestNeighbour(nodeTable, theNetwork)
> tentativeDistance(theNetwork, nodeTable, nearestNeighbour)
所以,我试着查看网络函数提供的值,在populateNodeTable函数中将所有节点设置为'visited = false',然后通过查看前一个函数中提供的值来确定节点的最近邻居,尽管我得到了此错误消息:
> Traceback (most recent call last):
> File "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 77, in <module>
> tentativeDistance(theNetwork, nodeTable, nearestNeighbour) File
> "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 51, in tentativeDistance
> for nodeIndex in nearestNeighbour: TypeError: 'function' object is not iterable
当我运行我的网络功能时,我得到了这个输出:
[[0, 2, 4, 1, 6, 0, 0], [2, 0, 0, 0, 5, 0, 0], [4, 0, 0, 0, 5, 5, 0], [1, 0, 0, 0, 1, 1, 0], [6, 5, 0, 1, 0, 5, 5], [0, 0, 5, 1, 5, 0, 0], [0, 0, 0, 0, 5, 0, 0]]
到目前为止,非常好 - 当我运行populateNodeTable函数和我的网络函数时,我得到了这个输出:
> The previous node is -1
The distance from source is 1000000 # happens 7 times#
>
另外,这很好 - 除了上述功能之外,执行myNeighbour函数后的输出是:
[0, 1, 2, 3, 4, 5, 6]
这个输出是错误的,是我的问题开始的地方
此外,当我运行我的所有代码包括tentativeDistance时,我收到此错误:
> for nodeIndex in nearestNeighbour:
TypeError: 'function' object is not iterable
我为这篇文章长篇大论而道歉,我很沮丧,我无法掌握似乎是基本的功能
您将方法nearestNeighbour
传递给tentativeDistance
而不是方法的结果。
这是问题所在
tentativeDistance(theNetwork, nodeTable, nearestNeighbour)
应该
x = nearestNeighbour(nodeTable, theNetwork)
tentativeDistance(theNetwork, nodeTable, x)
看一下错误,你会看到代码试图迭代一个不可迭代的对象。 这在Python for - in -
syntax中是隐含的。
您可能还会考虑重命名变量名称或函数名称以避免混淆。 无论如何,这都是一个容易犯的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.