Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要Python和Matlab中SVM的帮助吗_Python_Matlab_Machine Learning_Scikit Learn_Svm - Fatal编程技术网

需要Python和Matlab中SVM的帮助吗

需要Python和Matlab中SVM的帮助吗,python,matlab,machine-learning,scikit-learn,svm,Python,Matlab,Machine Learning,Scikit Learn,Svm,我是机器学习和Python的初学者。我正在尝试将支持向量机应用到我的数据中,比较MATLAB和Python。我希望得到类似的结果,但我没有。我试着找出我的类似问题,但没有任何问题,否则我会错过它们 我有一套训练和测试集,这是二进制分类。 首先,我使用内置函数svmtrain和svmclassify在MATLAB中进行了实现 load('sample.mat') y_test = y_test'; y_train = y_train'; %% Training and Classification

我是机器学习和Python的初学者。我正在尝试将支持向量机应用到我的数据中,比较MATLAB和Python。我希望得到类似的结果,但我没有。我试着找出我的类似问题,但没有任何问题,否则我会错过它们

我有一套训练和测试集,这是二进制分类。 首先,我使用内置函数svmtrain和svmclassify在MATLAB中进行了实现

load('sample.mat')
y_test = y_test';
y_train = y_train';
%% Training and Classification 
svmStruct = svmtrain(X_train, y_train, 'boxconstraint', 0.1);
species = svmclassify(svmStruct, X_test);
%% Compute Accuracy
count = 0;
for i=1:length(X_test)
if species(i)==y_test(i)
    count = count+1;
end
end
count/length(X_test)*100
%% Plotting
scatter(time, X_test, [], species)
hold on 
y_test(y_test==1) = 0.03;
y_test(y_test==0) = -0.03;
plot(time, y_test, 'g')
这是测试集的结果(A类用红色分类,B类用蓝色分类,绿线是A类的实际标签

其次,我使用scikit学习包在Python中做了同样的事情

import numpy as np
import matplotlib.pyplot as plt 
import scipy.io as sio

from sklearn import svm

""" Load Data"""
LoadData = sio.loadmat('sample.mat')
X_test = LoadData['X_test'].reshape(-1, 1)
X_train = LoadData['X_train'].reshape(-1, 1)
y_test = LoadData['y_test'].T
y_train = LoadData['y_train'].T
time = LoadData['time']

""" Train Classifier """
clf = svm.LinearSVC(C=1.0)
clf.fit(X_train,y_train)

predict_X_test = clf.predict(X_test)    # Predict test set
acc = clf.score(X_test, y_test)         # Accuracy

""" Plotting """
plt.figure(1)
plt.figure(figsize=(15,8))
plt.scatter(time, X_test, c = predict_X_test)
y_test[y_test==1] = 0.03
y_test[y_test==0] = -0.03
plt.plot(time, y_test)

""" Plot Threshold """
w = clf.coef_
b = clf.intercept_ 
xx = np.linspace(300,610)
yy = -b/w*np.ones(xx.shape)
yy = yy.T
plt.plot(xx, yy, 'k-') 
这是Python中的结果。黑线是它的阈值。红线是类A的实际标签

通过使用Python,似乎阈值太高了,甚至我试图调整C参数,但根本不起作用

我现在不知道我的代码中有什么错误。任何建议都将不胜感激

编辑

我在下面的链接中做了验证曲线。我想我做得对。


蓝线是Python中的曲线,红线是MATLAB中的曲线,x轴是[0.001 0.01 0.1 1 10 100]处的C参数(我使用了log(x)),y轴是精度。

如果能看到matlab和python的验证曲线,以及不同的参数,那会很有趣C@Moritz我更新了交叉验证。在使用Python的SVM中没有任何更改。尝试在Python中使用网格搜索来优化某些参数,这有意义吗?当我看到红线时,我的第一个想法是您希望将其扩展为再往左看,看它是否接触到蓝线。(可能在-10?或-100?@DennisJaheruddin x轴是对数刻度中的C参数,为[0.001 0.01 0.1 110])。我将该值向左扩展,但精度降低。