Decision Tree using Python With Random Data

Import all required libraries

— — — — — — — — — — — — —

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

from sklearn import tree

import matplotlib.pyplot as plt

import random

import os

— — — — — — — — — — — — — — — — — — – — ———————-

Define a Data Frame with three attributes named A, B, C, and fourth as decision variables, D.

Here, some random data and random decision classes are created and initialized, and taken over in order to

— — — — — — — — — — — — — — — — — — — — — — — — — — —

In this

DF = pd.DataFrame(columns=[‘A’,’B’,’C’, ‘D’])

— — — — — — — — — — — — — — — — — — — — — — — — — — —

Attribute defining 100 data entry points, taking value from the random choice of 85, 90, 95, 98, 100, 104

— — — — — — — — — — — — — — — — — — — — — — — — — — —

AttrubuteA1 = np.empty(100)
for i in range(0,99):
AttrubuteA1[i] = random.choice([100,90, 95, 98, 85, 104])

— — — — — — — — — — — — — — — — — — — — — — — — — — —

Second & Third Attribute defined in the same way

AttrubuteA2 = np.empty(100)
for i in range(0,99):
AttrubuteA2[i] = ….

AttrubuteA3 = np.empty(100)
for i in range(0,99):
AttrubuteA3[i] =….

— — — — — — — — — — — — — — — — — — — — — — — — — — —

Defining Decision Class with values of two decisions as defined here

decisionClass = np.empty(100)
for i in range(0,99):
decisionClass[i] = random.choice([1, 2])

DF = DF.assign(A=AttrubuteA1)

DF = DF.assign(B=AttrubuteA2)

DF = DF.assign(C=AttrubuteA3)

DF = DF.assign(D=decisionClass)

— — — — — — — — — — — — — — — — — — — — — — — — — — —

Copying the generated data into csv file

— — — — — — — — — — — — — — — -— — — — — — — — — — — — — –

DF.to_csv(“TheFinalDataCSV.csv”)

data = DF
print(data)

data = data.dropna()
X = data[[‘A’, ‘B’, ‘C’ ]]
y = data[[‘D’]]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=65)

decisionTable = DecisionTreeClassifier(max_depth=2)
decisionTableModel = decisionTable.fit(x_train, y_train)

predictions = decisionTable.predict(x_test)
print(predictions[:50])
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, predictions))

text_representation = tree.export_text(decisionTable)
print(text_representation)

fig = plt.figure(figsize=(25,20))
class_names = [‘1’, ‘2’]
_ = tree.plot_tree(decisionTable, feature_names=X.columns,
class_names=class_names )

Sample Output

Depiction of Sample Rules Generated

Published by Nidhika

Hi, I have an excellent problems solving skills in the domain of Engineering, Sciences, and Modelling Solutions for any topic. I gained my strong problem-solving skills with the problems I solved in elite schools with top grades, and hence can now think in direction of problem solving as a generic thing. Apart from profession, I have inherent interest in writing especially about Global Issues of Concern, poems, stories, doing painting, cooking, photography, music to mention a few! And most important on my website you can find my suggestions to latest problems, views and ideas, my poems, stories, novels, my comments, my proposals for people with funding's to implement, my blogs, my interests, my personal experiences and glimpses of my research work as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: