You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

R语言中使用svm函数预测时混淆矩阵结果异常求助

Hey there! Let's figure out why that confusion matrix is showing weird values in the second column when you run your SVM classification in R. Let's break down the possible causes and fixes step by step.

Possible Causes & Fixes
  • Your target variable Find1 isn't a factor type
    SVM in R's e1071 package (which I assume you're using) treats the task as regression if the target variable is numeric/integer instead of a factor. Even if you specify type="class" in predict(), this might not override the model's original regression behavior, leading to predicted continuous values. When you cross-tab these with your test set's Find1 (if it's also numeric), you'll see a messy table with random numerical values.
    Fix: Check and convert Find1 to a factor in both train and test sets:

    # Check type
    class(train$Find1)
    class(test$Find1)
    
    # Convert to factor if needed
    train$Find1 <- factor(train$Find1)
    test$Find1 <- factor(test$Find1)
    

    Then retrain your SVM model and re-run the prediction.

  • Test set has Find1 values not present in the training set
    If your test set's Find1 includes categories that never appeared in the training data, the confusion matrix will add columns for these unseen values. For example, if training Find1 only has levels "A" and "B", but test has "C", you'll see a "C" column that looks out of place.
    Fix: Compare the unique values in both sets:

    unique(train$Find1)
    unique(test$Find1)
    

    If there are mismatches, you can either remove the test rows with unseen values or adjust your training data to include those categories.

  • You're referencing the wrong column in test[,1]
    It's possible that Fulltable4's first column isn't actually Find1 (maybe columns were reordered accidentally). Using test[,1] pulls the first column, which might be a numeric feature instead of your target variable—hence the weird values in the confusion matrix.
    Fix: Use explicit column names instead of positional indexing to avoid this:

    table(p2, test$Find1)
    

    This ensures you're comparing predictions against the correct target variable.

  • The predict() parameter might be ignored
    If your initial SVM model was trained on a numeric target, specifying type="class" won't force it to output class labels. The model is still a regression model, so predictions will be continuous numbers. Converting Find1 to a factor before training is the only way to make it a classification task.

Once you've checked these points, re-run your code and the confusion matrix should make sense with your class labels instead of random numerical values.

内容的提问来源于stack exchange,提问作者henry dupuis

火山引擎 最新活动