Java 类名:com.alibaba.alink.operator.batch.statistics.ChiSquareTestBatchOp
Python 类名:ChiSquareTestBatchOp

功能介绍

卡法独立性检验是检验两个因素(各有两项或以上的分类)之间是否相互影响的问题,其零假设是两因素之间相互独立。

参数说明

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

| labelCol | 标签列名 | 输入表中的标签列名 | String | ✓ | | |

| selectedCols | 选择的列名 | 计算列对应的列名列表 | String[] | ✓ | | |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ['a1','b1','c1'],
  6. ['a1','b2','c1'],
  7. ['a1','b1','c2'],
  8. ['a2','b1','c1'],
  9. ['a2','b2','c2'],
  10. ['a2', 'b1','c1']
  11. ])
  12. batchData = BatchOperator.fromDataframe(df, schemaStr='x1 string, x2 string, x3 string')
  13. chisqTest = ChiSquareTestBatchOp()\
  14. .setSelectedCols(["x1","x2"])\
  15. .setLabelCol("x3")
  16. batchData.link(chisqTest).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  4. import com.alibaba.alink.operator.batch.statistics.ChiSquareTestBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class ChiSquareTestBatchOpTest {
  9. @Test
  10. public void testChiSquareTestBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("a1", "b1", "c1"),
  13. Row.of("a1", "b2", "c1"),
  14. Row.of("a1", "b1", "c2"),
  15. Row.of("a2", "b1", "c1"),
  16. Row.of("a2", "b2", "c2"),
  17. Row.of("a2", "b1", "c1")
  18. );
  19. BatchOperator <?> batchData = new MemSourceBatchOp(df, "x1 string, x2 string, x3 string");
  20. BatchOperator <?> chisqTest = new ChiSquareTestBatchOp()
  21. .setSelectedCols("x1", "x2")
  22. .setLabelCol("x3");
  23. batchData.link(chisqTest).print();
  24. }
  25. }

运行结果

| col | chi2_result | | —- | —- |

| x1 | {“comment”:”pearson test”,”df”:1.0,”p”:1.0,”value”:0.0} |

| x2 | {“comment”:”pearson test”,”df”:1.0,”p”:0.5402913746074196,”value”:0.37500000000000006} |