Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem making time series predictions with SVM and Matlab. I tried to solve the problem by myself in several ways without success.

I downloaded, compiled and installed LibSVM scripts for Matlab. But I don't know how to format my data in input. I mean, I have a time series that is an array of values, something like:

x=[1 2 3 4 5 6 7 8 9 10]

I want to use 70% of the vector as a training sequence of the SVM:

x1=[1 2 3 4 5 6 7]

Then I have to predict the last three values of the time series and I have to calculate the error between predicted values and the array [8 9 10].

I have to understand:

  1. How to set SVM parameters. But there is a script that I found here that is the Matlab version of So problem solved.

  2. How to format my time series in order to be "acceptable" with (easy.m), svmtrain and svmpredict.

On LibSVM FAQ website I found this procedure [code]:

matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
matlab> labels = SPECTF(:, 1); % labels from the 1st column
matlab> features = SPECTF(:, 2:end);
matlab> features_sparse = sparse(features); % features must be in a sparse matrix
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);

But I think this is a procedure for classification. From my point of view, how do I distinguish labels and features if I have only a simple array of values?

Here I found a similar procedure, but it is not clear if the script really works or not (I obtain a translated version of the input)

Can anyone help me?

share|improve this question

closed as off-topic by Matt Krause, gung, Nick Cox, COOLSerdash, Peter Flom Jul 6 '13 at 12:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about programming are off-topic here unless they involve statistical analysis in some fashion, but they can be asked on Stack Overflow." – Matt Krause, gung, Nick Cox, COOLSerdash, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

Uhm, not enough information. If you really only have feature vector, you won't get much of a prediction. Unless the later input to the classifier is exactly what you trained it with. In this case an SVM is overkill, just check for the known sequence. In any other case you'll at least want some negative examples. And why an SVM? It seems you want to map to multidimensional data. I'm not aware of a proven method to even do that with an SVM. What data do have and what do you expect? – Christian Mar 7 '13 at 19:50
up vote 1 down vote accepted

Welcome to CrossValidated, @Antonio!

I think your question can be reformulated like this:

I am given a sequence of values, how to apply general regression methods (like svm-regression) to this problem?

Indeed, as you mentioned, for regression, same as for classification, you need a feature vector usually denoted by $x_i$ with the response variable $y_i$, and you have a lot of samples like this: $i \in 1, \ldots, N$.

The usual approach is to set features to the time, when the value is measured. For example, if I observe

y = [11, 22, 33, 44, 55, 66, 77]

at times

x = [1, 2, 3, 4, 5, 6, 7]

then $x_i$ is a feature and $y_i$ is an observed value.

In case your observations are equidistant in MATLAB you can easily set features by writing

x1 = (1:7)'; % training set: should be column
y1 = [11, 22, 33, 44, 55, 66, 77]'; % your time series
model = svmtrain(y1, x1);
x2 = (8:10)'; % test set
y2 = [88, 99, 110]'; % hidden values that we don't use for training
[y2_predicted, accuracy] = svmpredict(y2, x2, model);

Hope it will help.

share|improve this answer
It returns y2_predicted = []!? – Rasoul Jul 30 '13 at 22:42
data              = x(1:end-1);
dataLabels        = x(2:end);
trainDataLength   = round(length(data)*70/100);
TrainingSet       = data(1:trainDataLength);
TrainingSetLabels = dataLabels(1:trainDataLength);
TestSet           = data(trainDataLength+1:end);
TestSetLabels     = dataLabels(trainDataLength+1:end);

options = ' -s 3 -t 2 -c 100 -p 0.001 -h 0';
model   = svmtrain(TrainingSetLabels, TrainingSet, options);

[predicted_label, accuracy, decision_values] = svmpredict(TestSetLabels, 
                                                          TestSet, model);
share|improve this answer
Welcome to the site, @AhmedH.Radhwan. Would you mind adding some peripheral information to situate / motivate / explain your code? Since you're new here, you may want to read our about page & our FAQ, which contain info about CV. – gung Feb 5 '13 at 17:05

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