跳过NaN值以获取距离

时间:2019-08-26 08:46:41

标签: python pandas numpy distance valueerror

我的数据集的一部分(实际上是我的数据集大小(106,1800)):

df =

    1           1.1     2           2.1     3           3.1     4           4.1     5           5.1
0   43.1024     6.7498  NaN         NaN     NaN         NaN     NaN         NaN     NaN         NaN
1   46.0595     1.6829  25.0695     3.7463  NaN         NaN     NaN         NaN     NaN         NaN
2   25.0695     5.5454  44.9727     8.6660  41.9726     2.6666  84.9566     3.8484  44.9566     1.8484
3   35.0281     7.7525  45.0322     3.7465  14.0369     3.7463  NaN         NaN     NaN         NaN
4   35.0292     7.5616  45.0292     4.5616  23.0292     3.5616  45.0292     6.7463  NaN         NaN

根据汤姆的回答,我现在能做什么:

  • 我手动写了第1行第2行,如p和q值:

p =

[[45.1024,7.7498],[45.1027,7.7513],[45.1072,7.7568],[45.1076,7.7563]]

q =

[[45.0595,7.6829],[45.0595,7.6829],[45.0564,7.6820],[45.0533,7.6796],[45.0501,7.6775]]

之后:

__all__ = ['frdist']


def _c(ca, i, j, p, q):

    if ca[i, j] > -1:
        return ca[i, j]
    elif i == 0 and j == 0:
        ca[i, j] = np.linalg.norm(p[i]-q[j])
    elif i > 0 and j == 0:
        ca[i, j] = max(_c(ca, i-1, 0, p, q), np.linalg.norm(p[i]-q[j]))
    elif i == 0 and j > 0:
        ca[i, j] = max(_c(ca, 0, j-1, p, q), np.linalg.norm(p[i]-q[j]))
    elif i > 0 and j > 0:
        ca[i, j] = max(
            min(
                _c(ca, i-1, j, p, q),
                _c(ca, i-1, j-1, p, q),
                _c(ca, i, j-1, p, q)
            ),
            np.linalg.norm(p[i]-q[j])
            )
    else:
        ca[i, j] = float('inf')

    return ca[i, j]

之后:

def frdist(p, q):

    # Remove nan values from p
    p = np.array([i for i in p if np.any(np.isfinite(i))], np.float64)
    q = np.array([i for i in q if np.any(np.isfinite(i))], np.float64)

    len_p = len(p)
    len_q = len(q)

    if len_p == 0 or len_q == 0:
        raise ValueError('Input curves are empty.')

    # p and q will no longer be the same length
    if len(p[0]) != len(q[0]):
        raise ValueError('Input curves do not have the same dimensions.')

    ca = (np.ones((len_p, len_q), dtype=np.float64) * -1)

    dist = _c(ca, len_p-1, len_q-1, p, q)
    return(dist)

frdist(p, q)

有效。但是如何将p和q应用于整个数据集?不是通过逐行选择?

最后,我需要获得106 to 106对角线的0对称矩阵

2 个答案:

答案 0 :(得分:2)

删除NaN个值

简单明了:

p = p[~np.isnan(p)]


计算整个数据集的弗雷谢特距离

最简单的方法是使用SciPy的成对距离计算pdist。它需要对m维数组进行n个观察,因此我们需要在reshape(-1,2)内使用frdist重塑行数组。 pdist返回压缩的(上三角)距离矩阵。我们根据需要使用squareform来获得m x m对角线的0对称矩阵。

import pandas as pd
import numpy as np
import io
from scipy.spatial.distance import pdist, squareform

data = """    1           1.1     2           2.1     3           3.1     4           4.1     5           5.1
0   43.1024     6.7498  NaN         NaN     NaN         NaN     NaN         NaN     NaN         NaN
1   46.0595     1.6829  25.0695     3.7463  NaN         NaN     NaN         NaN     NaN         NaN
2   25.0695     5.5454  44.9727     8.6660  41.9726     2.6666  84.9566     3.8484  44.9566     1.8484
3   35.0281     7.7525  45.0322     3.7465  14.0369     3.7463  NaN         NaN     NaN         NaN
4   35.0292     7.5616  45.0292     4.5616  23.0292     3.5616  45.0292     6.7463  NaN         NaN
"""
df = pd.read_csv(io.StringIO(data), sep='\s+')

def _c(ca, i, j, p, q):

    if ca[i, j] > -1:
        return ca[i, j]
    elif i == 0 and j == 0:
        ca[i, j] = np.linalg.norm(p[i]-q[j])
    elif i > 0 and j == 0:
        ca[i, j] = max(_c(ca, i-1, 0, p, q), np.linalg.norm(p[i]-q[j]))
    elif i == 0 and j > 0:
        ca[i, j] = max(_c(ca, 0, j-1, p, q), np.linalg.norm(p[i]-q[j]))
    elif i > 0 and j > 0:
        ca[i, j] = max(
            min(
                _c(ca, i-1, j, p, q),
                _c(ca, i-1, j-1, p, q),
                _c(ca, i, j-1, p, q)
            ),
            np.linalg.norm(p[i]-q[j])
            )
    else:
        ca[i, j] = float('inf')

    return ca[i, j]

def frdist(p, q):

    # Remove nan values and reshape into two column array
    p = p[~np.isnan(p)].reshape(-1,2)
    q = q[~np.isnan(q)].reshape(-1,2)

    len_p = len(p)
    len_q = len(q)

    if len_p == 0 or len_q == 0:
        raise ValueError('Input curves are empty.')

    # p and q will no longer be the same length
    if len(p[0]) != len(q[0]):
        raise ValueError('Input curves do not have the same dimensions.')

    ca = (np.ones((len_p, len_q), dtype=np.float64) * -1)

    dist = _c(ca, len_p-1, len_q-1, p, q)
    return(dist)

print(squareform(pdist(df.values, frdist)))

结果:

[[ 0.         18.28131545 41.95464432 29.22027212 20.32481187]
 [18.28131545  0.         38.9573328  12.59094238 20.18389517]
 [41.95464432 38.9573328   0.         39.92453004 39.93376923]
 [29.22027212 12.59094238 39.92453004  0.         31.13715882]
 [20.32481187 20.18389517 39.93376923 31.13715882  0.        ]]


无需重新发明轮子

Fréchet距离计算已由similaritymeasures提供。因此,以下内容将为您提供与上述相同的结果:

from scipy.spatial.distance import pdist, squareform
import similaritymeasures

def frechet(p, q):
    p = p[~np.isnan(p)].reshape(-1,2)
    q = q[~np.isnan(q)].reshape(-1,2)
    return similaritymeasures.frechet_dist(p,q)

print(squareform(pdist(df.values, frechet))) 

答案 1 :(得分:0)

我认为您唯一需要做的更改就是在frdist函数内部,首先从nan中删除p值。这样就必须消除pq的长度相同的条件,但是我认为应该没问题,因为您自己说p有1个值,而q有1800个值。

def frdist(p, q):

    # Remove nan values from p
    p = np.array([i for i in p if np.any(np.isfinite(i))], np.float64)
    q = np.array(q, np.float64)

    len_p = len(p)
    len_q = len(q)

    if len_p == 0 or len_q == 0:
        raise ValueError('Input curves are empty.')

    # p and q no longer have to be the same length
    if len(p[0]) != len(q[0]):
        raise ValueError('Input curves do not have the same dimensions.')

    ca = (np.ones((len_p, len_q), dtype=np.float64) * -1)

    dist = _c(ca, len_p-1, len_q-1, p, q)
    return(dist)

然后给出:

frdist(p, q)
1.9087938076177846