Description

Quantile discretizer calculate the q-quantile as the interval, output the interval as model, and can transform a new data using the model. The output is the index of the interval.

Parameters

Name Description Type Required? Default Value
selectedCols Names of the columns used for processing String[]
numBuckets number of buckets Integer 2
numBucketsArray Array of num bucket Integer[] null
selectedCols Names of the columns used for processing String[]
reservedCols Names of the columns to be retained in the output table String[] null
outputCols Names of the output columns String[] null

Script Example

Code

  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. (
  45. QuantileDiscretizer()
  46. .setSelectedCols(['f_double'])
  47. .setNumBuckets(8)
  48. .fit(batchSource())
  49. .transform(batchSource())
  50. .print()
  51. )
  52. (
  53. QuantileDiscretizer()
  54. .setSelectedCols(['f_double'])
  55. .setNumBuckets(8)
  56. .fit(batchSource())
  57. .transform(streamSource())
  58. .print()
  59. )
  60. StreamOperator.execute()

Result

Batch prediction

  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

Stream Prediction

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