1
votes

I am working on binary classification of imbalanced dataset.The dataset contains 777 minority classes and 2223 majority classes.I have built a one class SVM model with only minority labelled records.BUt when I am trying to predict on the built model,I am getting predicted values as all -1 and hence accuracy as 0.I have scaled my features. Here's my implementation

ml_file_df = pd.read_csv('/data/jayashree/3000_ML_features.csv')
minority_df = ml_file_df[ml_file_df['RESULT'] == 0]
array = minority_df.values
features = array[:, 0:60630]
labels = array[:, 60630]
scaler = MinMaxScaler()
scaled_features = scaler.fit_transform(features)
features_train, features_test, labels_train, labels_test = train_test_split(
    scaled_features, labels, test_size=0.3, random_state=10)
gamma_values = [0.001, 0.005, 0.01, 0.05, 0.1, 0.5]
nu_values = [0.1, 0.3, 0.5, 0.7]
for j in nu_values:
    for i in gamma_values:
        clf = svm.OneClassSVM(nu=j, kernel='rbf', gamma=i)
        clf.fit(features_train, labels_train)
        pred = clf.predict(features_test)
        print(i, classification_report(labels_test, pred))

For all the cases I am getting predictions like this

[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1  1 -1 -1 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 -1 -1 -1  1 -1 -1 -1 -1  1]

Where am I going wrong?

I have resolved the error.One class svm will predict 1 or -1.I used minority class as 1 and majority class as -1.This solved my issue.

1
That's not reproducible for us as you don't share your data (and the code is at least missing imports). But the obvious first step is: use a linear-kernel to make things much more simple. After this, you might get some insight if your params or your data is the problem. - sascha
I feel there is an error in splitting the dataset..why don’t you use sklearn’s test_train_split ?? Your label variable seems to be not one dimentional. - Mayukh Sarkar

1 Answers

0
votes

That's because you have 0 and 1 labels and One-class SVM show -1 and 1, it's enough you exchange -1 to 0.