Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeImplementationSupervised LearningClassificationPython scikit-learn library for Decision Tree model
Classification

Python scikit-learn library for Decision Tree model

March 7, 2024March 7, 2024CEO 415 views

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))

classifier, decision tree, metrics, sklearn

Post navigation

Previous Post
Previous post: Import your functions library to a Google Colab notebook
Next Post
Next post: Visualizing the Decision Tree

You Might Also Like

No image
BaggingClassifier from Scikit-Learn
April 7, 2024 Comments Off on BaggingClassifier from Scikit-Learn
No image
Parameter stratify from method train_test_split in scikit…
April 7, 2024 Comments Off on Parameter stratify from method train_test_split in scikit Learn
No image
Standardizing features by StandardScaler
March 11, 2024 Comments Off on Standardizing features by StandardScaler
No image
Choosing the right estimator
March 10, 2024 Comments Off on Choosing the right estimator
No image
Post-pruning Decision Tree with Cost Complexity Parameter…
March 8, 2024 Comments Off on Post-pruning Decision Tree with Cost Complexity Parameter ccp_alpha
  • Recent
  • Popular
  • Random
  • No image
    2 years ago Low-Rank Factorization
  • No image
    2 years ago Perturbation Test for a Regression Model
  • No image
    2 years ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    May 5, 2024MNIST dataset in artificial neural network
  • No image
    March 7, 2024Visualizing the Decision Tree
  • No image
    January 16, 2024Train-and-test isn’t the only approach
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2026 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS