def main(): (training_features, training_lables, test_features, true_labels) = makeTerrainData() classifier = classify(training_features, training_lables) predicted_lables = classifier.predict(test_features) simple_plot(training_features, training_lables, ['r', 'g']) simple_plot(test_features, predicted_lables, ['y', 'b'], show=True) print(accuracy_score(predicted_lables, true_labels)) print(calc_accuracy(true_labels, predicted_lables))
def buyselltrainer(): feature, target = csv_to_numpy_train() from ClassifyDT import classify clf = classify() from sklearn.cross_validation import train_test_split feature_train, feature_test, target_train, target_test = train_test_split( feature, target, test_size=0.5, random_state=42) clf.fit(feature_train, target_train) pred = clf.predict(feature_test) from sklearn.metrics import accuracy_score accuracy = accuracy_score(target_test, pred) print(accuracy) return clf
### 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) accuracy = clf.score(features_test, labels_test) print("the accuracy is : ", accuracy) ### 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()) #this code does not work in python 3, dont know why #running it using the 2 line code below image = Image.open('test.png') image.show()
from prep_terrain_data import makeTerrainData from class_vis import prettyPicture, output_image #from ClassifyNB import classify from ClassifyDT 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) print('accuracy = ', clf.score(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())