from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
for name, model in [
("Tree depth=3", DecisionTreeClassifier(max_depth=3, random_state=42)),
("RandomForest", RandomForestClassifier(n_estimators=100, random_state=42)),
]:
model.fit(X_train, y_train)
acc = accuracy_score(y_test, model.predict(X_test))
print(f"{name}: {acc:.2f}")
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
for name, model in [
("Tree depth=3", DecisionTreeClassifier(max_depth=3, random_state=42)),
("RandomForest", RandomForestClassifier(n_estimators=100, random_state=42)),
]:
model.fit(X_train, y_train)
acc = accuracy_score(y_test, model.predict(X_test))
print(f"{name}: {acc:.2f}")