Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn (SVC estimator) always give the same value for predictions

I am doing a task on supervised learning. I have two set of data -training and test.

My training data-set is about 2000 records. My test data has 10 records.
When I run the following code, the predict function gives the same value as output.

I am not sure what I am doing wrong... I tried changing the value of gamma and C. Still no luck..

I am wondering if this:

  1. has to do with the data (training set size) or
  2. am I just using the wrong estimator or
  3. my code is messed-up?

Here is the complete python code:

    import pandas as pd

    training_data = pd.read_csv("Train_wo_Header.csv") #I read my training data set
    data = training_data.ix[:,[0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]] #picking up all rows expect index 2, which is my output
    target = training_data.ix[:,[2]] 


    from sklearn import svm #Code from the URL above
    clf = svm.SVC(gamma=0.001, C=100.)
    clf.fit(data,target)  

    test_data = pd.read_csv("test_wo_Header.csv") #this is my test data

    clf.predict(test_data[-10:]) #predicting the last 10 values

Here is the output:

array([7734, 7734, 7734, 7734, 7734, 7734, 7734, 7734, 7734, 7734], dtype=int64)

I even tried using LinearSVC. Still no luck. The only difference is that the predicted output is a different value (9240), but same throughout ...

like image 210
Sudhendu Avatar asked Sep 15 '25 23:09

Sudhendu


1 Answers

Always giving the same output can have 2 causes :

  • your model is overfitting (unbalanced dataset ?)
  • you're not giving the correct data to your model

You didn't seem to convert your Pandas DataFrame to a numpy array, try

clf = svm.SVC()    
X = data.values
Y = target.values
assert len(X) == len(Y)

clf.fit(X,Y)
print clf.score(X,Y)

Do the same for your test data and try to print at least the shape of your data and one element of your arrays.

like image 178
dooms Avatar answered Sep 18 '25 18:09

dooms