0

我从哪里得到尾随的 0 或 9?我在每一步都检查了没有出现舍入问题,并且得到了正确的结果。但是,当我将此数字添加到图表时,会出现舍入问题。

我的完整代码如下:

from __future__ import division
from math import sqrt
import networkx as nx
import numpy as np
from decimal import Decimal

n=4   #n is the nummber of steps in the graph.
a = np.array([ 1.1656,  1.0125,  0.8594])

g=nx.DiGraph() #I initiate the graph

#f2 checks for equal nodes and removes them
def f2(seq): 
    checked = []
    for e in seq:
        if (e not in checked):
            checked.append(e)
    return np.asarray(checked)

root = np.array([1])
existing_nodes = np.array([1])
previous_step_nodes = np.array([1])
nodes_to_add =np.empty(0)
clean = np.array([1])

for step in range(1,n):
    nodes_to_add=np.empty(0)
    for values in previous_step_nodes:
        nodes_to_add = np.append(nodes_to_add,values*a)

    print "--------"
    print "*****nodes to add ****" + str(f2(np.round(nodes_to_add,4)))
    print "clean = " + str(clean) + "\n"
    #Up to here, the code generates the nodes I will need 

    # This for loop makes the edges and adds the nodes.
    for node in clean:
        for next_node in np.round(node*a,4):
            print str(node ) + "     "  + str( next_node)
            g.add_edge(np.round(node,4), np.round(next_node,4))
#            g.add_edge(Decimal(np.round(node,4)).quantize(Decimal('1.0000')), Decimal(np.round(next_node,4)).quantize(Decimal('1.0000')))

    previous_step_nodes = f2(nodes_to_add)
    clean = f2(np.round(previous_step_nodes,4))
#    g.add_nodes_from(clean)

    print "\n step" + str(step) + " \n"
    print " Current Step :" + "Number of nodes = " + str(len(f2(np.round(previous_step_nodes,4))))
    print clean

print "How many nodes are there ? " +str(len(g.nodes()))

这段代码工作并打印出一个非常简洁的图形描述,这正是我想要的。但是,当我打印节点列表时,要检查图形是否仅包含我需要的节点数,我得到:

How many nodes are there ? 22
[1, 0.88109999999999999, 1.0143, 1.038, 0.74780000000000002, 
1.1801999999999999, 1.3755999999999999, 1.0142, 0.8609, 
0.88100000000000001, 0.85940000000000005, 1.1656,
1.1950000000000001, 1.0125, 1.5835999999999999, 1.0017, 
0.87009999999999998, 1.1676,
0.63480000000000003, 0.73860000000000003, 1.3586, 1.0251999999999999]

这显然是一个使我的程序无用的问题。0.88109999999999999 和 0.88100000000000001 是同一个节点。

因此,在检查了几天的 stackoverflow 之后,我得出的结论是,解决该问题的唯一方法是使用 Decimal()。所以,我更换了:

g.add_edge(np.round(node,4), np.round(next_node,4))

g.add_edge(Decimal(np.round(node,4)).quantize(Decimal('1.0000')), 
           Decimal(np.round(next_node,4)).quantize(Decimal('1.0000')))

然而,结果并不是我所期望的:因为

0.88109999999999999 = 0.8811
0.88100000000000001 =0.8810, 

所以 Python 仍然认为它们是不同的数字。

理想情况下,我不希望使用 Decimal() 使代码复杂化,并且希望截断小数点,以便 0.88109999999999999 = 0.88100000000000001 = 0.8810 但我不知道如何解决这个问题。

感谢您的回复,我已经更新了我的代码。我接受了使用 f2 的建议:

def f2(seq): 
    near_equal = lambda x, y: abs(x - y) < 1.e-5
    checked = []
    for e in seq:
        if all([not near_equal(e, x) for x in checked]):
            checked.append(e)
    return np.asarray(checked)

我删除了所有 numpy.round() 因为如果我可以删除“相似”的节点,那么我根本不需要任何舍入。

但是,python仍然无法区分节点:

g.nodes() 打印出 23 个节点,而应该只有 20 个:(注意:我在更改容差级别 1.e-5 时尝试过,但没有得到不同的结果)

有多少个节点?23

[0.63474091729864457, 0.73858020442900385, 0.74781245698436638,
 0.85940689107605128, 0.86088399667008808, 0.86088399667008819,
 0.87014947721450187, 0.88102634567968308, 0.88102634567968319,
 1, 1.00171875, 1.0125, 1.0142402343749999, 1.02515625,
 1.0379707031249998, 1.1655931089239486, 1.1675964720799117,
 1.180163022785498, 1.1949150605703167, 1.358607295570996,
 1.3755898867656333, 1.3755898867656335, 1.5835833014513552]

这是因为:0.86088399667008808、0.86088399667008819;0.88102634567968308、0.88102634567968319 和 1.3755898867656333、1.3755898867656335 仍被视为不同的节点。

完整代码:

from __future__ import division
from math import sqrt
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

mu1 = 0.05; sigma1= 0.25
n=4

a0=1
a1 = 1 + mu1/n + sigma1*sqrt(3)/sqrt(2*n)
a2 = 1 + mu1/n
a3 = 1 + mu1 /n - sigma1*sqrt(3)/sqrt(2*n)
a = np.array([a1,a2,a3])

print " a = " + str(a)

g=nx.DiGraph() #I initiate the graph

def f2(seq): 
    near_equal = lambda x, y: abs(x - y) < 1.e-5
    checked = []
    for e in seq:
        if all([not near_equal(e, x) for x in checked]):
            checked.append(e)
    return np.asarray(checked)

root = np.array([1])
existing_nodes = np.array([1])
previous_step_nodes = np.array([1])
nodes_to_add =np.empty(0)
clean = np.array([1])

print "________________This Makes the Nodes____________________________________"
for step in range(1,n):
    nodes_to_add=np.empty(0)
    for values in previous_step_nodes:
        nodes_to_add = np.append(nodes_to_add,values*a)
    print "--------"    
    print "*****nodes to add ****" + str(f2(nodes_to_add))
    print "clean = " + str(clean) + "\n"
    #Up to here, the code generates the nodes I will need 

    # This for loop makes the edges and adds the nodes.
    for node in clean:
        for next_node in node*a:
            print str(node ) + "     "  + str( next_node)
            g.add_edge(node, next_node)

    previous_step_nodes = f2(nodes_to_add)
    clean = f2(previous_step_nodes)
#    g.add_nodes_from(clean)

    print "\n step" + str(step) + " \n"
    print " Current Step :" + "Number of nodes = " + str(len(f2(previous_step_nodes)))
    print clean

print "______________End of the Nodes_________________________________"
print "How many nodes are there ? " +str(len(g.nodes()))
print sorted(g.nodes())

结果:

有多少个节点?23 [0.63474091729864457, 0.73858020442900385, 0.74781245698436638, 0.85940689107605128, 0.86088399667008808, 0.86088399667008819, 0.87014947721450187, 0.88102634567968308, 0.88102634567968319, 1, 1.00171875, 1.0125, 1.0142402343749999, 1.02515625, 1.0379707031249998, 1.1655931089239486, 1.1675964720799117, 1.180163022785498, 1.1949150605703167, 1.358607295570996, 1.3755898867656333, 1.3755898867656335, 1.5835833014513552]

4

1 回答 1

1

依赖浮点数之间的精确相等通常不是一个好主意,因为用于生成数字的同一组输入可能会由于不同的浮点表示、数学运算顺序等而产生不同的结果。

除非您正在处理非常接近的节点,否则您可以f2使用以下内容修改您的函数(您可能希望将容差设为变量):

def f2(seq): 
    near_equal = lambda x, y: abs(x - y) < 1.e-8
    checked = []
    for e in seq:
        if all([not near_equal(e, x) for x in checked]):
            checked.append(e)
    return np.asarray(checked)

请注意,如果浮点数完全相等,则获取删除重复项的列表的更简单方法是

nodes_without_dupes = list(set(nodes_to_add)) 
于 2013-04-23T16:44:09.010 回答