• QuantileDiscretizer训练
    • 功能介绍
    • 参数说明
    • 脚本示例
      • 脚本代码
      • 脚本结果

    QuantileDiscretizer训练

    功能介绍

    分位点离散可以计算选定列的分位点,然后使用这些分位点进行离散化。生成选中列对应的q-quantile,其中可以所有列指定一个,也可以每一列对应一个

    参数说明

    名称 中文名称 描述 类型 是否必须? 默认值
    selectedCols 选择的列名 计算列对应的列名列表 String[]
    numBuckets quantile个数 quantile个数,对所有列有效。 Integer 2
    numBucketsArray quantile个数 quantile个数,每一列对应数组中一个元素。 Integer[] null

    脚本示例

    脚本代码

    1. import numpy as np
    2. import pandas as pd
    3. from pyalink.alink import *
    4. def exampleData():
    5. return np.array([
    6. ["a", 1, 1, 2.0, True],
    7. ["c", 1, 2, -3.0, True],
    8. ["a", 2, 2, 2.0, False],
    9. ["c", 0, 0, 0.0, False]
    10. ])
    11. def sourceFrame():
    12. data = exampleData()
    13. return pd.DataFrame({
    14. "f_string": data[:, 0],
    15. "f_long": data[:, 1],
    16. "f_int": data[:, 2],
    17. "f_double": data[:, 3],
    18. "f_boolean": data[:, 4]
    19. })
    20. def batchSource():
    21. return dataframeToOperator(
    22. sourceFrame(),
    23. schemaStr='''
    24. f_string string,
    25. f_long long,
    26. f_int int,
    27. f_double double,
    28. f_boolean boolean
    29. ''',
    30. op_type='batch'
    31. )
    32. def streamSource():
    33. return dataframeToOperator(
    34. sourceFrame(),
    35. schemaStr='''
    36. f_string string,
    37. f_long long,
    38. f_int int,
    39. f_double double,
    40. f_boolean boolean
    41. ''',
    42. op_type='stream'
    43. )
    44. trainOp = (
    45. QuantileDiscretizerTrainBatchOp()
    46. .setSelectedCols(['f_double'])
    47. .setNumBuckets(8)
    48. )
    49. predictBatchOp = (
    50. QuantileDiscretizerPredictBatchOp()
    51. .setSelectedCols(['f_double'])
    52. )
    53. (
    54. predictBatchOp
    55. .linkFrom(
    56. batchSource().link(trainOp),
    57. batchSource()
    58. )
    59. .print()
    60. )
    61. predictStreamOp = (
    62. QuantileDiscretizerPredictStreamOp(
    63. batchSource().link(trainOp)
    64. )
    65. .setSelectedCols(['f_double'])
    66. )
    67. (
    68. predictStreamOp
    69. .linkFrom(
    70. streamSource()
    71. )
    72. .print()
    73. )
    74. StreamOperator.execute()

    脚本结果

    批预测结果

    1. f_string f_long f_int f_double f_boolean
    2. 0 a 1 1 2 True
    3. 1 c 1 2 0 True
    4. 2 a 2 2 2 False
    5. 3 c 0 0 1 False

    流预测结果

    1. f_string f_long f_int f_double f_boolean
    2. 0 a 1 1 2 True
    3. 1 a 2 2 2 False
    4. 2 c 1 2 0 True
    5. 3 c 0 0 1 False