Coursera-MachineLearning-Week7编程题目整理

gaussianKernel.m

sim = exp(-sum((x1-x2).^2)/(sigma.^2*2));  % similarity(x,l^(1)) = exp(-(||x-l^(1)||^(2))/2Sigma^2)

dataset3Params.m

steps = [0.01,0.03,0.1,0.3,1,3,10,30];
minError = Inf;
minC = Inf;
minSigma = Inf;
 
 
for i = 1:length(steps)  %遍历所有的选项
    for j = 1:length(steps)  %遍历所有的选项
        currC = steps(i);  %选择C
        currSigma = steps(j);  %选择Sigma
        %训练模型
        model = svmTrain(X, y, currC, @(x1, x2) gaussianKernel(x1, x2, currSigma));
        predictions = svmPredict(model, Xval);  %进行预测
        error = mean(double(predictions ~= yval));  %计算误差
        %选择最小误差的参数
        if(error < minError)
            minError = error;
            minC = currC;
            minSigma = currSigma;
        end
    end
end
 
C = minC;
sigma = minSigma;

processEmail.m

    %遍历词汇表
    for i = 1:length(vocabList)
        if(strcmp(vocabList(i), str))  %如果相等
            word_indices = [word_indices; i];  %添加索引
            break;
        end
    end

emailFeatures.m

for i = 1:length(word_indices)  %遍历单词向量表
    x(word_indices(i)) = 1  %将对应的值置为1
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值