斯坦福机器学习第六次作业第二部分
一、第一篇优快云
一直从刚开始看这门课并做作业开始就有参考优快云上面的代码和相应解析。就拿这篇文章的作业来说,其实邮件分类这块儿作业真正需要我们写的不多,但是,像我这种刚看英文文献的人,理解他让我干什么用了很久。所以想写一篇优快云,希望可以让更多的人能更快的理解他让我们具体干什么。
二、说下需要在这部分做什么
在作业需求中需要我们做的只有2部分代码
1.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.
最直接用百度翻译过来就是:在代码中,会给您一个字符串str,它是处理过的电子邮件中的单词。您应该在词汇表中vocabList 查找该单词,并确定该单词是否存在于词汇表中。如果单词存在,则应将单词的索引添加到单词索引变量中。如果单词不存在,因此不在词汇表中,则可以跳过该单词。
2.You should now complete the code in emailFeatures.m to generate a feature vector for an email, given the word indices.
这一部分让我们干的就比较明确,通过上一步得到的word_indices,取出word_indices的数值,并另x中的位置为1
三、思路解析和代码
1.在第二部分,我们已经明确了任务,通过任务我们可以提取出对我们有效的信息如下:
(1)字符串str:它是处理过的电子邮件中的一个单词,即被比较的单词。
(2)在词汇表中vocabList 查找该单词。
因此在这一部分就需要查找该单词:由于vocabList不止一个单词,因此,就要有个循环。
// An highlighted block
for i=1:length(vocabList)
(3)并确定该单词是否存在于词汇表中,这句话就很容易判断需要一个判断,而判断的内容就是两个字符串是否相同。
// An highlighted block
if (strcmp(str,vocabList{i}))
(4)如果单词存在,则应将单词的索引添加到单词索引变量中。如果单词不存在,因此不在词汇表中,则可以跳过该单词。
// An highlighted block
word_indices = [word_indices; i];
完整填充代码如下:
// An highlighted block
for i=1:length(vocabList)
if (strcmp(str,vocabList{i}))
word_indices = [word_indices; i];
break;
end
end
2.这一部分让我们干的就比较明确,通过上一步得到的word_indices,取出word_indices的数值,并另x中的位置为1。
个人认为这比较好理解,就是需要一个循环。
// An highlighted block
for j=1:length(word_indices)
x(word_indices(j))=1;
end