-
Notifications
You must be signed in to change notification settings - Fork 5
/
L130_Dbscan_Template.Rmd
87 lines (62 loc) · 1.58 KB
/
L130_Dbscan_Template.Rmd
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
---
title: "Dbscan"
author: "Bert Gollnick"
output:
html_document:
toc: true
toc_float: true
number_sections: true
code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = F)
```
# Data Preparation
We will create a dataset, that consists of a circle in the center and a ring that surrounds the inner circle.
```{r}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(dbscan))
```
```{r}
set.seed(123)
n_points <- 4000
df <- tibble(x = runif(n = n_points, min = -10, max = 10),
y = runif(n = n_points, min = -10, max = 10),
z = x^2+y^2) %>%
mutate(class = ifelse(z < 10, "A", "B")) %>%
mutate(class = as.factor(class)) %>%
filter((z <10) | (z > 80 & z < 100))
```
Let's visualise the data.
```{r}
g <- ggplot(df, aes(x, y, col = class))
g <- g + geom_point()
g
```
# Modeling: dbscan
We start the modeling.
At first we transform the dataframe to a matrix.
```{r}
# code comes here
```
We need to get an estimate of eps parameter.
```{r}
# code comes here
```
For epsilon you choose the maximum value which has a low slope.
It still requires some manual adaptation to find the optimum set of parameters.
```{r}
# code comes here
g <- ggplot(df, aes(x, y, col = factor(cluster)))
g <- g + geom_point()
g
```
# Modeling: kmeans
Just for comparison we perform kmeans to see how this performs on this dataset.
```{r}
# code comes here
g <- ggplot(df, aes(x, y, col = factor(cluster_kmeans)))
g <- g + geom_point()
g
```