-
Notifications
You must be signed in to change notification settings - Fork 0
/
prodigy_task_3.py
93 lines (64 loc) · 2.45 KB
/
prodigy_task_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""Prodigy_task_3.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1rN6byQUe2Zp72hPQyZPo4F7lthBv_FUf
# **Prodigy DS Internship Task 3**
## Making a decision tree classifier
## **Import necessary libraries**
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from sklearn import tree
"""# **Loading of data**"""
d=pd.read_csv('/content/bank data - bank-full - bank data - bank-full (1).csv', header=0, sep=',');
df=pd.DataFrame(data=d)
df.head
"""# **Dropping the empty columns**"""
df.dropna(axis=0,inplace=True)
df
df.info()
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['job'] = le.fit_transform(df['job'])
df['job']=df['job'].astype(float)
df['marital'] = le.fit_transform(df['marital'])
df['marital']=df['marital'].astype(float)
df['education'] = le.fit_transform(df['education'])
df['education']=df['education'].astype(float)
df['poutcome'] = le.fit_transform(df['poutcome'])
df['poutcome']=df['poutcome'].astype(float)
df['month'] = le.fit_transform(df['month'])
df['month']=df['month'].astype(float)
df['contact'] = le.fit_transform(df['contact'])
df['contact']=df['contact'].astype(float)
df['housing'] = le.fit_transform(df['housing'])
df['housing']=df['housing'].astype(float)
df['default'] = le.fit_transform(df['default'])
df['default']=df['default'].astype(float)
df['loan'] = le.fit_transform(df['loan'])
df['loan']=df['loan'].astype(float)
df.info()
"""# **Defining X and Y for test and train split**"""
X=df.values[:,1:16]
print('These are the X values')
print(X)
Y=df.values[:,16]
print('These are the Y values')
print(Y)
"""# **Test-Train split**"""
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=123, shuffle=True)
"""# **Model Fitting**"""
mytree=DecisionTreeClassifier(criterion='gini',max_depth=4)
mytree.fit(X_train,Y_train)
pred=mytree.predict(X_test)
print(classification_report(Y_test, pred))
print("Our model has achieved an accuracy of ", accuracy_score(Y_test, pred)*100, "%", sep="")
from sklearn import tree
plt.figure(figsize=(25, 25))
tree.plot_tree(mytree, rounded=False,filled=True,fontsize=10)
plt.show()