-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
122 lines (87 loc) · 2.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from Cluster import Cluster
from Point import Point
import numpy as np
import math
from sklearn import datasets
def removeNeighbour(item):
if item in neighbours:
neighbours.remove(item)
def removePoint(item, list):
if item in list:
unvisited_points.remove(item)
def distance(first, second):
return math.sqrt(
((first.x() - second.x()) ** 2) +
((first.y() - second.y()) ** 2) +
((first.z() - second.z()) ** 2) +
((first.o() - second.o()) ** 2)
)
"""
Read dataset from text file
"""
iris = datasets.load_iris()
dataset = iris.data
epsilon = 0.39
minPoints = 4
clusters = []
"""List of points"""
points = []
unvisited_points = []
noise_points = []
"""
Convert dataset to objects
"""
for data in dataset:
p = Point(data[0], data[1], data[2], data[3])
points.append(p)
unvisited_points.append(p)
while len(unvisited_points) > 0:
neighbours = []
"""Select Random start point"""
start_point_index = np.random.randint(0, len(unvisited_points))
start_point = unvisited_points[start_point_index]
start_point.setVisited(True)
if start_point in unvisited_points:
unvisited_points.remove(start_point)
"""Find start point neighbourhoods"""
for point in points:
if distance(point, start_point) <= epsilon:
neighbours.append(point)
removeNeighbour(start_point)
"""Check if neighbours less than min points"""
if len(neighbours) < minPoints:
noise_points.append(start_point)
removePoint(start_point, unvisited_points)
else:
"""Else if neighbours equal or more than min points"""
"""Make a new cluster"""
cluster = Cluster()
"""Add neighbours to created cluster"""
cluster.addPoints(neighbours)
cluster.addPoint(start_point)
clusters.append(cluster)
"""Check all neighbours from selected start point"""
while len(neighbours) > 0:
"""Pop a neighbour"""
selected_neighbour = neighbours.pop()
"""Add selected neighbour to cluster"""
cluster.addPoint(selected_neighbour)
removePoint(selected_neighbour, unvisited_points)
selected_neighbour.setVisited(True)
"""Find all neighbours of was pop neighbour"""
for point_1 in points:
if point_1.getVisited() == False and distance(point_1, selected_neighbour) <= epsilon:
neighbours.append(point_1)
print("Epsilon: {}" . format(epsilon))
print("MinPoints: {}" . format(minPoints), end='\n\n')
print("Cluster count: {}" . format(len(clusters)))
cluster_count = 0
rates = []
"""Plot all data on a page"""
for cluster in clusters:
cluster_count += 1
print("Cluster {}th: {}" . format(cluster_count, len(cluster.points())))
rates.append(len(cluster.points()))
print("Noise count: {}" . format(len(noise_points)), end='\n\n')
rate = ((sum(rates) * 2) / (len(clusters)))
print('Rate: {}%' . format(rate))