Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 沿一个轴的矩阵相似性_Python_Matlab_Matrix_Correlation - Fatal编程技术网

Python 沿一个轴的矩阵相似性

Python 沿一个轴的矩阵相似性,python,matlab,matrix,correlation,Python,Matlab,Matrix,Correlation,Hi拥有2D矩阵,希望计算沿Y轴的相似性度量 例如,以下矩阵应产生0: [1, 0, 0, 0] [0, 1, 0, 0] [0, 0, 1, 0] [0, 0, 0, 1] 而这一个应该产生1: [0, 1, 1, 0] [0, 1, 1, 0] [0, 1, 1, 0] [0, 1, 1, 0] 在这些示例中,我在矩阵中使用了二进制值,但实际上它们是介于0和1之间的浮点值。矩阵要大得多,而且有噪音——计算速度必须非常快,因为每次实验我都要计算大量矩阵 现在我正在做一个随机PCA,保留第一

Hi拥有2D矩阵,希望计算沿Y轴的相似性度量

例如,以下矩阵应产生0:

[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
而这一个应该产生1:

[0, 1, 1, 0]
[0, 1, 1, 0]
[0, 1, 1, 0]
[0, 1, 1, 0]
在这些示例中,我在矩阵中使用了二进制值,但实际上它们是介于0和1之间的浮点值。矩阵要大得多,而且有噪音——计算速度必须非常快,因为每次实验我都要计算大量矩阵

现在我正在做一个随机PCA,保留第一个分量作为相似性的度量。然而,它有点慢,我有一种感觉,这是矫枉过正。欢迎任何建议

使用axis=0的all可以得到逻辑结果,然后重新应用到矩阵:

例如:

mx
matrix([[1, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]])

mx1
matrix([[0, 1, 1, 0],
        [0, 1, 1, 0],
        [0, 1, 1, 0]])
适用:

# use .A to convert to array to do the logical calculation
np.matrix(mx.A * mx.all(axis=0).A)
matrix([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]])
mx1也是如此:

np.matrix(mx1.A * mx1.all(axis=0).A) 
matrix([[0, 1, 1, 0],
        [0, 1, 1, 0],
        [0, 1, 1, 0]])
使用axis=0的all可获得逻辑结果,然后重新应用于矩阵:

例如:

mx
matrix([[1, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]])

mx1
matrix([[0, 1, 1, 0],
        [0, 1, 1, 0],
        [0, 1, 1, 0]])
适用:

# use .A to convert to array to do the logical calculation
np.matrix(mx.A * mx.all(axis=0).A)
matrix([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]])
mx1也是如此:

np.matrix(mx1.A * mx1.all(axis=0).A) 
matrix([[0, 1, 1, 0],
        [0, 1, 1, 0],
        [0, 1, 1, 0]])

这里真正的问题是如何定义相似性

我假设你把相似性定义为相等行的比例。也就是说,如果随机选取两行,那么这两行相等的概率是多少?这个定义是我能想到的最简单的,它符合您的示例所期望的结果

如果这确实是您想要的,那么可以按如下方式轻松计算,其中A表示数据矩阵:

d = squeeze(all(bsxfun(@eq, A, permute(A, [3 2 1])), 2)); %// test all pairs
    %// of rows for equality
result = (sum(d(:))-size(d,1))/(numel(d)-size(d,1)); %// compute average, but
    %// removing similarity of each row with itself

这里真正的问题是如何定义相似性

我假设你把相似性定义为相等行的比例。也就是说,如果随机选取两行,那么这两行相等的概率是多少?这个定义是我能想到的最简单的,它符合您的示例所期望的结果

如果这确实是您想要的,那么可以按如下方式轻松计算,其中A表示数据矩阵:

d = squeeze(all(bsxfun(@eq, A, permute(A, [3 2 1])), 2)); %// test all pairs
    %// of rows for equality
result = (sum(d(:))-size(d,1))/(numel(d)-size(d,1)); %// compute average, but
    %// removing similarity of each row with itself

元素相乘将给出相似性的度量。哪种编程语言?对行求和,第一种情况下得到[1,1,1],第二种情况下得到[0,4,4,0]。然后可以进行规范化,例如1映射为0,4映射为1。元素乘法将给出相似性度量。哪种编程语言?对行求和,第一种情况下得到[1,1,1],第二种情况下得到[0,4,4,0]。然后可以进行规范化,例如1映射为0,4映射为1。我同意相似性的定义是问题所在。平均数是一个合理的选择。我同意相似性的定义是一个问题。平均计数是一个合理的选择。