2016年10月26日 星期三

[Introduction to Machine Learning] 程式實做 Quiz: Features In The Enron Dataset & Quiz: How Many POIs Exist?

[Introduction to Machine Learning]  程式實做 Quiz: Features In The Enron Dataset

Python 的特色在於處理文字類型的資料,其中有個東西叫作 Dictionary

可以當作儲存不同型別的資料的工具


其中在 Datasets and Questions 這個章節中

問題1 : 我們需要找到 Enron_data 中總共有多少"人"

要計算總共有多少人的話,根據 Quiz: Size of the Enron Dataset 的提示

就相當於找到 dictionary 中的 key 的數目 (key:value pair中的key)

詳細可以看 5.5. Dictionaries 的解說


問題2 : 每個姓名總共有"幾種"不同種類的資料

要計算總共有幾種的話,根據論壇中的這篇發問 [Intro to Machine Learning] Solution to Lesson 5 quiz question (“finding the POIs in the Enron Data”) 

我們能夠順利找到總共有幾"種"

(但是實際執行的時候發現答案只有18,與真正的答案 21 差了 3 個... debug中)

附上正確答案 (Datasets and Question

除錯:
根據目前debug的結果可以發現,我猜測是之後有題目是要求找具有 'poi' 的項目總共有幾個

裡面的確有一項是 'poi',所以要找 value 項是 poi 且為 true 的項目

所以不是python的什麼神奇指令 poi ,完全是誤會一場...XD

所以是我誤會了,程式碼本身沒有問題,是我誤會了題目跟程式碼之間的關聯性

所以上的正確答案是對的,直接用 len() 函式把任意一個 key 的子dictionary長度求出來

(想像成資料結構是父dictionary裡面有子dictionary就好,只是父dictionary存的key/value pair是一個一個的姓名,子dictionary是存一個一個的信件流向的資料)

得到的數值就是dictionary的value總個數

程式碼如下
-------------------------------------------------------
for key,value in enron_data.items(): #enron_data 是課堂練習用的資料庫
    if value["poi"] == True:
        count += 1
        print count

-------------------------------------------------------
[Introduction to Machine Learning] 程式實做 Quiz: Quiz: How Many POIs Exist?

We compiled a list of all POI names (in ../final_project/poi_names.txt) and associated email addresses (in ../final_project/poi_email_addresses.py).

How many POI’s were there total? (Use the names file, not the email addresses, since many folks have more than one address and a few didn’t work for Enron, so we don’t have their emails.)

Lesson 5 - How many POIs exist?

小心多餘的那兩行,所以記得在計算len()的時候記得把不包含姓名的那兩段劃掉!

#----------------------------------------------------------#
* Python debugger 工具 *

方案1 ubuntu cmd中搭配 Ipython/pdb 文字指令式 除錯工具
方案2 IDE gui 圖形化介面 除錯

難度 : 2 > 1
自由度(可適應Linux系統環境)與挑戰自己能力: 1>2


沒有留言:

張貼留言

/* 載入prettify的autoloader */ /* 載入JQuery */