Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SMART
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UNI-KLU
SMART
Commits
72491c09
Commit
72491c09
authored
Feb 07, 2020
by
Alexander
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clustering based on time
parent
9bca8889
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
10 deletions
+40
-10
clusterer.py
...munity-detection-microservice/app/processing/clusterer.py
+27
-4
run_clustering.py
...ub/community-detection-microservice/app/run_clustering.py
+13
-6
No files found.
src/data-hub/community-detection-microservice/app/processing/clusterer.py
View file @
72491c09
...
@@ -21,7 +21,7 @@ class Clusterer:
...
@@ -21,7 +21,7 @@ class Clusterer:
partition_info
=
labels
partition_info
=
labels
)
)
def
_draw_locations
(
self
,
locations
:
np
.
ndarray
=
None
,
centroids
:
np
.
ndarray
=
None
,
partition_info
=
None
)
->
plt
.
Figure
:
def
_draw_locations
(
self
,
locations
:
np
.
ndarray
=
None
,
centroids
:
np
.
ndarray
=
None
,
partition_info
:
List
=
None
)
->
plt
.
Figure
:
fig
=
plt
.
Figure
()
fig
=
plt
.
Figure
()
axis
=
fig
.
add_subplot
(
1
,
1
,
1
)
axis
=
fig
.
add_subplot
(
1
,
1
,
1
)
...
@@ -55,6 +55,9 @@ class Clusterer:
...
@@ -55,6 +55,9 @@ class Clusterer:
return
labels
.
tolist
()
return
labels
.
tolist
()
def
extract_location_data
(
self
,
locations
:
List
[
dict
])
->
np
.
ndarray
:
return
np
.
asarray
([(
float
(
l
[
'latitude'
]),
float
(
l
[
'longitude'
]))
for
l
in
locations
])
def
label_locations
(
self
,
locations
:
List
[
Dict
],
labels
:
List
)
->
List
:
def
label_locations
(
self
,
locations
:
List
[
Dict
],
labels
:
List
)
->
List
:
if
locations
is
None
or
labels
is
None
:
if
locations
is
None
or
labels
is
None
:
return
return
...
@@ -65,7 +68,7 @@ class Clusterer:
...
@@ -65,7 +68,7 @@ class Clusterer:
for
i
in
range
(
len
(
locations
)):
for
i
in
range
(
len
(
locations
)):
locations
[
i
][
'cluster_label'
]
=
labels
[
i
]
locations
[
i
][
'cluster_label'
]
=
labels
[
i
]
def
run
(
self
,
locations
:
List
[
Dict
])
->
Dict
[
int
,
List
[
Dict
]]:
def
cluster_locations
(
self
,
locations
:
List
[
Dict
])
->
Dict
[
int
,
List
[
Dict
]]:
'''Returns a dictionary with identified clusters and their locations copied from the input'''
'''Returns a dictionary with identified clusters and their locations copied from the input'''
if
locations
is
None
or
len
(
locations
)
==
0
:
if
locations
is
None
or
len
(
locations
)
==
0
:
# raise Exception("locations has to contain something")
# raise Exception("locations has to contain something")
...
@@ -80,5 +83,25 @@ class Clusterer:
...
@@ -80,5 +83,25 @@ class Clusterer:
return
clusters
return
clusters
def
extract_location_data
(
self
,
locations
:
List
[
dict
])
->
np
.
ndarray
:
def
cluster_times
(
self
,
times
:
List
[
Dict
])
->
Dict
[
int
,
List
[
Dict
]]:
return
np
.
asarray
([(
float
(
l
[
'latitude'
]),
float
(
l
[
'longitude'
]))
for
l
in
locations
])
\ No newline at end of file
# times.sort(key=lambda x: x['timestamp'])
times1
=
np
.
asarray
([(
float
(
t
[
'timestamp'
]),
float
(
0
))
for
t
in
times
])
# print(times)
dbsc
=
DBSCAN
(
eps
=
self
.
epsilon
,
min_samples
=
self
.
min_points
)
dbsc
=
dbsc
.
fit
(
times1
)
labels
=
dbsc
.
labels_
print
(
labels
)
self
.
label_locations
(
times
,
labels
)
times
=
[
t
for
t
in
times
if
t
[
'cluster_label'
]
!=
-
1
]
print
(
times
)
info
=
[
l
for
l
in
labels
if
l
!=
-
1
]
print
(
info
)
times1
=
np
.
asarray
([(
float
(
t
[
'timestamp'
]),
float
(
0
))
for
t
in
times
])
fig
=
self
.
_draw_locations
(
locations
=
times1
,
partition_info
=
info
)
fig
.
savefig
(
'img.png'
)
\ No newline at end of file
src/data-hub/community-detection-microservice/app/run_clustering.py
View file @
72491c09
...
@@ -12,16 +12,15 @@ from processing.clusterer import Clusterer
...
@@ -12,16 +12,15 @@ from processing.clusterer import Clusterer
DEBUG
=
False
DEBUG
=
False
# used to cluster the users based on their main location
user_clusterer
=
Clusterer
()
repo
=
Repository
()
repo
=
Repository
()
def
run_location_clustering
():
def
run_location_clustering
():
all_location_traces
=
repo
.
get_locations
()
user_clusterer
=
Clusterer
()
all_location_traces
=
repo
.
get_agi_locations
()
cluster_result
=
user_clusterer
.
run
(
cluster_result
=
user_clusterer
.
cluster_locations
(
[
l
.
to_serializable_dict
()
for
l
in
all_location_traces
])
[
l
.
to_serializable_dict
()
for
l
in
all_location_traces
])
clusters
=
[
UserCluster
(
key
,
value
)
clusters
=
[
UserCluster
(
key
,
value
)
...
@@ -30,6 +29,14 @@ def run_location_clustering():
...
@@ -30,6 +29,14 @@ def run_location_clustering():
store_user_clusters
(
clusters
)
store_user_clusters
(
clusters
)
def
run_time_clustering
():
all_location_traces
=
repo
.
get_agi_locations
()
user_clusterer
=
Clusterer
(
epsilon
=
10
**
5.8
)
user_clusterer
.
cluster_times
([
l
.
to_serializable_dict
()
for
l
in
all_location_traces
])
def
store_user_clusters
(
user_clusters
:
List
[
UserCluster
]):
def
store_user_clusters
(
user_clusters
:
List
[
UserCluster
]):
if
DEBUG
:
if
DEBUG
:
print
(
user_clusters
)
print
(
user_clusters
)
...
@@ -40,4 +47,4 @@ def store_user_clusters(user_clusters: List[UserCluster]):
...
@@ -40,4 +47,4 @@ def store_user_clusters(user_clusters: List[UserCluster]):
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
run_
location
_clustering
()
run_
time
_clustering
()
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