R语言中使用SVM预测新数据返回named numeric(0)问题求助
named numeric(0) Hey Laura, sorry to hear your grad project code threw a curveball out of nowhere—nothing’s more frustrating than code working one day and breaking the next! Let’s dig into why your SVM model’s predict call is returning a named numeric(0) and get you back on track.
Most Likely Culprits & Fixes
Let’s start with the most common issues that cause this empty prediction result:
Mismatched column names between training and test data
SVM models rely on exact feature column name matches to map test data to the trained model. If youra_sca_testhas different column names (even a tiny typo or case difference) than the feature columns ina_sca_train(excluding thetrain_resulttarget column),predict()won’t recognize the features and returns nothing.To check this quickly:
# Compare feature columns (exclude the target column from training data) train_feature_cols <- setdiff(colnames(a_sca_train), "train_result") test_feature_cols <- colnames(a_sca_test) # Print the comparison cat("Training features:", paste(train_feature_cols, collapse = ", "), "\n") cat("Test features:", paste(test_feature_cols, collapse = ", "), "\n")If they don’t match exactly, rename your test set columns to match the training set’s feature names.
Incorrect number of features in test data
Your test set must have the same number of feature columns as the training set (minus thetrain_resulttarget column). For example, if your training data has 10 features + 1 target column, your test data needs exactly 10 feature columns.Verify this with:
cat("Training data dimensions:", dim(a_sca_train), "\n") cat("Test data dimensions:", dim(a_sca_test), "\n")If the column counts don’t align, double-check how you’re preparing
a_sca_test—did you accidentally drop or add a column since yesterday?Incomplete
predict()call
Your code cuts off atpredict_s..., so make sure you’re properly specifying thenewdataparameter. The full call should look like this:predictions <- predict(svm_st, newdata = put_test)Omitting
newdatamight lead to unexpected behavior, though it usually defaults to the training data—so this is less likely, but worth confirming.Hidden issues in training data
Even if training seemed normal, double-check thata_sca_trainactually contains thetrain_resultcolumn (your target variable for regression). Runhead(a_sca_train)to confirm the column exists and is numeric (since you’re usingeps-regression).
Quick Test Code
Here’s a streamlined snippet to validate and fix the issue:
# Step 1: Validate column matches train_feature_cols <- setdiff(colnames(a_sca_train), "train_result") if (!all(train_feature_cols == colnames(a_sca_test))) { stop("Test set columns don't match training set features! Fix column names first.") } # Step 2: Re-train (just to be safe) svm_st <- svm(train_result ~ ., data = a_sca_train, type = "eps-regression", kernel = "radial", scale = TRUE) # Step 3: Predict correctly put_test <- a_sca_test # No need to re-wrap in data.frame if it's already one predictions <- predict(svm_st, newdata = put_test) # Check the result str(predictions)
9 times out of 10, this is a column name or feature count mismatch—start there, and you’ll probably have it fixed in no time.
内容的提问来源于stack exchange,提问作者Laura

