SVM classification using LIBSVM, Accuracy Problem

4 次查看(过去 30 天)
I have datasets belongs to two different classes as given in attached xls file. I converted the data into "train data" set and "test data" set in CSV format
I have still doubt on the way i am giving input to the program.
If i run my attached matlab code, it is always showing 50% accuracy only...
Please let me know whether i am giving input data correctly or not, and how to get good accuracy?
here the test data is a part of train data only...

回答(1 个)

Aditya
Aditya 2025-7-22
Hi HST,
When working with neural networks for classification in MATLAB, it’s crucial to properly format your input data and target labels. For a two-class classification problem, your feature matrix should have samples as columns and features as rows. The target labels should be converted to a one-hot encoded format, where each class is represented by a vector (for example, class 1 as [1; 0] and class 2 as [0; 1]). If you use raw class labels (like 1 and 2) instead of one-hot vectors, the network will not be able to learn the classification task correctly, which often leads to an accuracy of around 50%—equivalent to random guessing.
Below is an example of how you should prepare your data and train a simple neural network in MATLAB:
% Load your CSV data
trainData = readmatrix('train.csv');
testData = readmatrix('test.csv');
% Separate features and labels
X_train = trainData(:, 1:end-1)'; % Features as rows, samples as columns
y_train = trainData(:, end); % Class labels (1 or 2)
X_test = testData(:, 1:end-1)';
y_test = testData(:, end);
% Convert labels to one-hot encoding
T_train = full(ind2vec(y_train')); % Now T_train is 2 x N
T_test = full(ind2vec(y_test'));
% Create and train the neural network
net = patternnet(10);
net = train(net, X_train, T_train);
% Predict on test data
Y_pred = net(X_test);
% Convert network output to class labels
[~, predicted_labels] = max(Y_pred, [], 1);
% Calculate accuracy
accuracy = sum(predicted_labels' == y_test) / numel(y_test);
fprintf('Test Accuracy: %.2f%%\n', accuracy*100);

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by