9951 explained code solutions for 126 technologies


python-scikit-learnHow to get decision tree classifier accuracy score


from sklearn import datasets, tree, model_selection, metrics

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = tree.DecisionTreeClassifier()
model.fit(X_train, y_train)

accuracy_score = metrics.accuracy_score(model.predict(X_test), y_test)ctrl + c
from sklearn import

import module from scikit-learn

load_iris

loads Iris dataset

model_selection.train_test_split

splits given X and y datasets to test (25% of values by default) and train (75% of values by default) subsets

.DecisionTreeClassifier(

creates decision tree classifier model

.fit(

train model with a given features and target variable dataset

.predict(

predict target variable based on given features dataset

.accuracy_score(

accuracy classification score


Usage example

from sklearn import datasets, tree, model_selection, metrics

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = tree.DecisionTreeClassifier()
model.fit(X_train, y_train)

accuracy_score = metrics.accuracy_score(model.predict(X_test), y_test)
print(accuracy_score)
output
0.9210526315789473