Python 使用(arr.transpose()==arr.all()的意外对称测试结果

Python 使用(arr.transpose()==arr.all()的意外对称测试结果,python,numpy,symmetric,Python,Numpy,Symmetric,我有 对S的对称测试产生True,但对S\u hat,则返回False。我不能把我的头绕在这个上面 [编辑]和数据定义如下: temp = np.power(np.sum(S,1),-0.5) S_hat = np.diagflat(temp).dot(S).dot(np.diagflat(temp)) 您很可能看到舍入错误。请尝试使用以下方法,而不是严格的相等性测试: 根据您的具体数据大小调整公差。特别是,对于示例数据,atol似乎应该设置为0(您的数据范围从1到1e-14)。什么是S?你能

我有

S
的对称测试产生
True
,但对
S\u hat
,则返回
False
。我不能把我的头绕在这个上面

[编辑]和数据定义如下:

temp = np.power(np.sum(S,1),-0.5)
S_hat = np.diagflat(temp).dot(S).dot(np.diagflat(temp))

您很可能看到舍入错误。请尝试使用以下方法,而不是严格的相等性测试:


根据您的具体数据大小调整公差。特别是,对于示例数据,
atol
似乎应该设置为0(您的数据范围从1到1e-14)。

什么是
S
?你能解释一下这个测试应该测试什么吗?有没有可能有一个输入数据的例子?@AGNGazer:我已经编辑了这篇文章,把S包括在内。至于标题,我只是想把它作为一种表示。我在帖子里说的是考试。
from math import pi

def make_moons(n):
    """Create a 'two moons' dataset with n feature vectors, 
        and 2 features per vector."""

    assert n%2==0, 'n must be even'
    # create upper moon
    theta = np.linspace(-pi / 2, pi / 2, n/2)
    # create lower moon
    x = np.r_[np.sin(theta) - pi / 4, np.sin(theta)]
    y = np.r_[np.cos(theta), -np.cos(theta) + .5]
    data = np.c_[x, y]
    # Add some noise
    data = data + 0.03 * np.random.standard_normal(data.shape)

    # create labels
    labels = np.r_[np.ones((n//2, 1)), -np.ones((n//2, 1))]
    labels = labels.ravel().astype(np.int32)

    return data,labels

S = np.zeros((100,100));
sig = 0.09;
from scipy.spatial.distance import pdist, squareform, cdist
B = pdist(data);
C = squareform(B);
S = np.exp(-C/sig);
np.allclose(S_hat.T, S_hat, rtol=1e-7, atol=1e-8)