def submitClassify(): clf = classify(features_train, labels_train) ### draw the decision boundary with the text points overlaid try: prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read()) except NameError: pass
def main(): features_train, labels_train, features_test, labels_test = makeTerrainData( ) ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] bumpy_fast = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. gamma, c = 'auto', 1.0 kernel = raw_input('Select the kernel: ') if (kernel != 'linear'): gamma = raw_input('Gamma: ') c = raw_input('C: ') clf = classify(features_train, labels_train, kernel, c, gamma) print('Python SVM Example') accuracy = clf.score(features_test, labels_test) print('Accuracy score: {}'.format(accuracy)) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("naive_bayes.png", "png", open("naive_bayes.png", "rb").read()) os.system('display naive_bayes.png &')
features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] bumpy_fast = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. clf = classify(features_train, labels_train) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read())
import pylab as pl features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==0] bumpy_fast = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==0] grade_slow = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==1] bumpy_slow = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==1] clf = classify(features_train, labels_train) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read()) # Accuracy # method 1 pred = clf.predict(features_test) accuracy = sum(labels_test == pred) / float(len(labels_test)) print("Accuracy is: ", accuracy) # method 2 from sklearn.metrics import accuracy_score
from prep_terrain_data import makeTerrainData from class_vis import prettyPicture, output_image from ClassifyNB import classify import numpy as np import pylab as pl features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0] bumpy_fast = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0] grade_slow = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1] bumpy_slow = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. classifier = classify(features_train, labels_train) print classifier.score(features_test, labels_test) ### draw the decision boundary with the text points overlaid prettyPicture(classifier, features_test, labels_test) # output_image("test.png", "png", open("test.png", "rb").read())
features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] bumpy_fast = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. clf = classify(features_train, labels_train, features_test, labels_test) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read())
features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] bumpy_fast = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. classifier = classify(features_train, labels_train) print classifier.score(features_test, labels_test) ### draw the decision boundary with the text points overlaid prettyPicture(classifier, features_test, labels_test) # output_image("test.png", "png", open("test.png", "rb").read())
features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 0 ] grade_slow = [ features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] bumpy_slow = [ features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii] == 1 ] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. # Instantiate, then get or return the Naive Bayes Gaussain TRAINED classifier clf = classify('NB', features_train, labels_train) # print("\ttype(clf) - {}\n".format(type(clf))) # class 'sklearn.naive_bayes.GaussianNB # Instantiate, then get or return the SupportVectorMachines TRAINED classifier SVMclf = classify('SVM', features_train, labels_train) # use the trained classifier to do the predictions # generate the ** pred ** numpy.ndarray (n dimensional array) # how to time something, performance timing t0 = time.time() pred = clf.predict(features_test) # used for accuracy methods 2 and 3 below print("Naive Bayes, predict timing: - {}".format(round(time.time() - t0, 3))) # print("\tpred is {}\n".format(pred)) # print("\ttype(pred) - {}\n".format(type(pred))) # numpy.ndarray # generate Support Vector Machines - SVM - predictor
from ..bin.prep_terrain_data import makeTerrainData from ..bin.class_vis import prettyPicture, output_image from ClassifyNB import classify #from classify import NBAccuracy import matplotlib.pyplot as plt import numpy as np import pylab as pl #from pprintpp import pprint #pprint(makeTerrainData()) features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==0] bumpy_fast = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==0] grade_slow = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==1] bumpy_slow = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==1] clf, accuracy = classify(features_train, labels_train, features_test, labels_test) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read()) print "Classification accuracy = ", accuracy
from prep_terrain_data import makeTerrainData from class_viz import prettyPicture, output_image from ClassifyNB import classify import numpy as np import pylab as pl features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==0] bumpy_fast = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==0] grade_slow = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==1] bumpy_slow = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==1] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. clf = classify(features_train, labels_train,features_test, labels_test) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) #output_image("test.png", "png", open("test.png", "rb").read())
def classify(features_train, labels_train): ### import the sklearn module for GaussianNB ### create classifier ### fit the classifier on the training features and labels ### return the fit classifier ### your code goes here! import numpy as np import pylab as pl features_train, labels_train, features_test, labels_test = makeTerrainData() ### the training data (features_train, labels_train) have both "fast" and "slow" points mixed ### in together--separate them so we can give them different colors in the scatterplot, ### and visually identify them grade_fast = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==0] bumpy_fast = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==0] grade_slow = [features_train[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]==1] bumpy_slow = [features_train[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]==1] # You will need to complete this function imported from the ClassifyNB script. # Be sure to change to that code tab to complete this quiz. clf = classify(features_train, labels_train) ### draw the decision boundary with the text points overlaid prettyPicture(clf, features_test, labels_test) output_image("test.png", "png", open("test.png", "rb").read())