def deserialize_gaussian_nb(model_dict): model = GaussianNB(model_dict['params']) model.classes_ = np.array(model_dict['classes_']) model.class_count_ = np.array(model_dict['class_count_']) model.class_prior_ = np.array(model_dict['class_prior_']) model.theta_ = np.array(model_dict['theta_']) model.sigma_ = np.array(model_dict['sigma_']) model.epsilon_ = model_dict['epsilon_'] return model
def NB_predict(): X = json.loads(request.form['X']) params = json.loads(request.form['params']) clf = GaussianNB() clf.class_prior_ = np.array(params['class_prior']) clf.class_count_ = np.array(params['class_count']) clf.theta_ = np.array(params['theta']) clf.sigma_ = np.array(params['sigma']) clf.classes_ = np.array(params['classes']) y = clf.predict(X) return jsonify(pred=y.tolist())
def naiveBayesClassifier(self,X_train,y_train): """Method to train a regularized logistic regression classifier Parameters ---------- X_train: Array shape [n_samples,n_features] for training the model with features y_train: Array shape[n_samples] for training the model with features of that target Returns --------- model: The trained Logistic Regression model.""" meansArray, varianceArray = self.NBMeanAndVariance(X_train,y_train) model = GaussianNB() #fit the model with training data model.fit(X_train,y_train) model.theta_ = meansArray model.sigma_ = varianceArray return model
# In[554]: #def P9(): ### STUDENT END ### model = GaussianNB() model.fit(mini_train_data, mini_train_labels) expected = dev_labels prediction = model.predict(dev_data) print "Initial Model" print classification_report(expected, prediction) for i in range(1, 100, 5): model = GaussianNB() model.fit(mini_train_data, mini_train_labels) model.sigma_ = model.sigma_ / (i) expected = dev_labels prediction = model.predict(dev_data) acc = accuracy_score(expected, prediction) plt.scatter(i, acc, color="blue") plt.title("Accuracy as Sigma Increases") plt.ylabel("Accuracy"), plt.xlabel("Sigma") plt.show() ### STUDENT END ### #gnb = P9() # ANSWER: ### As shown in the plot above, modifying Sigma can have a dramatic impact on the accuracy outcome. # (10) Because Naive Bayes is a generative model, we can use the trained model to generate digits. Train a BernoulliNB model and then generate a 10x20 grid with 20 examples of each digit. Because you're using a Bernoulli model, each pixel output will be either 0 or 1. How do the generated digits compare to the training digits?