2017-03-22 1 views
0

저는 마지막 클래스 이름을 기반으로 한 데이터 프레임을 svm에 몇 시간이나 몇 시간 동안 노력하고 있습니다.SVM 마지막 열을 기반으로하는 데이터 프레임

나는이 데이터 프레임을 가지고

#FIll the data frame 
df = read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", 
       sep=",", 
       col.names=c("buying", "maint", "doors", "persons", "lug_boot", "safety", ""), 
       fill=TRUE, 
       strip.white=TRUE) 

lastColName <- colnames(df)[ncol(df)] 

... 내가 NULL 또는 Error in model.frame.default(formula = str(lastColName) ~ ., data = df1, : invalid type (NULL) for variable 'str(lastColName)' 중 하나를 얻고있다

model <- svm(lastColName~., 
      data = df, 
      kernel="polynomial", 
      degree = degree, 
      type = "C-classification", 
      cost = cost) 

. 열이 이름이 없을 때 NULL이 도착하는 것을 이해합니다. 나는 마지막 열 이름이기 때문에 다른 오류를 이해하지 못합니다 ..

어떤 생각?

답변

1

수식에 동적 변수를 사용하려는 경우 as.formula을 사용해야합니다. 자세한 내용은 ?as.formula

를 참조하십시오 다음 코드는 잘 작동 :

library(e1071) 
df_1 = read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", 
       sep=",", 
       col.names=c("buying", "maint", "doors", "persons", "lug_boot", "safety", ""), 
       fill=TRUE, 
       strip.white=TRUE) 

lastColName <- colnames(df_1)[ncol(df_1)] 

model <- svm(as.formula(paste(lastColName, "~ .", sep = " ")), 
      data = df_1, 
      kernel="polynomial", 
      degree = 3, 
      type = "C-classification", 
      cost = 1) 
# to predict on the data remove the last column 
prediction <- predict(model, df_1[,-ncol(df_1)]) 

# The output 
table(prediction) 

# The output is: 

prediction 
acc good unacc vgood 
0  0 1728  0 

# Since this is a highly unbalanced classification the model is not doing a very good job 
+0

와우, 그것은 작동합니다 감사합니다! 그러나 예측은 인수가 같은 길이가 아니라는 말로 실패합니다. 나는 당신의 방법 =>'table (predict (model), as.formula (paste (lastColName, "~.", sep = "")), dnn = c ("예측", "실제")) – Emixam23

+0

모델에서 예측할 답변을 업데이트했습니다. – discipulus

관련 문제