gradient boosted trees 28gbts 29 learning algorithm for regression

Solutions on MaxInterview for gradient boosted trees 28gbts 29 learning algorithm for regression by the best coders in the world

showing results for - "gradient boosted trees 28gbts 29 learning algorithm for regression"
Blake
26 Jul 2019
1# Gradient-Boosted Trees (GBTs) learning algorithm for regression
2
3from numpy import allclose
4from pyspark.ml.linalg import Vectors
5df = spark.createDataFrame([
6  (1.0, Vectors.dense(1.0)),
7  (0.0, Vectors.sparse(1, [], []))], ["label", "features"])
8gbt = GBTRegressor(maxIter=5, maxDepth=2, seed=42)
9print(gbt.getImpurity())
10# variance
11model = gbt.fit(df)
12model.featureImportances
13# SparseVector(1, {0: 1.0})
14model.numFeatures
15# 1
16allclose(model.treeWeights, [1.0, 0.1, 0.1, 0.1, 0.1])
17# True
18test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"])
19model.transform(test0).head().prediction
20# 0.0
21test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"])
22model.transform(test1).head().prediction
23# 1.0
24gbtr_path = temp_path + "gbtr"
25gbt.save(gbtr_path)
26gbt2 = GBTRegressor.load(gbtr_path)
27gbt2.getMaxDepth()
28# 2
29model_path = temp_path + "gbtr_model"
30model.save(model_path)
31model2 = GBTRegressionModel.load(model_path)
32model.featureImportances == model2.featureImportances
33# True
34model.treeWeights == model2.treeWeights
35# True
36model.trees
37# [DecisionTreeRegressionModel (uid=...) of depth..., DecisionTreeRegressionModel...]