Ravindra BagaleCourses & study guides English Track your progress

35. Decision Trees आणि Random Forests — Python मध्ये

Data Science

35.3 छोटे Decision Tree Copy-Run

का गरजेचे आहे. छोटे tree fit करा जेणेकरून fit / predict आणि optional text rules दिसतील.

हे काय आहे. max_depth सह DecisionTreeClassifier beginners साठी tree लहान ठेवते.

कसे करायचे.

छोटा tree copy-run

  1. छोटे churn-style table पुन्हा वापरा.
  2. max_depth=3 सह fit करा.
  3. Test rows वर predict करा.
  4. Optional: rules print करण्यासाठी export_text.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text

df = pd.DataFrame({
 "orders_last_30d": [2, 8, 1, 12, 3, 9, 0, 11, 4, 7, 1, 10],
 "complaints": [1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0],
 "churned": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
})
X = df[["orders_last_30d", "complaints"]]
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
 X, y, test_size=0.33, random_state=0, stratify=y
)

tree = DecisionTreeClassifier(max_depth=3, random_state=0)
tree.fit(X_train, y_train)
print("preds:", tree.predict(X_test))
print(export_text(tree, feature_names=list(X.columns)))

तुम्हाला काय दिसायला हवे. Predictions आणि छोटी indented rule यादी. Depth 3 वाचता येण्यासारखे राहते.