ValueError: Classification metrics can't handle a mix of unknown and multiclass targets - classification

I'm getting this error :ValueError: Classification metrics can't handle a mix of unknown and multiclass targets
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
pred = knn.predict(x_test)
print(accuracy_score(y_test,pred))
print(classification_report((y_test,pred)))
print(confusion_matrix(y_test,pred)
i'm not figure out the error in it, why it's coming?

I don't know what your data looks like but maybe that helps: I had a similar problem, as I had multiclass data that scikit's classification_report wouldn't accept. What did the trick for me, was to flatten the variables y_test and pred so that both were just simple lists like so: [0,1,1,2,0 ...].

Related

Invalid labels for classification logistic regression model in pyspark databricks

I am using Spark ML library for classification problem using a logistic regression.
I have vectorized input features and created training dataset and test dataset.
While fitting the model I get invalid labels issue.
the training dataset is :
where my input features as Independent_features and my target feature as Category_con.
Use the words : label, features instead of independent_features and Category_con while creating your vectors.
For the labels, you would need to change them into just 3 categories. It looks like you might have 6 from the error message. You would need to use conditional replacement to group or bin the categories like below:
train_df.withColumn('label', when((col('Category_con') == firstCondition) ).otherwise(when((col('Category_con') == secondCondition) ).otherwise(lastCondition))

"No valid system or dataset was specified" while using predict() in MATLAB

I am trying to classify my input features into two classes using SVM. I want to use 10-fold cross-validation to train an SVM classifier. I am using MATLAB inbuilt functions. But while using predict() function along with crossval() function, I am getting an error:
No valid system or dataset was specified.
Does anyone knows how to resolve this issue?
Training_Features = X;
Training_Labels = Y;
SVMModel =
fitcsvm(Training_Features,Training_Labels,'KernelFunction','RBF');
CVSVMModel = crossval(SVMModel);
[Predict_Labels,Predict_Scores] =
predict(CVSVMModel,Training_Features);
I think you understood the cross validation functionality wrong. Your CVSVMModel is a so called ClassificationPartitionedModel which has no function predict() since Cross Validation is meant for testing the generalisation of your model BEFORE you train it with the WHOLE dataset (not cross-validated).
I suggest the following:
Call [Predict_Labels,Predict_Scores] = kfoldPredict(CVSVMModel); to see how well it does on each validation dataset during the cross-validation
If you're satisfied train a new SVMModel with the whole dataset and predict with that one.
EDIT:
A ClassificationPartitionedModel is a collection of models (in your case 10 different ones). You can access and even call predict() on them by for example:
[Predict_Labels,Predict_Scores] = predict(CVSVMModel.Trained{1, 1},X);

support vector machine regression & prediction using MATLAB fitrsvm function

I'm relatively new to using SVM and I have a question regarding how to use the results of the SVM regression. I have found many easy-to-understand documentation on SVM classification, and I can understand how to use the result of SVM for binary classification (i.e. data on one side of the support vector is labeled as one, data on the other side of the support vector is labeled as another), but I have no been able to find such hints on SVM regression- which is why I have run into the following question:
Using both libsvm package and the fitrsvm function in MATLAB, I was able to successfully generate models that are capable of fitting the abalone data set. the result of the libsvm (using svmtrain function) was used along with svmpredict to the successfully predict with new input parameters as followed:
model=svmtrain(age_train,X_train,['-s 3 -t 2 -c 2 -g 2']);
[prediction,accuracy,~]=svmpredict(age_eval,X_eval,model);
Also, as I've said, I was able to achieve the same results using the fitrsvm function as followed:
model1=fitrsvm(X_train,age_train,'OptimizeHyperparameters','auto',...
'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',...
'expected-improvement-plus'),'KernelFunction','rbf');
age_predict1=predict(model1,X_eval);
Now my question is, how do the svmpredict function (in the case of libsvm package) and the predict function (in the case of fitrsvm function in MATLAB) take the values within the trained models and apply them to the new input data? For example, is there a mathematical equation in which I apply the parameters of the trained model (such as the 'Mu' and the 'Sigma' parameters in the fitrsvm result) to the new input data to obtain the results?
It would be greatly appreciated if someone could help me with this or refer me to someone/somewhere who can help me, thank you very much in advance.

Multi-Class SVM. Binary Decision Tree. Issues with LIBSVM

So I'm trying to implement a multi-class SVM.
Matlab didn't like having more than two classes to classify data into, so I'm using a Binary Decision Tree to classify data.
I have three classes and am splitting the data into two and one, I'll then classify the first results using a SVM and then classify the results of this to the one unclassified class.
However, when using LIBSVM, I'm getting an error when using svmpredict:
td= a{1,1};
tc = b{1,1};
td1 = a{1,2}; %data to test svm
testdatatest = td1(1:30,1:4); %data to test svm
data = td(1:80, 1:4); %split data
target = tc(1:80); %split data
model = svmtrain(data, target); %train
[predicted_label, accuracy, decision_values]=svmpredict(testdatatest,target, model);
The error I get is:
Undefined function 'svmpredict' for input arguments of type 'struct'.
Any suggestions would be great, thanks.
you must download and make libsvm, open the zip file and select your langauges like Matlab etc. then make it! it would give you two files, now you are using Matlab SVM not libsvm.
good luck

Unusual result of SVM classification in Matlab

I am trying to use SVM to do a binary classification from a very high dimensional dataset which is a 3249X40 matrix. I have five similar datasets. While I get results from Decision tree properly(low but different), I get exactly same result from SVM everytime whatever dataset I may use. I use svm in following way:
svmModel = svmtrain(train_mat(trainIdx,:), groups(trainIdx), ...
'Autoscale',true, 'Showplot',false, 'Method','QP', ...
'BoxConstraint',2e-1, 'Kernel_Function','rbf', 'RBF_Sigma',1);
pred = svmclassify(svmModel, train_mat(testIdx,:), 'Showplot',false);
What's wrong with it? I am using Decision tree like this:
tree=ClassificationTree.fit(train_mat(trainIdx,:),groups(trainIdx,:));
pred=tree.predict(train_mat(testIdx,:));
I am getting different results(which appear correct as well) from those 5 datasets in decision tree. What's wrong? Is it because SVM cannot handle such datasets that have very few observations compared to number of variables?
You will probably need to find a combination of C (what you call box constraint) and sigma for the RBF kernel that works well. This is typically done through cross validation. That is separate your trainig data in two, for each combination of box constraint and sigma train on one half and test on the other half, and train on the second half and test on the first half, average both accuracies. Use for testing the box constraint and sigma combination that got the best accuracy.
This question covers many things to check Supprt Vector Machine works in matlab, doesn't work in c++