For building a decision tree model in scikit-learn (sklearn), you need to import the relevant modules and classes. Here are the main components you’ll typically use:
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn import tree
from sklearn.metrics import accuracy_score
# Load your dataset and split it into features (X) and target variable (y)
# X_train, X_test, y_train, y_test = train_test_split(...)
# Create a decision tree classifier
clf = DecisionTreeClassifier(criterion='gini', random_state=1)
# Train the model
clf.fit(X_train, y_train)
# Make predictions on the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
#or alternatively
print("Accuracy on test set : ",clf.score(X_test, y_test))