支持向量机SVM相关代码 吴恩达机器学习第六次作业

高斯核函数(相似度函数)

function sim = gaussianKernel(x1, x2, sigma)
%相似度函数
sim=exp(-sum((x1-x2).^2)/(2*sigma*sigma));

end

选择最优参数

function [C, sigma] = dataset3Params(X, y, Xval, yval)

C=1;
sigma=0.3;
params = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];%参数列表
min_error=0;%记录最小错误率

for c=params
    for s=params
        model=svmTrain(X, y, c, @(x1, x2) gaussianKernel(x1, x2, s));
        predictions=svmPredict(model,Xval);
        error=mean(double(predictions~=yval));
        
        if min_error==0||error<min_error%保留错误最小的C和sigma
            min_error=error;
            C=c;
            sigma=s;
        end
    end
end
end

将邮件字符处理为数字(若字典中的字符在邮件中出现,对应的数字编号放入 word_indices中)

    for i=1:length(vocabList)
        if strcmp(vocabList{i},str)
            word_indices = [word_indices; i];
        end
    end

构建特征矩阵

for i=1:length(word_indices)
    x(word_indices(i,1),1)=1
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值