Decision trees are a non-parametric supervised learning method used for both classification and regression tasks. They model decisions and their possible consequences in a tree-like structure, where internal nodes represent tests on features, branches represent the outcome of the test, and leaf nodes represent the final prediction (class label or value).
For classification, decision trees use measures like Gini impurity or entropy to split the data:
- Gini Impurity: Measures the likelihood of an incorrect classification of a randomly chosen element.
- Entropy (Information Gain): Measures the amount of uncertainty or impurity in the data.
For regression, decision trees minimize the variance (mean squared error) in the splits.
## Implementation Example
Suppose we have a dataset with features like age, income, and student status to predict whether a person buys a computer.
# Import necessary libraries
Results:
The accuracy is 0.01. Correct this (LEFT as EXERCISE).
Plot:
#### Explanation of the Code
1. Libraries: We import necessary libraries like numpy, pandas, sklearn, and matplotlib.
2. Data Preparation: We create a DataFrame containing features and the target variable. Categorical features are converted to numeric values.
3. Feature and Target: We separate the features (Age, Income, Student) and the target (Buys_Computer).
4. Train-Test Split: We split the data into training and testing sets.
5. Model Training: We create a DecisionTreeClassifier model, specifying the criterion (Gini impurity) and maximum depth of the tree, and train it using the training data.
6. Predictions: We use the trained model to predict whether a person buys a computer for the test set.
7. Evaluation: Evaluate the model using accuracy, confusion matrix, and classification report.
8. Visualization: Plot decision tree to visualize the decision-making process.
## Evaluation Metrics
- Accuracy
- Confusion Matrix: Shows the counts of true positives, true negatives, false positives, and false negatives.
- Classification Report: Provides precision, recall, F1-score, and support for each class.
Like if you need similar content 😄👍
Hope this helps you 😊
No comments:
Post a Comment