# -*- coding: utf-8 -*-
"""DecisionTree.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1jp0cig39Z6BncnHmlvQmCcxX0vriWmJm
"""

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import plot_tree
from sklearn import tree

# import dataset
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df['target'] = data.target

#checking null values
df.isnull().sum()

# showing first 5 values
df.head()

df.shape

# showing only target data (Dependent Variable)
print(df['target'])

# splitting data

fc = [x for x in df.columns if x!='target']
x = df[fc]
y = df['target']
X_train, X_test, Y_train, Y_test = train_test_split(x,y, random_state = 100, test_size = 0.30)

# Display of data
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)

# building Decision tree model
model1 = DecisionTreeClassifier()

# Fitting the training model
model1.fit(X_train, Y_train)

Y_pred = model1.predict(X_test)

# Comparision
data2 = pd.DataFrame({"Actual":Y_test, "Predicted":Y_pred})
data2.head()

# Testing the accuracy of model prediction
accuracy_score(Y_test, Y_pred)

# Plotting
f_n = ["Sepal length", "Sepal width", "Petal length", "Petal width"]
c_n = ["Setosa", "Versicolor", "Virginica"]
plot_tree(model1, feature_names = f_n, class_names = c_n, filled = True)

modelx = DecisionTreeClassifier().fit(x,y)

plt.figure(figsize = (20,15))
tree = tree.plot_tree(modelx, feature_names= f_n, class_names = c_n, filled = True)

"""### Thank You"""
55bd55af1dcb4b262748ff332ac840c83ce73821
<mask token>

<mask token>
df.isnull().sum()
df.head()
df.shape
print(df['target'])
<mask token>
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
<mask token>
model1.fit(X_train, Y_train)
<mask token>
data2.head()
accuracy_score(Y_test, Y_pred)
<mask token>
plot_tree(model1, feature_names=f_n, class_names=c_n, filled=True)
<mask token>
plt.figure(figsize=(20, 15))
<mask token>

<mask token>
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
df.isnull().sum()
df.head()
df.shape
print(df['target'])
fc = [x for x in df.columns if x != 'target']
x = df[fc]
y = df['target']
X_train, X_test, Y_train, Y_test = train_test_split(x, y, random_state=100,
    test_size=0.3)
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
model1 = DecisionTreeClassifier()
model1.fit(X_train, Y_train)
Y_pred = model1.predict(X_test)
data2 = pd.DataFrame({'Actual': Y_test, 'Predicted': Y_pred})
data2.head()
accuracy_score(Y_test, Y_pred)
f_n = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width']
c_n = ['Setosa', 'Versicolor', 'Virginica']
plot_tree(model1, feature_names=f_n, class_names=c_n, filled=True)
modelx = DecisionTreeClassifier().fit(x, y)
plt.figure(figsize=(20, 15))
tree = tree.plot_tree(modelx, feature_names=f_n, class_names=c_n, filled=True)
<mask token>

<mask token>
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import plot_tree
from sklearn import tree
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
df.isnull().sum()
df.head()
df.shape
print(df['target'])
fc = [x for x in df.columns if x != 'target']
x = df[fc]
y = df['target']
X_train, X_test, Y_train, Y_test = train_test_split(x, y, random_state=100,
    test_size=0.3)
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
model1 = DecisionTreeClassifier()
model1.fit(X_train, Y_train)
Y_pred = model1.predict(X_test)
data2 = pd.DataFrame({'Actual': Y_test, 'Predicted': Y_pred})
data2.head()
accuracy_score(Y_test, Y_pred)
f_n = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width']
c_n = ['Setosa', 'Versicolor', 'Virginica']
plot_tree(model1, feature_names=f_n, class_names=c_n, filled=True)
modelx = DecisionTreeClassifier().fit(x, y)
plt.figure(figsize=(20, 15))
tree = tree.plot_tree(modelx, feature_names=f_n, class_names=c_n, filled=True)
<mask token>

# -*- coding: utf-8 -*-
"""DecisionTree.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1jp0cig39Z6BncnHmlvQmCcxX0vriWmJm
"""

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import plot_tree
from sklearn import tree

# import dataset
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df['target'] = data.target

#checking null values
df.isnull().sum()

# showing first 5 values
df.head()

df.shape

# showing only target data (Dependent Variable)
print(df['target'])

# splitting data

fc = [x for x in df.columns if x!='target']
x = df[fc]
y = df['target']
X_train, X_test, Y_train, Y_test = train_test_split(x,y, random_state = 100, test_size = 0.30)

# Display of data
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)

# building Decision tree model
model1 = DecisionTreeClassifier()

# Fitting the training model
model1.fit(X_train, Y_train)

Y_pred = model1.predict(X_test)

# Comparision
data2 = pd.DataFrame({"Actual":Y_test, "Predicted":Y_pred})
data2.head()

# Testing the accuracy of model prediction
accuracy_score(Y_test, Y_pred)

# Plotting
f_n = ["Sepal length", "Sepal width", "Petal length", "Petal width"]
c_n = ["Setosa", "Versicolor", "Virginica"]
plot_tree(model1, feature_names = f_n, class_names = c_n, filled = True)

modelx = DecisionTreeClassifier().fit(x,y)

plt.figure(figsize = (20,15))
tree = tree.plot_tree(modelx, feature_names= f_n, class_names = c_n, filled = True)

"""### Thank You"""