-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_tree_classifier_and_random_forest_classifier.py
62 lines (40 loc) · 1.29 KB
/
decision_tree_classifier_and_random_forest_classifier.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
# -*- coding: utf-8 -*-
"""Decision Tree Classifier and Random Forest Classifier.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1gzUvU-Prjp9ZGHiPEd0xKOn3MDGHLFdl
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier,export_graphviz
import graphviz
from sklearn.model_selection import train_test_split
df=pd.read_csv("heart.csv")
df.head(3)
"""# Normal Method"""
y=df["output"]
x=df.drop("output",axis=1)
tree=DecisionTreeClassifier()
model=tree.fit(x,y)
model.score(x,y)
"""# With Train Test Split Method"""
x_train,x_test,y_train,y_test=train_test_split(x,y,train_size=0.70,random_state=16)
tree=DecisionTreeClassifier()
model=tree.fit(x_train,y_train)
model.score(x_train,y_train)
model.score(x_test,y_test)
model.predict([[63,1,3,145,233,1,0,150,0,2.3,0,0,1]])
"""# Visualization"""
dot=export_graphviz(model,feature_names=x.columns,filled=True)
gorsel=graphviz.Source(dot)
gorsel
"""# Random Forest Classifier"""
from sklearn.ensemble import RandomForestClassifier
forest=RandomForestClassifier()
model=forest.fit(x,y)
model.score(x,y)
forest=RandomForestClassifier(n_estimators=400,max_depth=4)
model=forest.fit(x_train,y_train)
model.score(x_test,y_test)
df.info()