0% found this document useful (0 votes)
9 views

HousepricedataDT - Ipynb - Colab

Uploaded by

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

HousepricedataDT - Ipynb - Colab

Uploaded by

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

6/23/24, 11:36 PM HousepricedataDT.

ipynb - Colab

# Load libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation

col_names = ['lotarea', 'qual', 'cond', 'bsmt', 'fullbath', 'halfbath', 'bedroom', 'trag', 'fireplaces','garage','label']
# load dataset
pima = pd.read_csv("housepricedata.csv", header=0, names=col_names)
pima.head()

lotarea qual cond bsmt fullbath halfbath bedroom trag fireplaces garage label

0 8450 7 5 856 2 1 3 8 0 548 1

1 9600 6 8 1262 2 0 3 6 1 460 1

2 11250 7 5 920 2 1 3 6 1 608 1

3 9550 7 5 756 1 0 3 7 1 642 0

4 14260 8 5 1145 2 1 4 9 1 836 1

#split dataset in features and target variable


feature_cols = ['lotarea', 'qual', 'cond', 'bsmt', 'fullbath', 'halfbath', 'bedroom', 'trag', 'fireplaces','garage']#feature selection
X = pima[feature_cols] # Features
y = pima.label # Target variable
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test
# Create Decision Tree classifer object
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer


clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

Accuracy: 0.8538812785388128

!pip install pydotplus


import pydotplus as pydotplus

Requirement already satisfied: pydotplus in /usr/local/lib/python3.10/dist-packages (2.0.2)


Requirement already satisfied: pyparsing>=2.0.1 in /usr/local/lib/python3.10/dist-packages (from pydotplus) (3.1.2)

from IPython.display import Image

https://colab.research.google.com/drive/16i-7WHnG1IQYCC6-WOlbeZEPwLZLcChd#printMode=true 1/3
6/23/24, 11:36 PM HousepricedataDT.ipynb - Colab
from six import StringIO
from sklearn.tree import export_graphviz
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('housedata.png')
Image(graph.create_png())

# Create Decision Tree classifer object


clf = DecisionTreeClassifier(criterion="entropy", max_depth=3)
# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

Accuracy: 0.8538812785388128

https://colab.research.google.com/drive/16i-7WHnG1IQYCC6-WOlbeZEPwLZLcChd#printMode=true 2/3
6/23/24, 11:36 PM HousepricedataDT.ipynb - Colab
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('advertising.png')
Image(graph.create_png())

https://colab.research.google.com/drive/16i-7WHnG1IQYCC6-WOlbeZEPwLZLcChd#printMode=true 3/3

You might also like