SVM方法能处理一维数据的预测吗?

问题描述 投票:0回答:1

我正在研究使用SVM来预测一个特定的1维数据的未来值。该数据包含54个月的销售值,其月份指数从1到54。第一个问题是,我认为SVM可以做预测,但我并不确定。据我所知,SVM可以做分类,但是回归呢?谁能告诉我为什么SVM可以做回归?

而在我的问题中,我试着把X设为月份指数,y设为每个月的值。我不太确定我的做法是否正确,因为没有标签(或者标签我已经用值累了),特征只有月份指数。

我试着通过以下方法来适应它 from sklearn import svm 并得到训练集的准确率为100%,测试集的准确率为0的结果。我不知道哪里错了。

下面是代码。

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))

数据集是这样的 X=[1,2,3,4,...,53,54] y=[90,18,65,150.... 289],1D数据集。

python machine-learning time-series svm
1个回答
0
投票

用于回归目的的SVM被称为 支持向量回归(SVR) 而它在 sklearn 模块。

而不是 svm.SVC() 你需要使用 svm.SVR() 与适当的参数。是的,一维数据应该是可以的。

这里有一个更完整的例子.


1
投票

是的,你可以使用回归算法进行预测。现介绍如何使回归算法适应预测问题的一般方法。此处.

此外,还要确保你正确评估你的预测算法。当你使用 train_test_split 你随机洗牌和拆分你的数据。相反,你应该只使用过去的数据来适合你的算法,并根据未来的数据进行评估。

如果你有兴趣,我们正在开发一个工具箱,它扩展了scikit-learn,正是为了这些用例。所以有了 sktime,你可以简单地写。

import numpy as np
from sktime.datasets import load_airline
from sktime.forecasting.compose import ReducedRegressionForecaster
from sklearn.svm import SVR
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.performance_metrics.forecasting import smape_loss

y = load_airline()  # load 1-dimensional time series
y_train, y_test = temporal_train_test_split(y)  
fh = np.arange(1, len(y_test) + 1)  # forecasting horizon
regressor = SVR()  
forecaster = ReducedRegressionForecaster(regressor, window_length=10)
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
print(smape_loss(y_test, y_pred))
>>> 0.139046791779424
© www.soinside.com 2019 - 2024. All rights reserved.