Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One class SVM, got all -1

I am doing a binary classification that only returns "yes" or "no" for the image. As I only got an image of one class, so I wanna classify between "Target" and " Outlier".

For example, I am classifying the fireman. enter image description here

I am using Scikit Learn svm.OneClassSVM(). However, after training the model, I got "-1" every time, even for predicting the training data.

Here is my code:

X_train = []
for subdir, dirs, files in os.walk("training"):
    for imagePath in files:
        print ("path = ", imagePath)
        img = Image.open(os.path.join(subdir, imagePath))
        img = img.resize(sample_size, PIL.Image.ANTIALIAS)
        img = np.array(img)
        img = img[:,:,0]
        img = img.reshape(1, img.shape[0]* img.shape[1])
        X_train.append(img[0])

clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(X_train)

And then I predict the result of "training data"

print clf.predict (X_train)

However, I still get all "-1". Can anyone tell me what's wrong?

like image 442
VICTOR Avatar asked Oct 31 '22 03:10

VICTOR


1 Answers

you can set the threshold level by calculating the score sample. For example

clf = OneClassSVM()        
y_scores = clf.score_samples(test)

You need to check by yourself which threshold is better. You can check that by running the model on some data of abnormal class and check below which threshold are you getting your answer. The lesser the score sample the more outlier is that.

like image 177
umair Avatar answered Nov 09 '22 22:11

umair