了解 ECG 节拍之间的马哈拉诺比斯距离

问题描述 投票:0回答:0

我有一个数据框,其中每一行代表一个 200 维的心电图节拍。我有很多嘈杂的心电图节拍(行),所以我想计算每个节拍的马氏距离及其平均节拍(每行和平均行)并识别距离较远的节拍(行)。这是我的代码:

df_cut = dfs[0].iloc[:,:200].fillna(0)
df_selected = df_cut
mean_vec = np.mean(df_selected, axis=0)
df_diff = df_selected - mean_vec

cov_mat = np.cov(df_diff.values.T)
det = np.linalg.det(cov_mat)
if det == 0:
    print('Matrix is singular')
else:
    print('Matrix is not singular')
# std_devs = np.sqrt(np.diagonal(cov_mat))
# cor_mat = cov_mat / np.outer(std_devs, std_devs)
zero_vec = np.zeros(mean_vec.shape)
# Calculate the inverse of the covariance matrix
# If the covariance matrix is singular or not well-conditioned, add a small positive number to the diagonal
if np.linalg.cond(cov_mat) < 1/sys.float_info.epsilon:
    inv_cov_mat = inv(cov_mat)
else:
    inv_cov_mat = inv(cov_mat + np.eye(cov_mat.shape[0]) * 1e-8)

# Calculate Mahalanobis distance for each row
df_diff['Mahalanobis_Distance'] = df_diff.apply(lambda row: distance.mahalanobis(row, zero_vec, inv_cov_mat), axis=1)

我得到的马哈洛比斯距离绘制在下面的直方图中:

根据我的理解,马哈洛诺比斯距离表示该点与我的均值相距多少标准差。但是在这里,当我看到大约 10 或更多的距离值时,我很困惑我的代码是否有错误?如果我认为这是错误的,我该如何解释结果

python numpy signal-processing mahalanobis
© www.soinside.com 2019 - 2024. All rights reserved.