我有2列的熊猫df,每列包含270万行长度为20的归一化向量。
我要对column1-row1 vs column2- row1,column1-row2 vs column2-row2 ...进行余弦模拟,依此类推,直到270万。
我尝试了循环,但是这非常慢。最快的方法是什么?
这是我现在正在使用的:
for index, row in df.iterrows():
x = 1 - spatial.distance.cosine(tempdf['unit_vector'][index],
tempdf['ave_unit_vector'][index])
print(index,x)
数据:
tempdf['unit_vector']
Out[185]:
0 [0.7071067811865475, 0.7071067811865475, 0.0, ...
1 [0.634997029655247, 0.634997029655247, 0.43995...
2 [0.5233710392524532, 0.5233710392524532, 0.552...
3 [0.4792468085399227, 0.4792468085399227, 0.505...
4 [0.4937468195427678, 0.4937468195427678, 0.492...
5 [0.49444897739151283, 0.49444897739151283, 0.5...
6 [0.49548793862403173, 0.49548793862403173, 0.4...
7 [0.5027211862475275, 0.5027211862475275, 0.495...
8 [0.5136216906905179, 0.5136216906905179, 0.489...
9 [0.5035958124287837, 0.5035958124287837, 0.508...
10 [0.5037995208120967, 0.5037995208120967, 0.493...
tempdf['ave_unit_vector']
Out[186]:
0 [0.5024525269125278, 0.5024525269125278, 0.494...
1 [0.5010905514059507, 0.5010905514059507, 0.499...
2 [0.4993456468410199, 0.4993456468410199, 0.501...
3 [0.5005492367626839, 0.5005492367626839, 0.498...
4 [0.4999384715200533, 0.4999384715200533, 0.501...
5 [0.49836832120891517, 0.49836832120891517, 0.5...
6 [0.49842376222388335, 0.49842376222388335, 0.5...
7 [0.4984869391887457, 0.4984869391887457, 0.500...
8 [0.4990867844970344, 0.4990867844970344, 0.499...
9 [0.49977780370532715, 0.49977780370532715, 0.4...
10 [0.5003161478128204, 0.5003161478128204, 0.499...
这不是相同的数据集,但将创建可用的df。列“ B”和“ C”:
df = pd.DataFrame(list(range(0,1000)),columns = ['A'])
for i in range(0,5):
df['New_{}'.format(i)] = df['A'].shift(i).tolist()
cols = len(df.columns)
start_col = cols - 6
df['B'] = df.iloc[:,start_col:cols].values.tolist()
df['C'] = df['B'] * 2
答案 0 :(得分:2)
这是我尝试过的最快方法。将计算从一个循环的30分钟以上减少到大约5秒:
tempdf['vector_mult'] = np.multiply(tempdf['unit_vector'], tempdf['ave_unit_vector'])
tempdf['cosinesim'] = tempdf['vector_mult'].apply(lambda x: sum(x))
之所以可行,是因为我的向量已经是单位向量。
第一个函数将两列中的向量逐行相乘。第二个函数按行对其求和。这里的挑战是没有预建函数要逐行求解。相反,它想对每一列中的向量求和,然后计算结果。