2
votes

I have been having trouble with the predict function underestimating (or overestimating) the predictions for new text category (or its class if they sport...health....politcs)

Firstly I import a tdm matrix of my corpus then split my data training / test to use in my model with knn algorithm.

And it works fine.

Now I need to import new text unknown category to predict it but I did not how to do that.

I don't how to use predict function

The error is

ff<-predict(knn.pred,PathFilename)

Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "factor"

# Text mining: Corpus and Document Term Matrix
library(tm)
# KNN model
library(class) 
# Stemming words
library(SnowballC)
# CrossTable
library('gmodels')
# function prediction
library(caret) 
# function for factor
library(e1071)
library(SparseM)

# Stemming words

# Read csv with columns: Document , Terms and category
PathFile <- read.csv(file.choose(), sep =";", header = TRUE)
PathFilename<-read.csv(file.choose(), sep =";", header = TRUE)
#Strectur of Csv file
str(PathFile)
tail(PathFile)

# Column bind category (known classification)
#mat.df <- cbind(PathFile, PathFile$Category)
#tail(mat.df)
# Change name of new column to "category"
#colnames(mat.df)[ncol(mat.df)] <- "Category"

# Split data by rownumber into two equal portions
train <- sample(nrow(PathFile), ceiling(nrow(PathFile) * .70))
test <- (1:nrow(PathFile))[- train]

##Show Training Data
train
##Show Test Data
test
#n <- names(PathFile)
#f <- as.formula(paste("Category ~", paste(n[!n %in% "Category"], collapse = " + ")))
#f


# Isolate classifier
cl <- PathFile[, "Category"]

# Create model data and remove "category"
modeldata <- PathFile[,!colnames(PathFile) %in% "Category"]

# Create model: training set, test set, training set classifier
knn.pred <- knn(modeldata[train, ], modeldata[test, ], cl[train], 70)
knn.pred

# Confusion matrix
conf.mat <- table("Predictions" = knn.pred, Actual = cl[test])
conf.mat


ff<-predict(knn.pred,PathFilename) #here i have an error 

ct<-CrossTable(x = cl[test], y = knn.pred, prop.chisq=FALSE)

table(knn.pred,cl[test])

plot(knn.pred,
     xlab = "Number of neighbours(k)",
     main = "Comparison of Accuracy against k",
     type = "b",
     col = "black",
     lwd = 1.8,
     pch = "O")



studentModel <- train(Category ~ ., data=PathFile, method = "knn")
studentTestPred <- predict(model, test) 

# Accuracy
(accuracy <- sum(diag(conf.mat))/length(test) * 100)


# Create data frame with test data and predicted category
setwd("C:/Users/Public/Desktop/")
df.pred <- cbind(knn.pred, modeldata[test, ])
write.table(df.pred, file="output.csv", sep=";")
1

1 Answers

1
votes

knn works differently than many other classifiers. It is a "lazy" classifier. There is no model. It computes everything when you need to make a prediction. Because of that, there is no predict method for knn. The knn function returns its predictions directly. Since you do not provide your data, I cannot use your example, but I will illustrate with built in data using code that is a simplified version of your code.

library(class)
train <- sample(nrow(iris), ceiling(nrow(iris) * .70))
test <- (1:nrow(iris))[- train]
knn.pred <- knn(iris[train, 1:4], iris[test,1:4 ], iris$Species[train], 5)
knn.pred
 [1] setosa     setosa     setosa     setosa     setosa     setosa     setosa    
 [8] setosa     setosa     setosa     setosa     setosa     setosa     setosa    
[15] setosa     setosa     setosa     setosa     setosa     setosa     versicolor
[22] versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[29] versicolor versicolor versicolor virginica  versicolor versicolor versicolor
[36] versicolor virginica  virginica  virginica  virginica  virginica  virginica 
[43] virginica  virginica  virginica 
Levels: setosa versicolor virginica 

As you can see, knn.pred is not a model. It is the predictions for the test data.

In order to predict your new test data, you need to run knn again.

 knn(modeldata[train, ], PathFilename, cl[train], 70)

This should produce the predictions that you want.