Python 使用多索引从距离矩阵创建数据帧
我有两种类型的对象——F和p,上面有标记,我想计算属于不同类别的每个标记之间的距离,然后构建一个数据框架,其中来自不同类别的每对标记一行及其距离。下面的代码似乎符合我的要求:Python 使用多索引从距离矩阵创建数据帧,python,pandas,multi-index,Python,Pandas,Multi Index,我有两种类型的对象——F和p,上面有标记,我想计算属于不同类别的每个标记之间的距离,然后构建一个数据框架,其中来自不同类别的每对标记一行及其距离。下面的代码似乎符合我的要求: import itertools import operator from collections import OrderedDict import numpy as np import pandas as pd from scipy.spatial.distance import cdist i = np.sqrt
import itertools
import operator
from collections import OrderedDict
import numpy as np
import pandas as pd
from scipy.spatial.distance import cdist
i = np.sqrt(2)
j = 2 * i
# dicts mapping category and tag to x, y coordinates
timeframe_f = OrderedDict(
[(('F1', 'tag1f1'), (0, 0)), (('F2', 'tag1f2'), (-i, -i)), ])
timeframe_p = OrderedDict(
[(('B1', 'tag1b1'), (i, i)), (('B2', 'tag1b2'), (j, j)),
(('B2', 'tag2b2'), (2 * j, 2 * j)), ])
# calculate the distances
distances = cdist(np.array(list(timeframe_f.values())),
np.array(list(timeframe_p.values())), 'sqeuclidean')
print('distances:\n', distances, '\n')
# here is the matrix with the MultiIndex
distances_matrix = pd.DataFrame(data=distances,
index=pd.MultiIndex.from_tuples(
timeframe_f.keys(),
names=['F', 'Ftags']),
columns=pd.MultiIndex.from_tuples(
timeframe_p.keys(),
names=['P', 'Ptags']), )
print('distances_matrix:\n', distances_matrix, '\n')
# hacky construction of the data frame
index = list(map(lambda x: operator.add(*x), (
itertools.product(timeframe_f.keys(), timeframe_p.keys()))))
# print(index)
multi_index = pd.MultiIndex.from_tuples(index)
distances_df = pd.DataFrame(data=distances.ravel(),
index=multi_index, ).reset_index()
print('distances_df:\n', distances_df)
它打印:
distances:
[[ 4. 16. 64.]
[ 16. 36. 100.]]
distances_matrix:
P B1 B2
Ptags tag1b1 tag1b2 tag2b2
F Ftags
F1 tag1f1 4.0 16.0 64.0
F2 tag1f2 16.0 36.0 100.0
distances_df:
level_0 level_1 level_2 level_3 0
0 F1 tag1f1 B1 tag1b1 4.0
1 F1 tag1f1 B2 tag1b2 16.0
2 F1 tag1f1 B2 tag2b2 64.0
3 F2 tag1f2 B1 tag1b1 16.0
4 F2 tag1f2 B2 tag1b2 36.0
5 F2 tag1f2 B2 tag2b2 100.0
但是我想找到一种直接使用距离矩阵
的方法。我查看了其他各种问题,如:
- :但是当我想使用列乘积构造索引时,这会将列名作为字符串进行操作
- :在这里,我们没有列中的多索引,尽管有一种方法可以使用它会很好,因为我最终想要按类别分组
- 这就是你需要的吗
distances_matrix.reset_index().melt(id_vars=['F','Ftags'])
Out[434]:
F Ftags P Ptags value
0 F1 tag1f1 B1 tag1b1 4.0
1 F2 tag1f2 B1 tag1b1 16.0
2 F1 tag1f1 B2 tag1b2 16.0
3 F2 tag1f2 B2 tag1b2 36.0
4 F1 tag1f1 B2 tag2b2 64.0
5 F2 tag1f2 B2 tag2b2 100.0
这是你需要的吗
distances_matrix.reset_index().melt(id_vars=['F','Ftags'])
Out[434]:
F Ftags P Ptags value
0 F1 tag1f1 B1 tag1b1 4.0
1 F2 tag1f2 B1 tag1b1 16.0
2 F1 tag1f1 B2 tag1b2 16.0
3 F2 tag1f2 B2 tag1b2 36.0
4 F1 tag1f1 B2 tag2b2 64.0
5 F2 tag1f2 B2 tag2b2 100.0
哦,太棒了-让我检查一下-这种熔化方法是什么(我真的无法将我的大脑围绕在熊猫神秘的列/索引操作上)?@Mr_和Mrs_D这更像是重塑,从宽到长的格式我尝试过重置索引和堆栈等组合,但没有用-这正是我想要的。您认为,给定我使用的OrderedPicts,该方法可以保证为我提供正确的行(映射正确的距离)吗?还有其他方法吗?立即构建最终的数据帧(出于好奇)?旁白:“这个函数对数据帧的按摩很有用…”-。。。显然,我应该搜索“按摩”…哦,太好了-让我检查一下-这种融化方法是什么(我真的无法将我的大脑围绕在熊猫神秘的列/索引操作上)?@Mr_和Mrs_D这更像是重塑,从宽格式到长格式,我尝试过重置索引和堆栈等组合,但没有效果——这正是我想要的。您认为,给定我使用的OrderedPicts,该方法可以保证为我提供正确的行(映射正确的距离)吗?还有其他方法吗?立即构建最终的数据帧(出于好奇)?旁白:“这个函数对数据帧的按摩很有用…”-。。。我应该找“按摩”的显然。。。