SVM 方法可以处理一维数据进行预测吗?
Can SVM method deal with 1 dimensional data for forecasting?
我正在研究使用 SVM 来预测一个特定一维数据的未来值。数据包含 54 个月的销售额及其月份索引从 1 到 54。第一个问题是我认为 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],一维数据集。
用于回归目的的 SVM 称为 支持向量回归 (SVR),它在 sklearn 模块中可用。
而不是 svm.SVC()
您需要使用带有适当参数的 svm.SVR()
。是的,一维数据应该没问题。
是的,您可以使用回归算法进行预测。描述了如何使回归算法适应预测问题的一般方法 here。
还要确保正确评估预测算法。当您使用 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
我正在研究使用 SVM 来预测一个特定一维数据的未来值。数据包含 54 个月的销售额及其月份索引从 1 到 54。第一个问题是我认为 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],一维数据集。
用于回归目的的 SVM 称为 支持向量回归 (SVR),它在 sklearn 模块中可用。
而不是 svm.SVC()
您需要使用带有适当参数的 svm.SVR()
。是的,一维数据应该没问题。
是的,您可以使用回归算法进行预测。描述了如何使回归算法适应预测问题的一般方法 here。
还要确保正确评估预测算法。当您使用 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