I am trying to create a weighted svm model using e1071 package in R. I have successfully created the model with good accuracy. The problem is that when I am changing the class.weghts the result is still the same that does not make sense to me.
I am using following script:
data01<- read_excel("C:\Users\after_PCA_for_SVM_PASS_FAIL_TRUE.xlsx")
data_frame<-data.frame(data01)
n <- nrow(data_frame) # Number of observations
n
ntrain <- round(n*0.75) # 75% for training set
tindex <- sample(n, ntrain) # Create a random index
train <- data_frame[tindex,] # Create training set
test <- data_frame[-tindex,]
tr_size= round(nrow(data_frame)* 0.60) # the size of training dataset (60%)
vsize= nrow(data_frame)-tr_size # the size of validating and test data set
tr_sample= sample(nrow(data_frame), tr_size,replace = FALSE, set.seed(1)) # randomly select training dataset
train_data= data_frame[tr_sample, ]
#Again split the data into validating dataset and test dataset
new_data= data_frame[-tr_sample, ]
val_sample=sample(nrow(new_data),ceiling(vsize*0.1), replace=FALSE,set.seed(1) )
val_data=new_data[val_sample,]
test_data= new_data[-val_sample,]
# check the samples generated for each dataset
nrow(test_data) # number of observation in test dataset
nrow(val_data) # number of observation in validating dataset
nrow(train_data) # number of observation in training dataset
nrow(data_frame) # number observation of the whole dataset
X <- subset(data_frame, select=-STATUS)
Y <- subset(data_frame, select=STATUS)
X_train <- subset(train_data, select=-STATUS)
Y_train <- subset(train_data, select=STATUS)
install.packages("e1071")
library(e1071)
wts<-1/table(Y)
wts1<- c("0"=50, "1"=1500)
best_model= svm(train_data, Y_train, type="C-classification", kernel="linear", class.weights = wts)
best_model= svm(train_data, Y_train, type="C-classification", kernel="linear", class.weights = wts1)
linear_w1<- predict(best_model, val_data)
linear_weighted<- table(val_data$STATUS, linear_w1)
linear_weighted
Could anybody help me with this issue? I am using attached dataset.
data01
Thank you :)
question from:
https://stackoverflow.com/questions/66052455/r-class-weight-changing-in-svm-model-using-e1071-package-does-not-influence-the 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…