0% found this document useful (0 votes)
1K views7 pages

2021BCS0103

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)
1K views7 pages

2021BCS0103

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
You are on page 1/ 7

ICS-322 Machine Learning Assignment-1

Singareddy Vaishnavi Reddy

2021BCS0103

# Importing all libraries


import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score,
recall_score

data = pd.DataFrame({
'Study Hours':[3, 5, 8, 2, 6, 9, 4],
'Previous Exam Scores':[70, 65, 80, 50, 75, 85, 60],
'Result':['Fail','Fail','Pass','Fail','Pass','Pass','Fail']
})

data.head()

Study Hours Previous Exam Scores Result


0 3 70 Fail
1 5 65 Fail
2 8 80 Pass
3 2 50 Fail
4 6 75 Pass

#Split the dataset into features (x) and target variable(y)


x = data[['Study Hours','Previous Exam Scores']]
y = data['Result']

# Split the dataset into training and testing sets


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size =
0.2, random_state = 42)

# a.) Building of decision tree with depth of 3


tree_depth_3 = DecisionTreeClassifier(max_depth = 3)
tree_depth_3.fit(x_train, y_train)

DecisionTreeClassifier(max_depth=3)

actual_depth_3 = tree_depth_3.get_depth()
print(f"The actual depth of the decision tree with depth 3:
{actual_depth_3}")

The actual depth of the decision tree with depth 3: 1

# b.) Evaluating the model of depth 3

y_pred_depth_3 = tree_depth_3.predict(x_test)
print(y_pred_depth_3)
['Fail' 'Fail']

accuracy_depth_3 = accuracy_score(y_test, y_pred_depth_3)


print(accuracy_depth_3)

1.0

precision_depth_3 = precision_score(y_test, y_pred_depth_3)


print(precision_depth_3)

0.0

/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py:608:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
mask &= (ar1 != a)
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1344: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 due to no predicted samples. Use `zero_division` parameter
to control this behavior.
_warn_prf(average, modifier, msg_start, len(result))

recall_depth_3 = recall_score(y_test, y_pred_depth_3)


print(recall_depth_3)

0.0

/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py:608:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
mask &= (ar1 != a)
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1344: UndefinedMetricWarning: Recall is ill-defined and being set
to 0.0 due to no true samples. Use `zero_division` parameter to
control this behavior.
_warn_prf(average, modifier, msg_start, len(result))

# c.) Building a decision tree with a depth of 5


tree_depth_5 = DecisionTreeClassifier(max_depth = 5)
tree_depth_5.fit(x_train, y_train)

DecisionTreeClassifier(max_depth=5)

actual_depth_5 = tree_depth_5.get_depth()
print(f"The actual depth of the decision tree with depth 5:
{actual_depth_5}")

The actual depth of the decision tree with depth 5: 1

# d.) Evaluating the model of depth 5


y_pred_depth_5 = tree_depth_5.predict(x_test)
print(y_pred_depth_5)

['Fail' 'Fail']

accuracy_depth_5 = accuracy_score(y_test, y_pred_depth_5)


print(accuracy_depth_5)

1.0

precision_depth_5 = precision_score(y_test, y_pred_depth_5)


print(precision_depth_5)

0.0

/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py:608:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
mask &= (ar1 != a)
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1344: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 due to no predicted samples. Use `zero_division` parameter
to control this behavior.
_warn_prf(average, modifier, msg_start, len(result))

recall_depth_5 = recall_score(y_test, y_pred_depth_5)


print(recall_depth_5)

0.0

/usr/local/lib/python3.10/dist-packages/numpy/lib/arraysetops.py:608:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
mask &= (ar1 != a)
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1344: UndefinedMetricWarning: Recall is ill-defined and being set
to 0.0 due to no true samples. Use `zero_division` parameter to
control this behavior.
_warn_prf(average, modifier, msg_start, len(result))

Analysis

-For the given dataset, both the trees constructed hhas its actual depth as 1. Therefore, there is
no difference in the performance of the decision tree with max-depth of 3 and the decision tree
with max- depth of 5.

-Since the size of the dataset is very small. Therefore, the chances of dataset to face overfitting
is very high.

-The accuracy obtained from both the dataset is 1.

-To analyse about the optimal depth of tree we should increase the size of the dataset.

You might also like