Description

The random forest use the bagging to prevent the overfitting.

In the operator, we implement three type of decision tree to increase diversity of the forest.

    id3 cart c4.5
and the criteria is
    information gini information ratio mse

Parameters

Name Description Type Required? Default Value
predictionCol Column name of prediction. String
predictionDetailCol Column name of prediction result, it will include detailed info. String
reservedCols Names of the columns to be retained in the output table 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. [1.0, "A", 0, 0, 0],
  7. [2.0, "B", 1, 1, 0],
  8. [3.0, "C", 2, 2, 1],
  9. [4.0, "D", 3, 3, 1]
  10. ])
  11. def sourceFrame():
  12. data = exampleData()
  13. return pd.DataFrame({
  14. "f0": data[:, 0],
  15. "f1": data[:, 1],
  16. "f2": data[:, 2],
  17. "f3": data[:, 3],
  18. "label": data[:, 4]
  19. })
  20. def batchSource():
  21. return dataframeToOperator(
  22. sourceFrame(),
  23. schemaStr='''
  24. f0 double,
  25. f1 string,
  26. f2 int,
  27. f3 int,
  28. label int
  29. ''',
  30. op_type='batch'
  31. )
  32. def streamSource():
  33. return dataframeToOperator(
  34. sourceFrame(),
  35. schemaStr='''
  36. f0 double,
  37. f1 string,
  38. f2 int,
  39. f3 int,
  40. label int
  41. ''',
  42. op_type='stream'
  43. )
  44. trainOp = (
  45. RandomForestRegTrainBatchOp()
  46. .setLabelCol('label')
  47. .setFeatureCols(['f0', 'f1', 'f2', 'f3'])
  48. )
  49. predictBatchOp = (
  50. RandomForestRegPredictBatchOp()
  51. .setPredictionCol('pred')
  52. )
  53. (
  54. predictBatchOp
  55. .linkFrom(
  56. batchSource().link(trainOp),
  57. batchSource()
  58. )
  59. .print()
  60. )
  61. predictStreamOp = (
  62. RandomForestRegPredictStreamOp(
  63. batchSource().link(trainOp)
  64. )
  65. .setPredictionCol('pred')
  66. )
  67. (
  68. predictStreamOp
  69. .linkFrom(
  70. streamSource()
  71. )
  72. .print()
  73. )
  74. StreamOperator.execute()

Result

Batch prediction

  1. f0 f1 f2 f3 label pred
  2. 0 1.0 A 0 0 0 0.0
  3. 1 2.0 B 1 1 0 0.0
  4. 2 3.0 C 2 2 1 1.0
  5. 3 4.0 D 3 3 1 1.0