Answers for "decision tree"

0

skitlearn decision tree

from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(random_state=0)
iris = load_iris()
cross_val_score(clf, iris.data, iris.target, cv=10)
Posted by: Guest on January-05-2021
5

decision tree

from sklearn.datasets import load_iris
>>> from sklearn import tree
>>> X, y = load_iris(return_X_y=True)
>>> clf = tree.DecisionTreeClassifier()
>>> clf = clf.fit(X, y)
Posted by: Guest on May-06-2020
0

Decision tree learning algorithm for classification

# Decision tree learning algorithm for classification

from pyspark.ml.linalg import Vectors
from pyspark.ml.feature import StringIndexer
df = spark.createDataFrame([
  (1.0, Vectors.dense(1.0)),
  (0.0, Vectors.sparse(1, [], []))], ["label", "features"])
stringIndexer = StringIndexer(inputCol="label", outputCol="indexed")
si_model = stringIndexer.fit(df)
td = si_model.transform(df)
dt = DecisionTreeClassifier(maxDepth=2, labelCol="indexed")
model = dt.fit(td)
model.numNodes
# 3
model.depth
# 1
model.featuresImportances
# SparseVector(1, {0: 1.0})
model.numFeatures
# 1
model.numClasses
# 2
print(model.toDebugString)
# DecisionTreeClassificationModel (uid=...) of depth 1 with 3 nodes...
test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"])
result = model.transform(test0).head()
result.prediction
# 0.0
result.probability
# DenseVectors([1.0, 0.0])
result.rawPrediction
# DenseVector([1.0, 0.0])
test1 = spark.createDataFrame([Vectors.sparse(1, [0], [1.0]),)], ["features"])
model.transform(test1).head().prediction
# 1.0

dtc_path = temp_path + "/dtc"
dt.save(dtc_path)
dt2 = DecisionTreeClassifier.load(dtc_path)
dt2.getMaxDepth()
# 2
model_path = temp_path + "/dtc_model"
model.save(model_path)
model2 = DecisionTreeClassificationModel.load(model_path)
model.featureImportances == model2.featureImportances
# True
Posted by: Guest on April-03-2020
0

Decision tree learning algorithm for regression

# Decision tree learning algorithm for regression

from pyspark.ml.linalg import Vectors
df = spark.createDataFrame([
  (1.0, Vectors.dense(1.0)),
  (0.0, Vectors.sparse(1, [], []))], ["label", "features"])
dt = DecisionTreeRegressor(maxDepth=2, varianceCol="variance")
model = dt.fit(df)
model.depth
# 1
model.numNodes
# 3
model.featureImportances
# SparseVector(1, {0: 1.0}
model.numFeatures
# 1
test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"])
model.transform(test0).head().prediction
# 0.0
test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"])
model.transform(test1).head().prediction
# 1.0
dtr_path = temp_path + "/dtr"
dt.save(dtr_path)
dt2 = DecisionTreeRegressor.load(dtr_path)
dt2.getMaxDepth()
# 2
model_path = temp_path + "/dtr_model"
model.save(model_path)
model2 = DecisionTreeRegressionModel.load(model_path)
model.numNodes == model2.numNodes
# True
model.depth == model2.depth
# True
model.transform(test1).head().variance
# 0.0
Posted by: Guest on March-27-2020
-1

decision trees

Training Data Set Accuracy:  0.9610705596107056
Training Data F1 Score  0.972027972027972
Validation Mean F1 Score:  0.6348494236272646
Validation Mean Accuracy:  0.7030561269468117
Posted by: Guest on June-18-2021

Code answers related to "decision tree"

Python Answers by Framework

Browse Popular Code Answers by Language