Tree Classifier - Focus on percentile parameter

Importing Modules


In [ ]:
from sklearn import tree
# from email_preprocess import preprocess

Run Variables Setup If Necessary


In [ ]:
if 'features_train' not in locals() or globals():
    %run ../dev/environment_setup.ipynb

Functions


In [ ]:
def preprocess2 (number):
    
#     words_file = "../data/word_data.pkl"
#     authors_file="../data/email_authors.pkl"
    
    ### the words (features) and authors (labels), already largely preprocessed
    ### this preprocessing will be repeated in the text learning mini-project
    word_data = pickle.load( open("../data/word_data.pkl", "r"))
    authors = pickle.load( open("../data/email_authors.pkl", "r") )

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



    ### text vectorization--go from strings to lists of numbers
    vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
                                 stop_words='english')
    features_train_transformed = vectorizer.fit_transform(features_train)
    features_test_transformed  = vectorizer.transform(features_test)



    ### feature selection, because text is super high dimensional and
    ### can be really computationally chewy as a result
    selector = SelectPercentile(f_classif, percentile=number)
    selector.fit(features_train_transformed, labels_train)
    features_train_transformed = selector.transform(features_train_transformed).toarray()
    features_test_transformed  = selector.transform(features_test_transformed).toarray()
       

    return features_train_transformed, features_test_transformed, labels_train, labels_test

Re run Preprocess with percentile parameter = 1


In [ ]:
features_train, features_test, labels_train, labels_test = preprocess2(1)

Load Tree Classifier


In [ ]:
clf = tree.DecisionTreeClassifier(min_samples_split=40)

Train and Predict Data


In [ ]:
train_predict_fulldataset("Train and Predict Data with percentile = 1")

Re run Preprocess with percentile parameter = 10


In [ ]:
features_train, features_test, labels_train, labels_test = preprocess2(10)

Train and Predict Data


In [ ]:
train_predict("Train and Predict Data with percentile = 10")