コード例 #1
0
def NBAccuracy(features_train, labels_train, features_test, labels_test):
    """ compute the accuracy of your Naive Bayes classifier """
    ### import the sklearn module for GaussianNB
    from sklearn.naive_bayes import GaussianNB
    from sklearn.metrics import accuracy_score

    ### create classifier
    clf = GaussianNB() #TODO

    ### fit the classifier on the training features and labels
    clf.fit(features_train, labels_train)#TODO

    ### use the trained classifier to predict labels for the test features
    pred = clf.predict([[-0.8, -1]])#TODO


    ### calculate and return the accuracy on the test data
    ### this is slightly different than the example, 
    ### where we just print the accuracy
    ### you might need to import an sklearn module
    accuracy = clf.accuracy_score(features_train, labels_train)#TODO
    return accuracy

    """
コード例 #2
0
import sys
from time import time
sys.path.append("../tools/")
from email_preprocess import preprocess
from sklearn.naive_bayes import GaussianNB
from sklearn.sklearn.metrics import accuracy_score

### features_train and features_test are the features for the training
### and testing datasets, respectively
### labels_train and labels_test are the corresponding item labels
features_train, features_test, labels_train, labels_test = preprocess()

#########################################################
### your code goes here ###
t0 = time.time()
cls = GaussianNB()

cls.fit(features_train, labels_test)
t1 = time.time()
time_to_fitness = t1 - t0

accuracy = cls.accuracy_score(features_test, labels_test)
t2 = time.time()
print(
    "Achieved accuracy {} in time {} ({} seconds to fit the classifier, {} seconds to test accuracy"
    .format(accuracy, t2 - t0, time_to_fitness, t2 - t1))

return cls

#########################################################