Код: Выделить всё
import pandas as pd
from itertools import combinations
# Example DataFrame A (replace this with your actual DataFrame)
import pandas as pd
from collections import defaultdict
# Example DataFrame A (replace this with your actual DataFrame)
A = pd.DataFrame({
'user_id': [1, 2, 3],
'history': [[1, 2, 3], [2, 3, 4], [1, 3, 5]]
})
# Initialize a dictionary to store counts of triples
triple_counts = defaultdict(int)
# Iterate over each row of A
for index, row in A.iterrows():
history = row['history']
n = len(history)
# Iterate over all triples (bi, bj, bk) where i < j < k
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
bi = history[i]
bj = history[j]
bk = history[k]
if bi < bj:
triple_counts[(bi, bj, bk)] += 1
else:
triple_counts[(bj, bi, bk)] += 1
# Output the counts of all triples
for triple, count in triple_counts.items():
print(f"Triple {triple}: Count = {count}")
Подробнее здесь: https://stackoverflow.com/questions/786 ... ue-triples