Java 类名:com.alibaba.alink.operator.batch.recommendation.NegativeItemSamplingBatchOp
Python 类名:NegativeItemSamplingBatchOp

功能介绍

当给定user-item pair数据的时候,为数据生成若干负样本数据,构成训练数据。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| samplingFactor | 采样因子 | 采样因子 | Integer | | | 3 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. [1, 1],
  6. [2, 2],
  7. [2, 3],
  8. [4, 1],
  9. [4, 2],
  10. [4, 3],
  11. ])
  12. data = BatchOperator.fromDataframe(df_data, schemaStr='user bigint, item bigint')
  13. NegativeItemSamplingBatchOp().linkFrom(data).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.recommendation.NegativeItemSamplingBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class NegativeItemSamplingBatchOpTest {
  9. @Test
  10. public void testNegativeItemSamplingBatchOp() throws Exception {
  11. List <Row> df_data = Arrays.asList(
  12. Row.of(1, 1),
  13. Row.of(2, 2),
  14. Row.of(2, 3),
  15. Row.of(4, 1),
  16. Row.of(4, 2),
  17. Row.of(4, 3)
  18. );
  19. BatchOperator <?> data = new MemSourceBatchOp(df_data, "user int, item int");
  20. new NegativeItemSamplingBatchOp().linkFrom(data).print();
  21. }
  22. }

运行结果

| user | item | label | | —- | —- | —- |

| 2 | 1 | 0 |

| 1 | 3 | 0 |

| 4 | 1 | 1 |

| 4 | 2 | 1 |

| 1 | 3 | 0 |

| 2 | 1 | 0 |

| 2 | 1 | 0 |

| 4 | 3 | 1 |

| 2 | 2 | 1 |

| 2 | 3 | 1 |

| 2 | 1 | 0 |

| 1 | 1 | 1 |

| 2 | 1 | 0 |

| 1 | 3 | 0 |

| 2 | 1 | 0 |