You are on page 1of 3

3/2/23, 3:29 PM Untitled - Jupyter Notebook

In [1]:

import pandas

df = pandas.read_csv("astronaut.csv")

print(df)

Age Likes dogs Likes gravity Going to be an astronaut


0 24 0 0 0
1 30 1 1 1
2 36 0 1 1
3 36 0 0 0
4 42 0 0 0
5 44 1 1 1
6 46 1 0 0
7 47 1 1 1
8 47 0 1 0
9 51 1 1 1

In [11]:

features = ['Age', 'Likes dogs', 'Likes gravity']

X = df[features]
y = df['Going to be an astronaut']
print("FEATURE COLUMNS")
print(X)
print("TARGET COLUMNS")
print(y)

FEATURE COLUMNS
Age Likes dogs Likes gravity
0 24 0 0
1 30 1 1
2 36 0 1
3 36 0 0
4 42 0 0
5 44 1 1
6 46 1 0
7 47 1 1
8 47 0 1
9 51 1 1
TARGET COLUMNS
0 0
1 1
2 1
3 0
4 0
5 1
6 0
7 1
8 0
9 1
Name: Going to be an astronaut, dtype: int64

localhost:8888/notebooks/Documents/Untitled Folder/Untitled.ipynb?kernel_name=python3 1/3


3/2/23, 3:29 PM Untitled - Jupyter Notebook

In [17]:

import pandas
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt

df = pandas.read_csv("astronaut.csv")

dtree = DecisionTreeClassifier()
dtree = dtree.fit(X.values, y)
print("PLOTTED DECISION TREE : ")
tree.plot_tree(dtree, feature_names=features)

PLOTTED DECISION TREE :

Out[17]:

[Text(0.4, 0.875, 'Likes gravity <= 0.5\ngini = 0.5\nsamples = 10\nvalue =


[5, 5]'),
Text(0.2, 0.625, 'gini = 0.0\nsamples = 4\nvalue = [4, 0]'),
Text(0.6, 0.625, 'Likes dogs <= 0.5\ngini = 0.278\nsamples = 6\nvalue =
[1, 5]'),
Text(0.4, 0.375, 'Age <= 41.5\ngini = 0.5\nsamples = 2\nvalue = [1, 1]'),
Text(0.2, 0.125, 'gini = 0.0\nsamples = 1\nvalue = [0, 1]'),
Text(0.6, 0.125, 'gini = 0.0\nsamples = 1\nvalue = [1, 0]'),
Text(0.8, 0.375, 'gini = 0.0\nsamples = 4\nvalue = [0, 4]')]

localhost:8888/notebooks/Documents/Untitled Folder/Untitled.ipynb?kernel_name=python3 2/3


3/2/23, 3:29 PM Untitled - Jupyter Notebook

In [18]:

n = int(input("ENTER NUMBER OF TESTCASES : "))


print(n)
for i in range(n):
agee = input("ENTER AGE : ")
likesdogs = input("ENTER VALUE(0/1) : ")
likesgravity = input("ENTER VALUE(0/1) : ")
val = dtree.predict([[agee,likesdogs,likesgravity]])
if(val==0):
print("NO")
else:
print("YES")

ENTER NUMBER OF TESTCASES : 5


5
ENTER AGE : 40
ENTER VALUE(0/1) : 0
ENTER VALUE(0/1) : 0
NO
ENTER AGE : 50
ENTER VALUE(0/1) : 1
ENTER VALUE(0/1) : 0
NO
ENTER AGE : 45
ENTER VALUE(0/1) : 0
ENTER VALUE(0/1) : 1
NO
ENTER AGE : 40
ENTER VALUE(0/1) : 1
ENTER VALUE(0/1) : 1
YES
ENTER AGE : 60
ENTER VALUE(0/1) : 1
ENTER VALUE(0/1) : 0
NO

In [ ]:

localhost:8888/notebooks/Documents/Untitled Folder/Untitled.ipynb?kernel_name=python3 3/3

You might also like