def draw_svm_db(kernel="linear", gamma=1000, C=1.0): #Train X_train, X_test, y_train, y_test = mytools.make_fake_data() clf = SVC(kernel=kernel, gamma=gamma, C=C) start = time.time() clf.fit(X_train, y_train) train_time = time.time() - start #Predict start = time.time() preds = clf.predict(X_test) test_time = time.time() - start #Score accuracy = accuracy_score(y_test, preds) print "" print "Kernel: ", kernel print "C: ", C, " , gamma: ", gamma print "Accuracy: ", accuracy print "Training time: ", round(train_time,3) print "Test time: ", round(test_time,3) #Visualization mytools.prettyPicture(clf, X_test, y_test) plt.show()
plt.show() ##Initialize GaussianNB classifier clf = GaussianNB() clf.fit(X_train, y_train) ##Test and calculate %accuracy #preds = clf.predict(X_test) #n_test = len(X_test) #accs = [preds[i] == y_test[i] for i in range(0,n_test)] #print sum(accs) / float(len(accs)) # ##Alternatively, use built-in function #print clf.score(X_test, y_test) #Plot predictions: black if correctly labeled, red otherwise #plt.figure(0) #plt.xlabel("grade"); plt.ylabel("bumpiness") #for i, point in enumerate(X_test): # color = "black" # if not accs[i]: #wrong classification # color = "red" # plt.scatter(point[0], point[1], marker = u'x', c=color) #plt.show() #viz mytools.prettyPicture(clf, X_test, y_test)
### labels_train and labels_test are the corresponding item labels features_train, features_test, labels_train, labels_test = preprocess() kernel = "linear" clf = SVC(kernel=kernel) start = time() clf.fit(features_train, features_test) train_time = time() - start start = time() preds = clf.predict(features_test) test_time = time() - start acc = accuracy_score(labels_test, preds) print "train time: ", train_time print "test time: ", test_time print "accuracy: ", acc mytools.prettyPicture(clf, features_test, labels_test) ######################################################### ### your code goes here ### #########################################################