Issue
I am working on using SVM to predict the future values of one particular 1D data. The data contains 54 month sales values and with their month indexes from 1 to 54. The first problem is that I think the SVM could do the prediction but I am not quit sure about it. As I know, SVM could do the classification but how about the regression? Could anyone tell me about why SVM can do the regression?
And in my problem, I tried to set the X as month indexes and y as the value for each month. I am not quite sure if I was doing right as there is no label(or the label I've tired using the value) and feature is only the month index.
I tried to fit it by from sklearn import svm
and get the result that the accuracy for training set is 100% and 0 for testing set. I do not know where is wrong.
Here's the code:
import pandas as pd
import numpy as np
df = pd.read_csv('11.csv', header=None, names = ['a', 'b', 'c'])
X = df['b'].values.reshape(-1,1)
y = df['c'].values.reshape(-1,1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
from sklearn import svm
clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
clf.fit(X_train, y_train.ravel())
print("training result:",clf.score(X_train, y_train))
print("testing result:",clf.score(X_test,y_test))
The dataset looks like this X = [1, 2, 3, 4,...,53, 54] and y = [90, 18, 65, 150.... 289], 1D dataset.
Solution
SVM for regression purposes is called Support Vector Regression (SVR) and it's available in the sklearn module.
Instead of svm.SVC()
you need to use svm.SVR()
with the appropriate parameters. And yes, 1D data should be fine.
Here is a more complete example.
Answered By - karlphillip
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.