sklearn: SVR无法泛化加法函数

这是我用来学习加法函数(y=x1 + x2)的SVR模型:

%reset -f#Libsfrom sklearn import svm;#PROGRAMME ENTRY POINT==========================================================#Data, addition#Exp[I] = sum(Inp[I])Inp = [[1,2],[3,4],[5,6],[7,8],[9,0]];Exp = [ 3,    7,    11,   15,   9   ];#TrainModel = svm.SVR(kernel="poly",degree=3);Model.fit(Inp,Exp);#Inferprint("Input values are those in the train data:");print(f"1 + 2 = {Model.predict([[1,2]])[0]:.6f}");print("\nInput values are those in the train data:");print(f"5 + 6 = {Model.predict([[5,6]])[0]:.6f}");print("\nInput values are those NOT in the train data, but in range:");print(f"5 + 5 = {Model.predict([[5,5]])[0]:.6f}");print("\nInput values are those NOT in the train data, and OUT of range:");print(f"9 + 1 = {Model.predict([[9,1]])[0]:.6f}");#EOF

但结果与预期不符:

Input values are those in the train data:1 + 2 = 6.007171Input values are those in the train data:5 + 6 = 9.595818Input values are those NOT in the train data, but in range:5 + 5 = 8.533934Input values are those NOT in the train data, and OUT of range:9 + 1 = 9.170507

sklearn的SVR是否有可能泛化加法函数?在上面的代码中应该做哪些更改才能让SVR学习到x1+x2?


回答:

三次多项式核函数的方差过大,无法正确预测如此简单的函数,尤其是在如此小的数据集上。这是基于偏差/方差权衡的。你的模型在方差上损失了很多,而在偏差上几乎没有收益(你的函数过于复杂)。即使是低阶多项式和径向基函数也是如此。

降低模型的方差可以解决这个问题。只需使用线性核函数即可。

Model = svm.SVR(kernel="linear")

使用线性核函数的SVM结果如下:

Input values are those in the train data:1 + 2 = 3.100000Input values are those in the train data:5 + 6 = 10.966667Input values are those NOT in the train data, but in range:5 + 5 = 9.983333Input values are those NOT in the train data, and OUT of range:9 + 1 = 9.983333

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注