[Coursera机器学习]Support Vector Machines WEEK7编程作业
来源:互联网 发布:画平面图软件 编辑:程序博客网 时间:2024/06/05 20:14
1.2 SVM with Gaussian Kernels
You should now complete the code in gaussianKernel.m to compute the Gaussian kernel between two examples, (x(i); x(j)). The Gaussian kernel function is dened as:
% ====================== YOUR CODE HERE ======================% Instructions: Fill in this function to return the similarity between x1% and x2 computed using a Gaussian kernel with bandwidth% sigma%%sim = exp(-(sum((x1 - x2).^2)) / (2 * sigma^2));
1.2.3 Example Dataset 3
Your task is to use the cross validation set Xval, yval to determine the best C and parameter
% ====================== YOUR CODE HERE ======================% Instructions: Fill in this function to return the optimal C and sigma% learning parameters found using the cross validation set.% You can use svmPredict to predict the labels on the cross% validation set. For example, % predictions = svmPredict(model, Xval);% will return the predictions on the cross validation set.%% Note: You can compute the prediction error using % mean(double(predictions ~= yval))%steps = [ 0.01 0.03 0.1 0.3 1 3 10 30 ];minError = Inf;minC = Inf;minSigma = Inf;% i*j means every condition of different C and Sigma.for i = 1:length(steps) for j = 1:length(steps) currentC = steps(i); currentSigma = steps(j); model = svmTrain(X, y, currentC, @(x1, x2) gaussianKernel(x1, x2, currentSigma)); predictions = svmPredict(model, Xval); error = mean(double(predictions ~= yval)); if(error < minError) minError = error; minC = currentC; minSigma = currentSigma; end end endC = minC;sigma = minSigma;
2.1.1 Vocabulary List
Your task now is to complete the code in processEmail.m to perform this mapping. In the code, you are given a string str which is a single word from the processed email. You should look up the word in the vocabulary list vocabList and find if the word exists in the vocabulary list. If the word exists, you should add the index of the word into the word indices variable. If the word does not exist, and is therefore not in the vocabulary, you can skip the word.
% Look up the word in the dictionary and add to word_indices if % found % ====================== YOUR CODE HERE ====================== % Instructions: Fill in this function to add the index of str to % word_indices if it is in the vocabulary. At this point % of the code, you have a stemmed word from the email in % the variable str. You should look up str in the % vocabulary list (vocabList). If a match exists, you % should add the index of the word to the word_indices % vector. Concretely, if str = 'action', then you should % look up the vocabulary list to find where in vocabList % 'action' appears. For example, if vocabList{18} = % 'action', then, you should add 18 to the word_indices % vector (e.g., word_indices = [word_indices ; 18]; ). % % Note: vocabList{idx} returns a the word with index idx in the % vocabulary list. % % Note: You can use strcmp(str1, str2) to compare two strings (str1 and % str2). It will return 1 only if the two strings are equivalent. % for i = 1:length(vocabList) if(strcmp(vocabList(i), str)) word_indices = [word_indices; i] break; end end
2.2 Extracting Features from Emails
You should now complete the code in emailFeatures.m to generate a feature vector for an email, given the word_indices.
% ====================== YOUR CODE HERE ======================% Instructions: Fill in this function to return a feature vector for the% given email (word_indices). To help make it easier to % process the emails, we have have already pre-processed each% email and converted each word in the email into an index in% a fixed dictionary (of 1899 words). The variable% word_indices contains the list of indices of the words% which occur in one email.% % Concretely, if an email has the text:%% The quick brown fox jumped over the lazy dog.%% Then, the word_indices vector for this text might look % like:% % 60 100 33 44 10 53 60 58 5%% where, we have mapped each word onto a number, for example:%% the -- 60% quick -- 100% ...%% (note: the above numbers are just an example and are not the% actual mappings).%% Your task is take one such word_indices vector and construct% a binary feature vector that indicates whether a particular% word occurs in the email. That is, x(i) = 1 when word i% is present in the email. Concretely, if the word 'the' (say,% index 60) appears in the email, then x(60) = 1. The feature% vector should look like:%% x = [ 0 0 0 0 1 0 0 0 ... 0 0 0 0 1 ... 0 0 0 1 0 ..];%%for i = 1:length(word_indices) x(word_indices(i)) = 1;end
- [Coursera机器学习]Support Vector Machines WEEK7编程作业
- Andrew Ng机器学习week7(Support Vector Machines)编程习题
- 【Coursera】Machine learning - week7 : Support Vector Machines
- [机器学习] Coursera笔记 - Support Vector Machines
- Stanford 机器学习笔记 Week7 Support Vector Machines
- coursera机器学习笔记之“Support Vector Machines(SVMs)”
- 斯坦福机器学习视频笔记 Week7 支持向量机 Support Vector Machines
- Andrew NG 机器学习 笔记-week7-支持向量机(Support Vector Machines)
- 测试【Machine Learning week7】Support Vector Machines
- 机器学习 Support Vector Machines 1
- 机器学习 Support Vector Machines 2
- 机器学习 Support Vector Machines 3
- Coursera ML笔记 -----week7 Support Vector Machine
- Coursera机器学习 week7 assignment
- Coursera机器学习 Week7 笔记
- 【Stanford机器学习笔记】10-Support Vector Machines
- 机器学习之支持向量机: Support Vector Machines (SVM)
- Andrew NG 机器学习 练习6-Support Vector Machines
- swift3.0下闭包语法汇总
- 超像素分割(Superpixel Segmentation)发展
- 常见对象_Calendar类的add()和set()方法
- CSS揭秘--笔记--扩大可点击区域
- sdut oj3-5 学生成绩统计
- [Coursera机器学习]Support Vector Machines WEEK7编程作业
- Python+opencv3.0配置
- java细节,防止内存泄露
- 关于decorator 的理解
- 线性筛选求素数
- sass认识
- JAVA多线程售票问题
- JAVA WEB 开发环境的的搭建
- 给View添加特定数量的Border