2017-03-22 7 views
0

私は、最後のクラス名に基づいてデータフレームをsvmに変換するのに数時間から数時間をかけています。SVM最後の列に基づくデータフレーム

私はNULLまたはError in model.frame.default(formula = str(lastColName) ~ ., data = df1, : invalid type (NULL) for variable 'str(lastColName)'のいずれかを取得しています

#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)] 

...

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( "Prediction"、 "Actual")) – Emixam23

+0

モデルから予測する答えを更新しました。 – discipulus

関連する問題