Sklearn SVM:SVR 和 SVC,对每个输入得到相同的预测
Sklearn SVM: SVR and SVC, getting the same prediction for every input
这是代码的粘贴:SVM sample code
我检查了这个问题的其他几个答案...似乎这个问题的特定迭代有点不同。
首先,我的输入被归一化,每个点有五个输入。这些值的大小都合理(健康的 0.5s 和 0.7s 等——很少接近零或接近 1 的数字)。
我有大约 70 个 x 输入对应于他们的 70 个 y 输入。 y 输入也被归一化(它们是我的函数在每个时间步后的百分比变化)。
我初始化我的 SVR(和 SVC),训练它们,然后用 30 个样本外输入来测试它们......并且对每个输入都得到完全相同的预测(并且输入以合理的数量变化--0.3、0.6、0.5 等)。我认为分类器(至少)会有一些区别...
这是我得到的代码:
# train svr
my_svr = svm.SVR()
my_svr.fit(x_training,y_trainr)
# train svc
my_svc = svm.SVC()
my_svc.fit(x_training,y_trainc)
# predict regression
p_regression = my_svr.predict(x_test)
p_r_series = pd.Series(index=y_testing.index,data=p_regression)
# predict classification
p_classification = my_svc.predict(x_test)
p_c_series = pd.Series(index=y_testing_classification.index,data=p_classification)
这里是我输入的示例:
x_training = [[ 1.52068627e-04 8.66880301e-01 5.08504362e-01 9.48082047e-01
7.01156322e-01],
[ 6.68130520e-01 9.07506250e-01 5.07182647e-01 8.11290634e-01
6.67756208e-01],
... x 70 ]
y_trainr = [-0.00723209 -0.01788079 0.00741741 -0.00200805 -0.00737761 0.00202704 ...]
y_trainc = [ 0. 0. 1. 0. 0. 1. 1. 0. ...]
并且 x_test
矩阵 (5x30) 在输入的大小和方差方面与 x_training
矩阵相似...与 y_testr
和 y_testc
相同.
目前,所有测试的预测完全相同(回归为 0.00596,分类为 1...)
如何让 SVR 和 SVC 函数吐出相关预测?或者至少基于输入的不同预测...
至少,分类器应该能够做出选择。我的意思是,即使我没有提供足够的回归维度...
尝试增加默认值的 C。看来你欠拟合了。
my_svc = svm.SVC(probability=True, C=1000)
my_svc.fit(x_training,y_trainc)
p_classification = my_svc.predict(x_test)
p_classification 则变为:
array([ 1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
1., 0., 0., 0., 0., 0., 1., 1., 0., 1., 1., 1., 1.,
1., 1., 1., 1.])
对于 SVR
的情况,您还需要减少 epsilon。
my_svr = svm.SVR(C=1000, epsilon=0.0001)
my_svr.fit(x_training,y_trainr)
p_regression = my_svr.predict(x_test)
p_regression 则变为:
array([-0.00430622, 0.00022762, 0.00595002, -0.02037147, -0.0003767 ,
0.00212401, 0.00018503, -0.00245148, -0.00109994, -0.00728342,
-0.00603862, -0.00321413, -0.00922082, -0.00129351, 0.00086844,
0.00380351, -0.0209799 , 0.00495681, 0.0070937 , 0.00525708,
-0.00777854, 0.00346639, 0.0070703 , -0.00082952, 0.00246366,
0.03007465, 0.01172834, 0.0135077 , 0.00883518, 0.00399232])
您应该使用交叉验证来调整您的 C 参数,以便它能够在对您最重要的指标上表现最佳。您可能需要查看 GridSearchCV
来帮助您做到这一点。
我遇到了同样的问题,但原因完全不同,因此寻找解决方案的地方也完全不同。
如果您的预测输入因任何原因缩放不正确,您可能会遇到与此处相同的症状。这可能是在以后的预测中忘记(或错误编码)输入值的缩放,或者是由于输入的顺序错误。
在我的例子中,我需要使用 sklearn 包中的 StandardScaler 来缩放我的数据。
在我的例子中,我还必须独立缩放每组特征,两种类型的距离分别缩放。
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
ss.fit(X[:,0:10])
X[:,0:10] = ss.transform(X[:,0:10])
ss = StandardScaler()
ss.fit(X[:,10:20])
X[:,10:20] = ss.transform(X[:,10:20])
这是代码的粘贴:SVM sample code
我检查了这个问题的其他几个答案...似乎这个问题的特定迭代有点不同。
首先,我的输入被归一化,每个点有五个输入。这些值的大小都合理(健康的 0.5s 和 0.7s 等——很少接近零或接近 1 的数字)。
我有大约 70 个 x 输入对应于他们的 70 个 y 输入。 y 输入也被归一化(它们是我的函数在每个时间步后的百分比变化)。
我初始化我的 SVR(和 SVC),训练它们,然后用 30 个样本外输入来测试它们......并且对每个输入都得到完全相同的预测(并且输入以合理的数量变化--0.3、0.6、0.5 等)。我认为分类器(至少)会有一些区别...
这是我得到的代码:
# train svr
my_svr = svm.SVR()
my_svr.fit(x_training,y_trainr)
# train svc
my_svc = svm.SVC()
my_svc.fit(x_training,y_trainc)
# predict regression
p_regression = my_svr.predict(x_test)
p_r_series = pd.Series(index=y_testing.index,data=p_regression)
# predict classification
p_classification = my_svc.predict(x_test)
p_c_series = pd.Series(index=y_testing_classification.index,data=p_classification)
这里是我输入的示例:
x_training = [[ 1.52068627e-04 8.66880301e-01 5.08504362e-01 9.48082047e-01
7.01156322e-01],
[ 6.68130520e-01 9.07506250e-01 5.07182647e-01 8.11290634e-01
6.67756208e-01],
... x 70 ]
y_trainr = [-0.00723209 -0.01788079 0.00741741 -0.00200805 -0.00737761 0.00202704 ...]
y_trainc = [ 0. 0. 1. 0. 0. 1. 1. 0. ...]
并且 x_test
矩阵 (5x30) 在输入的大小和方差方面与 x_training
矩阵相似...与 y_testr
和 y_testc
相同.
目前,所有测试的预测完全相同(回归为 0.00596,分类为 1...)
如何让 SVR 和 SVC 函数吐出相关预测?或者至少基于输入的不同预测...
至少,分类器应该能够做出选择。我的意思是,即使我没有提供足够的回归维度...
尝试增加默认值的 C。看来你欠拟合了。
my_svc = svm.SVC(probability=True, C=1000)
my_svc.fit(x_training,y_trainc)
p_classification = my_svc.predict(x_test)
p_classification 则变为:
array([ 1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
1., 0., 0., 0., 0., 0., 1., 1., 0., 1., 1., 1., 1.,
1., 1., 1., 1.])
对于 SVR
的情况,您还需要减少 epsilon。
my_svr = svm.SVR(C=1000, epsilon=0.0001)
my_svr.fit(x_training,y_trainr)
p_regression = my_svr.predict(x_test)
p_regression 则变为:
array([-0.00430622, 0.00022762, 0.00595002, -0.02037147, -0.0003767 ,
0.00212401, 0.00018503, -0.00245148, -0.00109994, -0.00728342,
-0.00603862, -0.00321413, -0.00922082, -0.00129351, 0.00086844,
0.00380351, -0.0209799 , 0.00495681, 0.0070937 , 0.00525708,
-0.00777854, 0.00346639, 0.0070703 , -0.00082952, 0.00246366,
0.03007465, 0.01172834, 0.0135077 , 0.00883518, 0.00399232])
您应该使用交叉验证来调整您的 C 参数,以便它能够在对您最重要的指标上表现最佳。您可能需要查看 GridSearchCV
来帮助您做到这一点。
我遇到了同样的问题,但原因完全不同,因此寻找解决方案的地方也完全不同。
如果您的预测输入因任何原因缩放不正确,您可能会遇到与此处相同的症状。这可能是在以后的预测中忘记(或错误编码)输入值的缩放,或者是由于输入的顺序错误。
在我的例子中,我需要使用 sklearn 包中的 StandardScaler 来缩放我的数据。
在我的例子中,我还必须独立缩放每组特征,两种类型的距离分别缩放。
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
ss.fit(X[:,0:10])
X[:,0:10] = ss.transform(X[:,0:10])
ss = StandardScaler()
ss.fit(X[:,10:20])
X[:,10:20] = ss.transform(X[:,10:20])