0% found this document useful (0 votes)
54 views3 pages

Decision Tree Classifier Analysis

This document loads and pre-processes a dataset, trains a decision tree classifier on the data, evaluates the model's performance, and plots the trained decision tree. It pre-processes the data by handling missing values, encoding categorical features and the target, and splitting the data into train and test sets. It then trains a decision tree classifier on the training data, evaluates it on the test data by measuring accuracy, classification report, and confusion matrix, and plots the trained decision tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Decision Tree Classifier Analysis

This document loads and pre-processes a dataset, trains a decision tree classifier on the data, evaluates the model's performance, and plots the trained decision tree. It pre-processes the data by handling missing values, encoding categorical features and the target, and splitting the data into train and test sets. It then trains a decision tree classifier on the training data, evaluates it on the test data by measuring accuracy, classification report, and confusion matrix, and plots the trained decision tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/27/24, 3:59 PM Amit_Decision_Tree_Precision_Engg.

ipynb - Colab

from google.colab import drive


drive.mount('/content/drive')

Mounted at /content/drive

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler, LabelEncoder

# Load the data


# file_path = '/content/drive/MyDrive/Group10 (2).csv'
file_path = '/content/drive/MyDrive/IMC 2024/Group10.csv'
data = pd.read_csv(file_path)

# Display the first few rows of the dataset to understand its structure
data.head()

X1_ActualPosition X1_ActualVelocity X1_ActualAcceleration X1_CommandPosition X

0 146.0 12.1 106.0 146.0

1 147.0 11.9 -100.0 147.0

2 148.0 12.3 100.0 148.0

3 149.0 11.9 -81.2 149.0

4 151.0 11.7 -150.0 151.0

5 rows × 54 columns

#Pre-Processing
# Handling missing values (simple imputation with the most frequent value)
data.fillna(data.mode().iloc[0], inplace=True)

# Encoding categorical features and the target variable


label_encoders = {}
categorical_columns = ['material', 'tool_condition', 'Machining_Process', 'machining_finalized', 'passed_visual_inspection']
for col in categorical_columns:
le = LabelEncoder()
data[col] = le.fit_transform(data[col])
label_encoders[col] = le

# Splitting the dataset into features and target variable


X = data.drop(columns=['machining_finalized'])
y = data['machining_finalized']

# Splitting the data into train and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Training a Decision Tree classifier


clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Predicting and evaluating the model


y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
matrix = confusion_matrix(y_test,y_pred)

print("Accuracy:", accuracy)
print("Classification Report:\n", report)
print("Confusion Matrix:\n", matrix)

Accuracy: 0.7088122605363985
Classification Report:
precision recall f1-score support

0 0.15 0.17 0.16 42


1 0.84 0.81 0.82 219

accuracy 0.71 261


macro avg 0.49 0.49 0.49 261
weighted avg 0.72 0.71 0.72 261

Confusion Matrix:
[[ 7 35]
[ 41 178]]

https://colab.research.google.com/drive/17XCgWXUe44a1gPbZu0x4pOM2pOD4dWNG?authuser=1#printMode=true 1/3
4/27/24, 3:59 PM Amit_Decision_Tree_Precision_Engg.ipynb - Colab

import matplotlib.pyplot as plt


import sklearn.tree as tree

fig, axes = plt.subplots (nrows = 1,ncols = 1,figsize = (16,16), dpi=1024)


tree.plot_tree(clf)
fig.savefig('decision_tree.png')

https://colab.research.google.com/drive/17XCgWXUe44a1gPbZu0x4pOM2pOD4dWNG?authuser=1#printMode=true 2/3
4/27/24, 3:59 PM Amit_Decision_Tree_Precision_Engg.ipynb - Colab

account_circle

Start coding or generate with AI.

https://colab.research.google.com/drive/17XCgWXUe44a1gPbZu0x4pOM2pOD4dWNG?authuser=1#printMode=true 3/3

You might also like