Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support Vector Regression: TypeError: must be real number, not str

I'm trying to use sklearn SVR for a small dataset. I'm getting error when I try to fit() data

TypeError: must be real number, not str

Here is my data and code:

     Revenue   Units      Rev_per_unit
0    147754.0  8333629.0  17.73
1    126146.0  7601824.0  16.59
2    152385.0  8487163.0  17.95
3    138703.0  8170619.0  16.98
4    157860.0  8589258.0  18.38
5    159981.0  8634245.0  18.53
6    160006.0  9063836.0  17.65
7    143556.0  9315878.0  15.41
8    129380.0  9012887.0  14.35
9    135771.0  9370077.0  14.49
10   129593.0  9018405.0  14.37
11   123941.0  9410973.0  13.17

from sklearn.svm import SVR
df = pd.read_csv('revenue.csv')
X = df[['Revenue', 'Unit']]
y = df['Rev_per_unit']
X_train, X_test, y_train, y_test = train_test_split(X, y)
svr_reg = SVR(gamma='scale', C=1.0, epsilon=0.2)
svr_reg.fit(X_train, y_train)

I understand the error however when I use the same data for LinearRegression(), I do not get any error for the same X_train, y_train.

like image 981
Hannan Avatar asked Sep 30 '18 22:09

Hannan


2 Answers

The parameter gamma expects a float value, but you are passing "scale". I know the documentation is a little bit misleading at this point.

So just change gamma to a float value like here:

X_train, X_test, y_train, y_test = train_test_split(X, y)
svr_reg = SVR(gamma=0.001, C=1.0, epsilon=0.2)
svr_reg.fit(X_train, y_train)

Or just remove the gamma parameter.

like image 88
Tim Avatar answered Nov 15 '22 06:11

Tim


Had the same issue when going through the scikit-learn.org website:

>>> clf.set_params(kernel='rbf', gamma='scale').fit(X,y)

Output (Shrinked):

...
File "sklearn/svm/libsvm.pyx", line 58, in sklearn.svm.libsvm.fit
TypeError: must be real number, not str

Had to check the 'type' for gamma

>>> type(clf.gamma)
<class 'float'>

Passing a string ('scale') wouldn't have worked anyways.

The best option is to pass it a float value (gamma=0.001)

like image 31
EnVs Avatar answered Nov 15 '22 04:11

EnVs