Python Pandas:如何找到组合的模式(Combinations of Combinations)——时间序列
Python Pandas: How to find patterns of combinations (Combinations of Combinations) - time series
从这里开始:
我找到了使用此代码的 3 列的出现次数最多到最少的组合:
def common_cols(df,n):
'''n is how many of the top results to show'''
df = df.groupby(['A','B','C']).size().reset_index().rename(columns={0:'count'})
df = df.sort_values(by='count', ascending=False).reset_index(drop=True).head(n)
return df
common_data = common_cols(df,10)
common_data的输出(显示前 10 个结果):
A B C count
0 0.00 0.00 0.00 96
1 0.00 1.00 0.00 25
2 0.14 0.86 0.00 19
3 0.13 0.87 0.00 17
4 0.00 0.72 0.28 17
5 0.00 0.89 0.11 16
6 0.01 0.84 0.15 16
7 0.03 0.97 0.00 15
8 0.35 0.65 0.00 15
9 0.13 0.79 0.08 14
现在,我想找到 A B C 行的组合,并计算它们出现了多少次。
例如,假设在 BASE df 中从第 1 行到第 4 行:
3 列的第一组组合(在使用 common_cols 函数之前由 dataframe(df) 告知)是
# each of these rows are their own combination of values
A B C
0 0.67 0.16 0.17
1 0.06 0.73 0.20
2 0.19 0.48 0.33
3 0.07 0.87 0.06
4 0.07 0.60 0.33
以上5行(按顺序)算作一种组合模式。它可以算作 2 行、3 行、4 行或更多行的组合(如果这样做很容易的话!)
如果这个模式被发现一次(在整个数据帧中),它会输出这个模式的计数为 1。如果它被发现 10 次;计数为 10。
关于如何计算连续行之间找到的组合的任何想法?
就像使用 common_cols 函数一样,但作为 'combinations of combinations'?
行必须是为了它是一个模式。非常感谢任何帮助!
我在这个测试数据帧中使用了整数,但是如果你的 groupby
在上面工作,这也应该适用于你的数据:
df_size = 1000000
df = pd.DataFrame( { 'A' : (np.random.randint(20) for i in range(df_size)),
'B' : (np.random.randint(20) for i in range(df_size)),
'C' : (np.random.randint(20) for i in range(df_size)),
})
print(df.head())
A B C
0 12 12 5
1 19 12 12
2 14 11 15
3 11 14 8
4 13 16 2
下面的代码使用 zip
创建了一个名为 source
的三元组(A、B、C)列表。 tmp
变量(生成器)实际上是一个列表,其中包含源列表的连续“移位”副本,例如 [source[0:], source[1:], source[2:]...]
最后,zip
交错 tmp
列表中的值,例如,对于 n=2
它将生成一个列表
[(source[0], source[1]), (source[1], source[2]), ... ]
source = list(zip(df['A'],df['B'],df['C']))
n_consecutive = 3
tmp = ( source[i:] for i in range(n_consecutive) )
output = pd.Series(list(zip(*tmp)))
对于这个例子,这是一个包含三元组(A、B、C)值计数的序列:
print(output.value_counts().head())
((6, 19, 14), (19, 12, 6), (13, 7, 10)) 2
((2, 18, 12), (17, 2, 19), (7, 19, 19)) 1
((10, 2, 3), (1, 18, 8), (3, 6, 19)) 1
((16, 15, 14), (11, 2, 9), (14, 14, 8)) 1
((3, 3, 7), (13, 9, 3), (18, 15, 6)) 1
dtype: int64
请注意,这可能会根据您要查找的内容重复计数。例如,如果基本 df 连续有 3 个记录,而您正在寻找 2 个连续的模式:
(1, 3, 4)
(1, 3, 4)
(1, 3, 4)
在那种情况下它会找到 (1, 3, 4), (1, 3, 4)
两次。
从这里开始:
我找到了使用此代码的 3 列的出现次数最多到最少的组合:
def common_cols(df,n):
'''n is how many of the top results to show'''
df = df.groupby(['A','B','C']).size().reset_index().rename(columns={0:'count'})
df = df.sort_values(by='count', ascending=False).reset_index(drop=True).head(n)
return df
common_data = common_cols(df,10)
common_data的输出(显示前 10 个结果):
A B C count
0 0.00 0.00 0.00 96
1 0.00 1.00 0.00 25
2 0.14 0.86 0.00 19
3 0.13 0.87 0.00 17
4 0.00 0.72 0.28 17
5 0.00 0.89 0.11 16
6 0.01 0.84 0.15 16
7 0.03 0.97 0.00 15
8 0.35 0.65 0.00 15
9 0.13 0.79 0.08 14
现在,我想找到 A B C 行的组合,并计算它们出现了多少次。
例如,假设在 BASE df 中从第 1 行到第 4 行:
3 列的第一组组合(在使用 common_cols 函数之前由 dataframe(df) 告知)是
# each of these rows are their own combination of values
A B C
0 0.67 0.16 0.17
1 0.06 0.73 0.20
2 0.19 0.48 0.33
3 0.07 0.87 0.06
4 0.07 0.60 0.33
以上5行(按顺序)算作一种组合模式。它可以算作 2 行、3 行、4 行或更多行的组合(如果这样做很容易的话!)
如果这个模式被发现一次(在整个数据帧中),它会输出这个模式的计数为 1。如果它被发现 10 次;计数为 10。
关于如何计算连续行之间找到的组合的任何想法? 就像使用 common_cols 函数一样,但作为 'combinations of combinations'?
行必须是为了它是一个模式。非常感谢任何帮助!
我在这个测试数据帧中使用了整数,但是如果你的 groupby
在上面工作,这也应该适用于你的数据:
df_size = 1000000
df = pd.DataFrame( { 'A' : (np.random.randint(20) for i in range(df_size)),
'B' : (np.random.randint(20) for i in range(df_size)),
'C' : (np.random.randint(20) for i in range(df_size)),
})
print(df.head())
A B C
0 12 12 5
1 19 12 12
2 14 11 15
3 11 14 8
4 13 16 2
下面的代码使用 zip
创建了一个名为 source
的三元组(A、B、C)列表。 tmp
变量(生成器)实际上是一个列表,其中包含源列表的连续“移位”副本,例如 [source[0:], source[1:], source[2:]...]
最后,zip
交错 tmp
列表中的值,例如,对于 n=2
它将生成一个列表
[(source[0], source[1]), (source[1], source[2]), ... ]
source = list(zip(df['A'],df['B'],df['C']))
n_consecutive = 3
tmp = ( source[i:] for i in range(n_consecutive) )
output = pd.Series(list(zip(*tmp)))
对于这个例子,这是一个包含三元组(A、B、C)值计数的序列:
print(output.value_counts().head())
((6, 19, 14), (19, 12, 6), (13, 7, 10)) 2
((2, 18, 12), (17, 2, 19), (7, 19, 19)) 1
((10, 2, 3), (1, 18, 8), (3, 6, 19)) 1
((16, 15, 14), (11, 2, 9), (14, 14, 8)) 1
((3, 3, 7), (13, 9, 3), (18, 15, 6)) 1
dtype: int64
请注意,这可能会根据您要查找的内容重复计数。例如,如果基本 df 连续有 3 个记录,而您正在寻找 2 个连续的模式:
(1, 3, 4)
(1, 3, 4)
(1, 3, 4)
在那种情况下它会找到 (1, 3, 4), (1, 3, 4)
两次。