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 158 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
    7 months ago Low-Rank Factorization
  • No image
    7 months ago Perturbation Test for a Regression Model
  • No image
    7 months 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
    January 18, 2024What is NumPy?
  • No image
    March 11, 2024What are the common Distance Measures in…
  • No image
    January 16, 2024Improve model with hyperparameters
  • 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)
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS