11.
DECISION TREE LEARNING
A)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from sklearn.model_selection import train_test_split, cross_val_score
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.head()
B)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from sklearn.model_selection import train_test_split, cross_val_score
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
x = df[['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width
(cm)']].values
y = df['target'].values
y = y.reshape(-1, 1)
print(x.shape)
print(y.shape)
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0,
test_size=0.2)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
clf = DecisionTreeClassifier(max_depth=3, random_state=0)
clf.fit(x_train, y_train)
prediction = clf.predict(x_test)
score = clf.score(x_test, y_test)
print('Accuracy score: {}'.format(score))