-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn.R
More file actions
57 lines (46 loc) · 1.22 KB
/
knn.R
File metadata and controls
57 lines (46 loc) · 1.22 KB
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
# criando dataset generico
n = 100
df <- data.frame(
x = c(rnorm(n/2,1,1),rnorm(n/2,10,1)),
y = c(rnorm(n/2,1,1),rnorm(n/2,10,1)),
z = c(rnorm(n/2,1,1),rnorm(n/2,10,1)),
w = c(rnorm(n/2,1,1),rnorm(n/2,10,1)),
#label = rbinom(n = n, size = 1, prob = 0.5)
label = c(rep(0,n/2),rep(1,n/2))
)
# knn na unha -----------------------------------------------------------------
#definindo o numero de vizinhos
k = 9
#definindo matriz de distancias
distancia <- matrix(
nrow = dim(df)[1],
ncol = dim(df)[1]
)
#calculando as distancia
for(i in 1:nrow(df)){
for(j in 1:nrow(df)){
distancia[i,j] <- sqrt(sum((df[i,] - df[j,])^2))
}
}
#espiando as distancias
distancia
#selecionando os vizinhos mais proximos
vizinhos <-NULL
for(i in 1:nrow(df)){
d_i <- data.frame(
id = 1:nrow(df),
d = distancia[i,]
)
d_i = d_i[-i,]
vizinhos[[i]] <- d_i[order(d_i$d),][1:k,'id']
}
#espiando os id dos k-vizinhos
vizinhos
#predicao
for (i in 1:nrow(df)) {
tab_label <- table(df[vizinhos[[i]],'label'])
df[i,'predito'] <- names(tab_label[tab_label==max(tab_label)])
}
#Validação
print(table(df[,'label'],df[,'predito']))
print(prop.table(table(df[,'label'],df[,'predito']),))