class_docstr: Conduct Pearson's independence test for every feature against the label. For each feature,
  the (feature, label) pairs are converted into a contingency matrix for which the Chi-squared
  statistic is computed. All label and feature values must be categorical.
  
  The null hypothesis is that the occurrence of the outcomes is statistically independent.
  
  .. versionadded:: 2.2.0
class_docstr_tok: ["Conduct","Pearson","'s","independence","test","for","every","feature","against","the","label",".","For","each","feature",",","the","(","feature",",","label",")","pairs","are","converted","into","a","contingency","matrix","for","which","the","Chi-squared","statistic","is","computed",".","All","label","and","feature","values","must","be","categorical",".","The","null","hypothesis","is","that","the","occurrence","of","the","outcomes","is","statistically","independent",".","..","versionadded",":",":","2.2.0"]
docstr: Perform a Pearson's independence test using dataset.
  
  .. versionadded:: 2.2.0
  .. versionchanged:: 3.1.0
     Added optional ``flatten`` argument.
  
  Parameters
  ----------
  dataset : :py:class:`pyspark.sql.DataFrame`
      DataFrame of categorical labels and categorical features.
      Real-valued features will be treated as categorical for each distinct value.
  featuresCol : str
      Name of features column in dataset, of type `Vector` (`VectorUDT`).
  labelCol : str
      Name of label column in dataset, of any numerical type.
  flatten : bool, optional
      if True, flattens the returned dataframe.
  
  Returns
  -------
  :py:class:`pyspark.sql.DataFrame`
      DataFrame containing the test result for every feature against the label.
      If flatten is True, this DataFrame will contain one row per feature with the following
      fields:
  
      - `featureIndex: int`
      - `pValue: float`
      - `degreesOfFreedom: int`
      - `statistic: float`
  
      If flatten is False, this DataFrame will contain a single Row with the following fields:
  
      - `pValues: Vector`
      - `degreesOfFreedom: Array[int]`
      - `statistics: Vector`
  
      Each of these fields has one value per feature.
  
  Examples
  --------
  >>> from pyspark.ml.linalg import Vectors
  >>> from pyspark.ml.stat import ChiSquareTest
  >>> dataset = [[0, Vectors.dense([0, 0, 1])],
  ...            [0, Vectors.dense([1, 0, 1])],
  ...            [1, Vectors.dense([2, 1, 1])],
  ...            [1, Vectors.dense([3, 1, 1])]]
  >>> dataset = spark.createDataFrame(dataset, ["label", "features"])
  >>> chiSqResult = ChiSquareTest.test(dataset, 'features', 'label')
  >>> chiSqResult.select("degreesOfFreedom").collect()[0]
  Row(degreesOfFreedom=[3, 1, 0])
  >>> chiSqResult = ChiSquareTest.test(dataset, 'features', 'label', True)
  >>> row = chiSqResult.orderBy("featureIndex").collect()
  >>> row[0].statistic
  4.0
docstr_tok: ["Perform","a","Pearson","'s","independence","test","using","dataset",".","..","versionadded",":",":","2.2.0","..","versionchanged",":",":","3.1.0","Added","optional","``","flatten","``","argument",".","Parameters","--","--","--","--","--","dataset",":",":","py",":","class",":","`","pyspark.sql.DataFrame","`","DataFrame","of","categorical","labels","and","categorical","features",".","Real-valued","features","will","be","treated","as","categorical","for","each","distinct","value",".","featuresCol",":","str","Name","of","features","column","in","dataset",",","of","type","`","Vector","`","(","`","VectorUDT","`",")",".","labelCol",":","str","Name","of","label","column","in","dataset",",","of","any","numerical","type",".","flatten",":","bool",",","optional","if","True",",","flattens","the","returned","dataframe",".","Returns","--","--","--","-",":","py",":","class",":","`","pyspark.sql.DataFrame","`","DataFrame","containing","the","test","result","for","every","feature","against","the","label",".","If","flatten","is","True",",","this","DataFrame","will","contain","one","row","per","feature","with","the","following","fields",":","-","`","featureIndex",":","int","`","-","`","pValue",":","float","`","-","`","degreesOfFreedom",":","int","`","-","`","statistic",":","float","`","If","flatten","is","False",",","this","DataFrame","will","contain","a","single","Row","with","the","following","fields",":","-","`","pValues",":","Vector","`","-","`","degreesOfFreedom",":","Array","[","int","]","`","-","`","statistics",":","Vector","`","Each","of","these","fields","has","one","value","per","feature",".","Examples","--","--","--","--",">",">",">","from","pyspark.ml.linalg","import","Vectors",">",">",">","from","pyspark.ml.stat","import","ChiSquareTest",">",">",">","dataset","=","[","[","0",",","Vectors.dense","(","[","0",",","0",",","1","]",")","]",",","...","[","0",",","Vectors.dense","(","[","1",",","0",",","1","]",")","]",",","...","[","1",",","Vectors.dense","(","[","2",",","1",",","1","]",")","]",",","...","[","1",",","Vectors.dense","(","[","3",",","1",",","1","]",")","]","]",">",">",">","dataset","=","spark.createDataFrame","(","dataset",",","[","``","label","''",",","``","features","''","]",")",">",">",">","chiSqResult","=","ChiSquareTest.test","(","dataset",",","'features","'",",","'label","'",")",">",">",">","chiSqResult.select","(","``","degreesOfFreedom","''",")",".collect","(",")","[","0","]","Row","(","degreesOfFreedom=","[","3",",","1",",","0","]",")",">",">",">","chiSqResult","=","ChiSquareTest.test","(","dataset",",","'features","'",",","'label","'",",","True",")",">",">",">","row","=","chiSqResult.orderBy","(","``","featureIndex","''",")",".collect","(",")",">",">",">","row","[","0","]",".statistic","4.0"]
code: def test(
      dataset: DataFrame,
      featuresCol: str,
      labelCol: str,
      flatten: bool = False,
  ) -> DataFrame:
      """
      Perform a Pearson's independence test using dataset.
  
      .. versionadded:: 2.2.0
      .. versionchanged:: 3.1.0
         Added optional ``flatten`` argument.
  
      Parameters
      ----------
      dataset : :py:class:`pyspark.sql.DataFrame`
          DataFrame of categorical labels and categorical features.
          Real-valued features will be treated as categorical for each distinct value.
      featuresCol : str
          Name of features column in dataset, of type `Vector` (`VectorUDT`).
      labelCol : str
          Name of label column in dataset, of any numerical type.
      flatten : bool, optional
          if True, flattens the returned dataframe.
  
      Returns
      -------
      :py:class:`pyspark.sql.DataFrame`
          DataFrame containing the test result for every feature against the label.
          If flatten is True, this DataFrame will contain one row per feature with the following
          fields:
  
          - `featureIndex: int`
          - `pValue: float`
          - `degreesOfFreedom: int`
          - `statistic: float`
  
          If flatten is False, this DataFrame will contain a single Row with the following fields:
  
          - `pValues: Vector`
          - `degreesOfFreedom: Array[int]`
          - `statistics: Vector`
  
          Each of these fields has one value per feature.
  
      Examples
      --------
      >>> from pyspark.ml.linalg import Vectors
      >>> from pyspark.ml.stat import ChiSquareTest
      >>> dataset = [[0, Vectors.dense([0, 0, 1])],
      ...            [0, Vectors.dense([1, 0, 1])],
      ...            [1, Vectors.dense([2, 1, 1])],
      ...            [1, Vectors.dense([3, 1, 1])]]
      >>> dataset = spark.createDataFrame(dataset, ["label", "features"])
      >>> chiSqResult = ChiSquareTest.test(dataset, 'features', 'label')
      >>> chiSqResult.select("degreesOfFreedom").collect()[0]
      Row(degreesOfFreedom=[3, 1, 0])
      >>> chiSqResult = ChiSquareTest.test(dataset, 'features', 'label', True)
      >>> row = chiSqResult.orderBy("featureIndex").collect()
      >>> row[0].statistic
      4.0
      """
      sc = SparkContext._active_spark_context
      assert sc is not None
  
      javaTestObj = _jvm().org.apache.spark.ml.stat.ChiSquareTest
      args = [
          _py2java(sc, arg)
          for arg in (dataset, featuresCol, labelCol, flatten)
      ]
      return _java2py(sc, javaTestObj.test(*args))
  
imports: import sys
  from typing import Optional, Tuple, TYPE_CHECKING
  from pyspark import since, SparkContext
  from pyspark.ml.common import _java2py, _py2java
  from pyspark.ml.linalg import Matrix, Vector
  from pyspark.ml.wrapper import JavaWrapper, _jvm
  from pyspark.sql.column import Column, _to_seq
  from pyspark.sql.dataframe import DataFrame
  from pyspark.sql.functions import lit
label_desc: Use image node_id 1 for calling the ChiSquareTest obj's underlying member method code with example usage: obj.test(dataset, featuresCol, labelCol, flatten) and returns: _java2py