[go: up one dir, main page]

0% found this document useful (0 votes)
5 views1 page

Exp11 Decision Tree Learning

The document outlines the implementation of Decision Tree Learning using the Iris dataset in Python. It includes data loading, preprocessing, splitting into training and testing sets, and training a Decision Tree Classifier with a specified maximum depth. Finally, it evaluates the model's accuracy on the test set.

Uploaded by

jnakka197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Exp11 Decision Tree Learning

The document outlines the implementation of Decision Tree Learning using the Iris dataset in Python. It includes data loading, preprocessing, splitting into training and testing sets, and training a Decision Tree Classifier with a specified maximum depth. Finally, it evaluates the model's accuracy on the test set.

Uploaded by

jnakka197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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))

You might also like