karx
karx

Reputation: 577

KNN - using R, want to get proportion of votes for all classes

Fairly new to text classification.

There are 11-12 classes that a document can belong to. I want to see all thee probability/measure for all possible classes to which the document belongs to.

The data i have can have noise. There are classes like. 'Dell' and 'Dell notebooks'

I am using the k-Nearest Neighbour Classification using R.

Bag of words to featurise the document.

Edit: What i am looking for is what 'predict' gives us with type

library(class)
library(e1071) 
data(iris)

train.idx <- sample(nrow(iris),ceiling(nrow(iris)*0.7))
test.idx <-(1:nrow(iris)) [- train.idx]

data.var <- iris[,1:4]
data.class<-iris[,5]

classifier<-naiveBayes(data.var[train.idx,], data.class[train.idx]) 
predict(classifier, data.var[test.idx,],type="raw")

This will give a table that shows the probability of each class possible. I want to generate a similar table.

Upvotes: 1

Views: 1047

Answers (1)

LyzandeR
LyzandeR

Reputation: 37889

I will use the iris3 data because I find it easier to work with. It is exactly the same data set as iris:

You need the KODAMA package and the function knn.probability to get what you want. See the following example:

data(iris3)
#every 25 rows belong to a specific type of flower
train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
#50-50 split on this ocassion
test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])

#first 25 rows are setosa, next 25 versicolor, and the last 25 virginica
cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))


#In order to get probabilities for all 3 classes you need the following library
library(KODAMA)

#rbind the train and test sets
x <- rbind(train,test)
#calculate the distances among rows (necessary step)
kdist <- knn.dist(x)
#calculate and 
# view probabilities (all class probabilities are returned)
#you just pass in the indices as you see for the training and test sets 
#first 75 rows is the train set, second 75 rows is the test set 
probs <- knn.probability(1:75, 76:150, cl, kdist, k=3)

#I prefer the transposed result more to be honest
head(t(probs),10)

And this is the output for each row in the test set:

> head(t(probs),30)
            c s         v
76  0.0000000 1 0.0000000
77  0.0000000 1 0.0000000
78  0.0000000 1 0.0000000
79  0.0000000 1 0.0000000
80  0.0000000 1 0.0000000
81  0.0000000 1 0.0000000
82  0.0000000 1 0.0000000
83  0.0000000 1 0.0000000
84  0.0000000 1 0.0000000
85  0.0000000 1 0.0000000
86  0.0000000 1 0.0000000
87  0.0000000 1 0.0000000
88  0.0000000 1 0.0000000
89  0.0000000 1 0.0000000
90  0.0000000 1 0.0000000
91  0.0000000 1 0.0000000
92  0.0000000 1 0.0000000
93  0.0000000 1 0.0000000
94  0.0000000 1 0.0000000
95  0.0000000 1 0.0000000
96  0.0000000 1 0.0000000
97  0.0000000 1 0.0000000
98  0.0000000 1 0.0000000
99  0.0000000 1 0.0000000
100 0.0000000 1 0.0000000
101 1.0000000 0 0.0000000
102 1.0000000 0 0.0000000
103 0.3333333 0 0.6666667
104 1.0000000 0 0.0000000
105 1.0000000 0 0.0000000

Upvotes: 2

Related Questions