1

拥有 50,000 个用户的 DF,每个用户都有不同的行数:

                      id            feature_1  ...           feature10  feature11
0  1587712104294-4384584            -0.661835  ...           -1.768028   -0.38924
1  1587712104294-4384584            -0.661835  ...           -1.709090   -0.38924
---- User 2 starts here ----
2  1587712104294-1234584            -0.661835  ...           -1.708693   -0.38924
3  1587712104294-1234584            -0.661835  ...           -1.627594   -0.38924
4  1587712104294-1234584            -0.653476  ...           -1.329767   -0.38924

我正在使用以下代码创建一个tf.ragged.constant

x_np_values = data.values
# take all columns beside the id column and use the id to group arrays 
X = np.split(x_np_values[:,1:], np.unique(x_np_values[:, 0], return_index=True)[1][1:])
X = tf.ragged.constant(X)

该代码删除了 id 列并创建了用户行的参差不齐的常量。但是,这只适用于数据的一小部分。对于整个数据集,它需要很长时间,有时会导致我的机器崩溃。

按 id 分组并从其余列创建参差不齐的常量的正确方法是什么?

4

1 回答 1

0

我发现这种方法在创建参差不齐的常量时要快得多:

def get_ragged_constants(data):
    return tf.RaggedTensor.from_row_lengths(
        values=data.values,
        row_lengths=data.groupby('GROUP_ID').size())
于 2021-06-16T09:15:55.620 回答