In [2]:
%load scripts/poi_flag_email.py

In [4]:
#writefile scripts/poi_flag_email.py
#!/usr/bin/python

###
### in poiFlagEmail() below, write code that returns a boolean
### indicating if a given email is from a POI
###

import sys
import reader
import poi_emails

def getToFromStrings(f):
    f.seek(0)
    to_string, from_string, cc_string   = reader.getAddresses(f)
    to_emails   = reader.parseAddresses( to_string )
    from_emails = reader.parseAddresses( from_string )
    cc_emails   = reader.parseAddresses( cc_string )

    return to_emails, from_emails, cc_emails


### POI flag an email

def poiFlagEmail(f):
    """ given an email file f,
        return a trio of booleans for whether that email is
        to, from, or cc'ing a poi """

    to_emails, from_emails, cc_emails = getToFromStrings(f)

    ### list of email addresses of all the POIs
    poi_email_list = poi_emails.poiEmails()

    to_poi = False
    from_poi = False
    cc_poi   = False

    ### to_poi and cc_poi are related functions, which flag whether
    ### the email under inspection is addressed to a POI, or if a POI is in cc
    ### you don't have to change this code at all

    ### there can be many "to" emails, but only one "from", so the
    ### "to" processing needs to be a little more complicated
    if to_emails:
        ctr = 0
        while not to_poi and ctr < len(to_emails):
            if to_emails[ctr] in poi_email_list:
                to_poi = True
            ctr += 1
    if cc_emails:
        ctr = 0
        while not to_poi and ctr < len(cc_emails):
            if cc_emails[ctr] in poi_email_list:
                cc_poi = True
            ctr += 1


    #################################
    ######## your code below ########
    ### set from_poi to True if #####
    ### the email is from a POI #####
    #################################

    if from_emails:
        ctr = 0
        while not from_poi and ctr < len(from_emails):
            if from_emails[ctr] in poi_email_list:
                from_poi = True
            ctr += 1    
    
    

    #################################
    return to_poi, from_poi, cc_poi


Overwriting scripts/poi_flag_email.py

In [6]:
#from sklearn.feature_extraction.text import TfidfVectorizer
#help(TfidfVectorizer)

In [1]:
%load ../ud120-projects/feature_selection/find_signature.py

In [119]:
#!/usr/bin/python

import pickle
import numpy
numpy.random.seed(42)


### the words (features) and authors (labels), already largely processed
words_file = "../ud120-projects/text_learning/your_word_data.pkl" ### like the file you made in the last mini-project 
authors_file = "../ud120-projects/text_learning/your_email_authors.pkl"  ### this too
word_data = pickle.load( open(words_file, "r"))
authors = pickle.load( open(authors_file, "r") )



### test_size is the percentage of events assigned to the test set (remainder go into training)
from sklearn import cross_validation
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(word_data, authors, test_size=0.1, random_state=42)


from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
                             stop_words='english')
features_train = vectorizer.fit_transform(features_train).toarray()
features_test  = vectorizer.transform(features_test).toarray()


### a classic way to overfit is to use a small number
### of data points and a large number of features
### train on only 150 events to put ourselves in this regime
features_train = features_train[:150]
labels_train   = labels_train[:150]



### your code goes here
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()

model.fit(features_train, labels_train)

pred_train = model.predict(features_train)
pred_test = model.predict(features_test)

overfit_acc_train = accuracy_score(labels_train, pred_train)
overfit_acc_test = accuracy_score(labels_test, pred_test)
print len(labels_train)
print overfit_acc_test
print overfit_acc_train


150
0.816837315131
1.0

In [107]:
print [x for x in sorted(model.feature_importances_, reverse=True) if x > 0.2]


[0.36363636363636365]

In [108]:
print numpy.argmax(model.feature_importances_)


21323

In [109]:
model.feature_importances_.max()


Out[109]:
0.36363636363636365

In [111]:
[(feature,key) for key,feature in enumerate(model.feature_importances_) if feature >= 0.2]


Out[111]:
[(0.36363636363636365, 21323)]

In [112]:
model.feature_importances_.size


Out[112]:
37861

In [114]:
vectorizer.get_feature_names()[33614]


Out[114]:
u'sshacklsims1rcsntxswbellnet'

In [98]:
vectorizer.get_feature_names()[14343]


Out[98]:
u'cgermannsf'

In [115]:
vectorizer.get_feature_names()[21323]


Out[115]:
u'houectect'

In [ ]: