Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
Flood Prediction
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Intelligent Tank Management System
Flood Prediction
Commits
09b3a3e9
Commit
09b3a3e9
authored
Jul 18, 2022
by
Mohamed Naseef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
26ac4ea2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
147 additions
and
0 deletions
+147
-0
decisiontree.py
decisiontree.py
+147
-0
No files found.
decisiontree.py
0 → 100644
View file @
09b3a3e9
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
import
math
import
copy
dataset
=
pd
.
read_csv
(
'datasetwether.csv'
)
X
=
dataset
.
iloc
[:,
1
:]
.
values
print
(
X
)
attribute
=
[
'Humidity'
,
'Wind Bearing (degrees)'
,
'Pressure (millibars)'
,
'Loud Cover'
]
class
Node
(
object
):
def
__init__
(
self
):
self
.
value
=
None
self
.
decision
=
None
self
.
childs
=
None
def
findEntropy
(
data
,
rows
):
yes
=
0
no
=
0
ans
=
-
1
idx
=
len
(
data
[
0
])
-
1
entropy
=
0
for
i
in
rows
:
if
data
[
i
][
idx
]
==
'this year low chance'
:
yes
=
yes
+
1
else
:
no
=
no
+
1
x
=
yes
/
(
yes
+
no
)
y
=
no
/
(
yes
+
no
)
if
x
!=
0
and
y
!=
0
:
entropy
=
-
1
*
(
x
*
math
.
log2
(
x
)
+
y
*
math
.
log2
(
y
))
if
x
==
1
:
ans
=
1
if
y
==
1
:
ans
=
0
return
entropy
,
ans
def
findMaxGain
(
data
,
rows
,
columns
):
maxGain
=
0
retidx
=
-
1
entropy
,
ans
=
findEntropy
(
data
,
rows
)
if
entropy
==
0
:
if
ans
==
1
:
print
(
"hight chance"
)
else
:
print
(
""
)
return
maxGain
,
retidx
,
ans
for
j
in
columns
:
mydict
=
{}
idx
=
j
for
i
in
rows
:
key
=
data
[
i
][
idx
]
if
key
not
in
mydict
:
mydict
[
key
]
=
1
else
:
mydict
[
key
]
=
mydict
[
key
]
+
1
gain
=
entropy
# print(mydict)
for
key
in
mydict
:
yes
=
0
no
=
0
for
k
in
rows
:
if
data
[
k
][
j
]
==
key
:
if
data
[
k
][
-
1
]
==
'Yes'
:
yes
=
yes
+
1
else
:
no
=
no
+
1
# print(yes, no)
x
=
yes
/
(
yes
+
no
)
y
=
no
/
(
yes
+
no
)
#print(x, y)
if
x
!=
0
and
y
!=
0
:
gain
+=
(
mydict
[
key
]
*
(
x
*
math
.
log2
(
x
)
+
y
*
math
.
log2
(
y
)))
/
14
# print(gain)
if
gain
>
maxGain
:
# print("hello")
maxGain
=
gain
retidx
=
j
return
maxGain
,
retidx
,
ans
def
buildTree
(
data
,
rows
,
columns
):
maxGain
,
idx
,
ans
=
findMaxGain
(
X
,
rows
,
columns
)
root
=
Node
()
root
.
childs
=
[]
print
(
maxGain
)
if
maxGain
==
0
:
if
ans
==
1
:
root
.
value
=
'Yes'
else
:
root
.
value
=
'No'
return
root
root
.
value
=
attribute
[
idx
]
mydict
=
{}
for
i
in
rows
:
key
=
data
[
i
][
idx
]
if
key
not
in
mydict
:
mydict
[
key
]
=
1
else
:
mydict
[
key
]
+=
1
newcolumns
=
copy
.
deepcopy
(
columns
)
newcolumns
.
remove
(
idx
)
for
key
in
mydict
:
newrows
=
[]
for
i
in
rows
:
if
data
[
i
][
idx
]
==
key
:
newrows
.
append
(
i
)
# print(newrows)
temp
=
buildTree
(
data
,
newrows
,
newcolumns
)
temp
.
decision
=
key
root
.
childs
.
append
(
temp
)
return
root
def
traverse
(
root
):
print
(
root
.
decision
)
print
(
root
.
value
)
n
=
len
(
root
.
childs
)
if
n
>
0
:
for
i
in
range
(
0
,
n
):
traverse
(
root
.
childs
[
i
])
def
calculate
():
rows
=
[
i
for
i
in
range
(
0
,
14
)]
columns
=
[
i
for
i
in
range
(
0
,
4
)]
root
=
buildTree
(
X
,
rows
,
columns
)
root
.
decision
=
'next year'
traverse
(
root
)
return
root
.
decision
+
" "
+
root
.
value
calculate
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment