我正在将数据框转换为方阵。数据框有一个索引,只有一列有 float 。我需要做的是计算所有索引对,并为每一对取两个关联列值的平均值。因此,通常的枢轴函数只是解决方案的一部分。
目前,该函数的估计复杂度为 O(n^2),这并不好,因为我必须同时处理具有数百行数据帧的较大输入。我可以采用另一种更快的方法吗?
示例输入(为简单起见,此处使用整数):
df = pd.DataFrame([3, 4, 5])
更新:转换逻辑
对于示例中的输入数据框:
0
0 3
1 4
2 5
我会执行以下操作(但并不声称这是最好的方法):
- 获取所有索引对:(0,1), (1,2), (0,2)
- 对于每一对,计算它们的平均值:(0,1):3.5, (1,2):4.5, (0,2):4.0
- 使用每对中的索引作为列和行标识符,并在对角线上使用零(如所需输出所示)构建一个正方形对称矩阵。
代码在 turn_table_into_square_matrix() 中。
期望的输出:
0 1 2
0 0.0 3.5 4.0
1 3.5 0.0 4.5
2 4.0 4.5 0.0
当前实现:
import pandas as pd
from itertools import combinations
import time
import string
import random
def turn_table_into_square_matrix(original_dataframe):
# get all pairs of indices
index_pairs = list(combinations(list(original_dataframe.index),2))
rows_for_final_dataframe = []
# collect new data frame row by row - the time consuming part
for pair in index_pairs:
subset_original_dataframe = original_dataframe[original_dataframe.index.isin(list(pair))]
rows_for_final_dataframe.append([pair[0], pair[1], subset_original_dataframe[0].mean()])
rows_for_final_dataframe.append([pair[1], pair[0], subset_original_dataframe[0].mean()])
final_dataframe = pd.DataFrame(rows_for_final_dataframe)
final_dataframe.columns = ["from", "to", "weight"]
final_dataframe_pivot = final_dataframe.pivot(index="from", columns="to", values="weight")
final_dataframe_pivot = final_dataframe_pivot.fillna(0)
return final_dataframe_pivot
为性能计时的代码:
for size in range(50, 600, 100):
index = range(size)
values = random.sample(range(0, 1000), size)
example = pd.DataFrame(values, index)
print ("dataframe size", example.shape)
start_time = time.time()
turn_table_into_square_matrix(example)
print ("conversion time:", time.time()-start_time)
计时结果:
dataframe size (50, 1)
conversion time: 0.5455281734466553
dataframe size (150, 1)
conversion time: 5.001590013504028
dataframe size (250, 1)
conversion time: 14.562285900115967
dataframe size (350, 1)
conversion time: 31.168692111968994
dataframe size (450, 1)
conversion time: 49.07127499580383
dataframe size (550, 1)
conversion time: 78.73740792274475
因此,具有 50 行的数据帧仅需半秒即可转换,而具有 550 行(长 11 倍)的数据帧需要 79 秒(长 11^2 倍)。这个问题有更快的解决方案吗?
最佳答案
我认为对于该计算,不可能比 O(n^2)
做得更好。正如@piiiipmatz 所建议的那样,您应该尝试使用 numpy 完成所有操作,然后将结果放入 pd.DataFrame
中。您的问题听起来像是 numpy.add.at
的一个很好的用例。
这是一个简单的例子
import numpy as np
import itertools
# your original array
x = np.array([1, 4, 8, 99, 77, 23, 4, 45])
n = len(x)
# all pairs of indices in x
a, b = zip(*list(itertools.product(range(n), range(n))))
a, b = np.array(a), np.array(b)
# resulting matrix
result = np.zeros(shape=(n, n))
np.add.at(result, [a, b], (x[a] + x[b]) / 2.0)
print(result)
# [[ 1. 2.5 4.5 50. 39. 12. 2.5 23. ]
# [ 2.5 4. 6. 51.5 40.5 13.5 4. 24.5]
# [ 4.5 6. 8. 53.5 42.5 15.5 6. 26.5]
# [ 50. 51.5 53.5 99. 88. 61. 51.5 72. ]
# [ 39. 40.5 42.5 88. 77. 50. 40.5 61. ]
# [ 12. 13.5 15.5 61. 50. 23. 13.5 34. ]
# [ 2.5 4. 6. 51.5 40.5 13.5 4. 24.5]
# [ 23. 24.5 26.5 72. 61. 34. 24.5 45. ]]
关于python - Pandas - 使用来自索引的成对组合将数据帧转换为方矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503165/