Skip to content

Instantly share code, notes, and snippets.

@lundquist-ecology-lab
Created January 16, 2023 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lundquist-ecology-lab/ee75ad238d78e3ae8e7493659a15b279 to your computer and use it in GitHub Desktop.
Save lundquist-ecology-lab/ee75ad238d78e3ae8e7493659a15b279 to your computer and use it in GitHub Desktop.
Example decision tree using Iris dataset in Python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load data
iris = load_iris()
X = iris.data
y = iris.target
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build decision tree
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# Make predictions on the test set
y_pred = clf.predict(X_test)
# Evaluate the model's performance
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment