from data import make_balanced_dataset, make_unbalanced_dataset from sklearn.naive_bayes import GaussianNB from sklearn.metrics import confusion_matrix, accuracy_score # (Question 1) # Put your funtions here # ... np.random.seed(0) if __name__ == "__main__": cnf = np.zeros((2, 2)) a = 0 st = 0 for k in range(5): ''' choose either unbalanced either balanced and uncomment another''' b = make_unbalanced_dataset(3000) #b=make_balanced_dataset(3000) Xtr = np.array(b[0][0:1000, :]) ytr = b[1][0:1000] Xte = np.array(b[0][1000:, :]) yte = b[1][1000:] c = GaussianNB() t = GaussianNB.fit(c, Xtr, ytr) if k == 0: plot_boundary(fname="Naive_Bias_Depth_%s.png" % (k), fitted_estimator=t, X=Xte, y=yte) pr = t.predict(Xte) cnf += confusion_matrix(yte, pr)
def makeDataset(FIXED_NB_INDEX): x, y = make_unbalanced_dataset(3000, FIXED_NB[FIXED_NB_INDEX]) trainSample = (x[:1000, :], y[:1000]) testSample = (x[1000:, :], y[1000:]) return (trainSample, testSample)
accuracies.append(np.mean(cross_val_score(knn,x,y,cv=10,n_jobs = -1))) optiNeighbors, optiAcc = max(enumerate(accuracies), key=operator.itemgetter(1)) optiNeighbors +=1 #Ploting plt.figure() plt.plot(nbNeighbor, accuracies,'b') plt.plot(optiNeighbors, optiAcc,'r.') plt.xlabel("Number of neighbors") plt.ylabel("Accuracy") plt.savefig("1OFCVAccByNeighbors1.svg") plt.figure() plt.plot(nbNeighbor[10:], accuracies[10:],'b') plt.plot(optiNeighbors, optiAcc,'r.') plt.xlabel("Number of neighbors") plt.ylabel("Accuracy") plt.savefig("1OFCVAccByNeighbors2.svg") return optiNeighbors, optiAcc def plotq22(): return if __name__ == "__main__": x, y = make_unbalanced_dataset(3000, FIXED_NB) q21(x,y) optiNeighbors, optiAcc = q22(x,y) print("The Optimal number of Neighbors, after using the 10 cross fold-validation, is {} with the accuracy of {}".format(optiNeighbors,optiAcc))