简体   繁体   English

python中不满足的条件

[英]Condition not satisfying in python

I am having the following problem. 我遇到以下问题。 Thought the differences are less than 0.0001 the condition is not getting satisfying. 以为差异小于0.0001,条件不能令人满意。 I am facing a problem with abs(beta - beta1).any() condition. 我面临abs(beta - beta1).any()条件的问题。

    alpha = -29.18835001947976
    prev_alpha = -29.188337321421681
    beta = np.matrix([[-0.26220145],[ 8.37991712]])
    beta1 = np.matrix([[-0.26220149],[ 8.37991514]])
    print(alpha - prev_alpha)
    print (beta - beta1)
    epsilon = 0.0001
    if ((abs(alpha - prev_alpha) <= epsilon) & (abs(beta - beta1).any() <= epsilon)):
        print 'x'


    print (-1.26980580788e-05 <= 0.001)
True
    print (abs(beta - beta1).all() <0.001)
False

Since the condition is not satisfying, the condition is not working. 由于条件不满足,因此条件不起作用。 I want to know what can be done to make the 'x' print. 我想知道可以做些'x'印刷。 I want to get into the condition if all the values in the matrix are almost the same as previous one. 如果矩阵中的所有值都与上一个几乎相同,我想进入条件。 If it same, 'x' needs to be printed. 如果相同,则需要打印“ x”。 I have taken 0.0001 as condition to determine it is almost same. 我已将0.0001作为确定它几乎相同的条件。 Can anybody help me with this. 谁能帮我这个忙。

Update : 更新:

    print (any(beta - beta1) <0.001)
False

print(alpha - prev_alpha)
print (beta - beta1)

-1.26980580788e-05
[[  4.00000000e-08]
 [  1.98000000e-06]]

abs(beta - beta1).any() is a boolean. abs(beta - beta1).any()是布尔值。 If it is True, it is the same as 1, which is always bigger than epsilon. 如果为True,则等于1,始终大于epsilon。

What you want is probably more like any(difference < epsilon) 您想要的可能更像any(difference < epsilon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM