2
$\begingroup$

I'm provided with the following dataset: Dataset. I'm meant to use sklearn to create a Support Vector Machine that can predict it.

I load A and B from my dataset into a 2 dimensional array called input_data and load the label from my dataset into an array called label.

First of all I'm scaling A and B to fit in the range of -1 to 1 using sklearn.preprocessing.MinMaxScaler.

input_data = np.array(input_data)
minmax = pre.MinMaxScaler(feature_range=(-1,1))
input_data = minmax.fit(input_data).transform(input_data)
label = np.array(label)

Then I use sklearn.model_selection.train_test_split to stratify and split my data keeping 40% for my test set and 60% for my training set.

model_selector = model_selection
X_train, X_test, y_train, y_test = model_selector.train_test_split(input_data, label, stratify=label, test_size=0.4)

After that I use sklearn.model_selection.GridSearchCV to try and come up with the optimal parameters (in my case only C).

gscv = model_selector.GridSearchCV
clf = gscv(svm.SVC(), tuned_parameters, cv=5, scoring='precision')
clf.fit(X_train, y_train)

After that I evaluate my results, but my Support Vector Machine just outputs 1 no matter what the input is, I'm not sure what exactly I'm doing wrong so after trying to figure things out by reading the documentation for a few hours I decided to ask for help, can anyone point out to me what I'm doing wrong? I'm pretty new in this area so my mistake is probably something simple I missed. Thanks in advance for any advice!

Here's an image of the dataset using a scatter plot: enter image description here

$\endgroup$
1
  • $\begingroup$ Not sure about this @patrickdamery but you might want to try changing your scoring metric to ´accuracy´ from ´precision´ in your ´GridSearchCV´ function. Maybe that helps. $\endgroup$ Mar 29, 2017 at 9:30

1 Answer 1

3
$\begingroup$

What is tuned_parameters?

If I train SVC with default parameters on your dataset, it works fine with 61% accuracy, predicting both classes.

model.predict(X_test) with the model trained with your parameters outputs both 0 and 1 for me, with 98% accuracy:

model = svm.SVC(kernel = 'rbf', C=10, gamma=10)
model.fit(X_train, y_train)
print(model.predict(X_test))
print(model.score(X_test, y_test))

So the question is, how do you check what the model outputs on your test set? You may have a mistake there.

$\endgroup$
5
  • $\begingroup$ The labels where already in the range of 0, 1 so I didn't think I needed to scale it with minmax, am I wrong? You can see the dataset in the link I provided. I've updated the question with a scatter plot of the dataset. If I set SVC to use the parameters: {'kernel': 'rbf', 'C': 10, 'gamma': 10} I get 95% accuracy on the training set and 92% on the test set, however I also need to present the svc using the linear kernel. Should the linear kernel not output 0 at least sometimes when using the linear kernel? $\endgroup$ Mar 29, 2017 at 8:04
  • $\begingroup$ See my edited answer. $\endgroup$
    – rinspy
    Mar 29, 2017 at 8:17
  • $\begingroup$ tuned_parameters is an array with an object that contains a variety of parameter information like this: tuned_parameters = [{'kernel': ['rbf'], 'gamma': [0.1, 0.5, 1, 3, 6, 10], 'C': [0.1, 0.5, 1, 3, 6, 10]},{'kernel': ['linear'], 'C': [0.1, 0.5, 1, 5, 10, 50, 100]}]. I know the rbf kernel works, but the linear one just returns 0 in my case, is this normal? I would have expected at least a few 0's even if it wasn't that accurate am I wrong? $\endgroup$ Mar 30, 2017 at 2:47
  • 1
    $\begingroup$ With a linear kernel, you are trying to linearly separate your data (which is not linearly separable). Since you have more 1s in your sample than 0s, I suspect the best linear model you can do is just predict 1 all the time. $\endgroup$
    – rinspy
    Mar 30, 2017 at 7:24
  • $\begingroup$ You are indeed correct, it turned out predicting only 1s was the best possible linear model! $\endgroup$ Mar 31, 2017 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.