Created
January 16, 2023 22:31
-
-
Save lundquist-ecology-lab/ee75ad238d78e3ae8e7493659a15b279 to your computer and use it in GitHub Desktop.
Example decision tree using Iris dataset in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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