Loading…
DS · Classification · Program 17 · In the record
Decision Tree classification.
1from sklearn.datasets import load_iris2from sklearn.model_selection import train_test_split3from sklearn.tree import DecisionTreeClassifier, export_text4from sklearn.metrics import accuracy_score5 6iris = load_iris()7X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)8 9dt = DecisionTreeClassifier(criterion='gini', max_depth=3, random_state=42).fit(X_train, y_train)10print("Decision Tree Accuracy:", accuracy_score(y_test, dt.predict(X_test)))11print("\nDecision Rules:\n" + export_text(dt, feature_names=iris.feature_names))Decision Tree Accuracy: 1.0 Decision Rules: |--- petal length (cm) <= 2.45 | |--- class: 0 |--- petal length (cm) > 2.45 | |--- petal length (cm) <= 4.75 | | |--- petal width (cm) <= 1.60 | | | |--- class: 1 | | |--- petal width (cm) > 1.60 | | | |--- class: 2 | |--- petal length (cm) > 4.75 | | |--- petal width (cm) <= 1.75 | | | |--- class: 1 | | |--- petal width (cm) > 1.75 | | | |--- class: 2

Run program 17 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.