Commit 3124f5d3 authored by Weigert, Andreas's avatar Weigert, Andreas
Browse files

added solution for classification

parent 4ade03c6
......@@ -157,4 +157,62 @@ selected.features <- cfs(formula=as.simple.formula(class="pNumResidents", attrib
#further feature filter
selected.features2 <- consistency(formula=as.simple.formula(class="pNumResidents",
attributes = all.features), data = alldata)
```
\ No newline at end of file
```
```{r Classification Basic evaluation approach}
set.seed(1506)
test.cases <- match(
sample(alldata$VID, size = .3*nrow(alldata)),
alldata$VID)
## decisoon tree
#train the model
model <- ctree(pNumResidents ~ . ,
data=alldata[-test.cases, c("pNumResidents", selected.features)])
#predict test cases
clres <- predict(model, newdata=alldata[test.cases,c("pNumResidents", selected.features)])
#create confusion matrix and calculate accuracy
cm <- table(clres, alldata$pNumResidents[test.cases])
(accuracy <- (sum(diag(cm))/sum(as.vector(cm))))
## random forest
#train the model
model <- randomForest(pNumResidents ~ . ,
data=alldata[-test.cases, c("pNumResidents", selected.features)])
#predict test cases
clres <- predict(model, newdata=alldata[test.cases,c("pNumResidents", selected.features)])
#create confusion matrix and calculate accuracy
cm <- table(clres, alldata$pNumResidents[test.cases])
(accuracy <- (sum(diag(cm))/sum(as.vector(cm))))
## kNN
testdata <- na.omit(alldata[test.cases, c("pNumResidents", selected.features)])
traindata <- na.omit(alldata[-test.cases, c("pNumResidents", selected.features)])
# predict test cases from training data (lazy learning algoritm has no explicit training step!)
clres <- knn(train = traindata[,-1], test = testdata[,-1],
traindata$pNumResidents, k = 5)
#create confusion matrix and calculate accuracy
cm <- table(clres, testdata$pNumResidents)
(accuracy <- (sum(diag(cm))/sum(as.vector(cm))))
## SVM
#train the model
model <- svm(pNumResidents ~ . , data=traindata)
#predict the test cases
clres <- predict(model, newdata=testdata)
#create confusion matrix and calculate accuracy
cm <- table(clres, testdata$pNumResidents)
(accuracy <- (sum(diag(cm))/sum(as.vector(cm))))
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment