from nb import NaiveBayes from sklearn import datasets from sklearn.model_selection import train_test_split import numpy as np def accuracy(pred, label): acc = np.sum(pred == label) / len(label) return acc X, y = datasets.make_classification(n_samples=1000, n_features=4, n_classes=2, random_state=123) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123) model = NaiveBayes() model.fit(X_train, y_train) predict = model.predict(X_test) acc = accuracy(predict, y_test) print(acc)
import numpy as np import scipy.io as sio from sklearn.metrics import zero_one_loss #change this to where mat_dict = sio.loadmat('XwindowsDocData.mat') Xtrain = mat_dict['xtrain'].toarray() Xtest = mat_dict['xtest'].toarray() ytrain = mat_dict['ytrain'].flatten() ytest = mat_dict['ytest'].flatten() nb = NaiveBayes() pi, theta = nb.fit(Xtrain, ytrain) ypred_train = nb.predict(Xtrain) ypred_test = nb.predict(Xtest) print(ypred_train) print(ypred_test) #because the classes are 1,2 ypred_train = 1 + ypred_train.argmax(axis=1) ypred_test = 1 + ypred_test.argmax(axis=1) print(ypred_train[-20:]) print(ytrain[-20:]) print(ypred_test[-20:]) print(ytest[-20:])
import numpy as np from sklearn.model_selection import train_test_split from sklearn import datasets import matplotlib.pyplot as plt from nb import NaiveBayes def accuracy(y_true, y_pred): accuracy = np.sum(y_true == y_pred) / len(y_true) return accuracy X, y = datasets.make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=123) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123) nb = NaiveBayes() nb.fit(X_train, y_train) predictions = nb.predict(X_test) # print(type(y_train)) print("Naive Bayes classification accuracy", accuracy(y_test, predictions))