Ejemplo n.º 1
0
def plotly(clf):
    try:
        prettyPicture(clf, features_test, labels_test)
    except NameError:
        pass
Ejemplo n.º 2
0
from time import time
from sklearn.metrics import accuracy_score
from tools.email_preprocess import preprocess
from choose_your_own.class_vis import prettyPicture, output_image
from sklearn import tree

### 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()

clf = tree.DecisionTreeClassifier(min_samples_split=40)

t0 = time()
clf = clf.fit(features_train, labels_train)
print "training time:", round(time() - t0, 3), "s"

t0 = time()
pred = clf.predict(features_test)
print "prediction time:", round(time() - t0, 3), "s"

acc_min_samples_split_40 = accuracy_score(pred, labels_test)  # type: float
print acc_min_samples_split_40

num_features = len(features_train[0])
print num_features

prettyPicture(clf, features_test, labels_test)
output_image("test.png", "png", open("test.png", "rb").read())
###############
Ejemplo n.º 3
0
    if labels_train[ii] == 1
]
bumpy_slow = [
    features_train[ii][1] for ii in range(0, len(features_train))
    if labels_train[ii] == 1
]

#### initial visualization
# plt.xlim(0.0, 1.0)
# plt.ylim(0.0, 1.0)
# plt.scatter(bumpy_fast, grade_fast, color = "b", label="fast")
# plt.scatter(grade_slow, bumpy_slow, color = "r", label="slow")
# plt.legend()
# plt.xlabel("bumpiness")
# plt.ylabel("grade")
# plt.show()
################################################################################

### your code here!  name your classifier object clf if you want the
### visualization code (prettyPicture) to show you the decision boundary

classifier = KNeighborsClassifier(n_neighbors=8, n_jobs=os.cpu_count())
classifier.fit(features_train, labels_train)

print("Accuracy:", classifier.score(features_test, labels_test))  # 0.944

try:
    prettyPicture(classifier, features_test, labels_test)
except NameError:
    pass