0
$\begingroup$

I am trying to visualize the Naive Bayes descision boundry where I am trainning a Naive Bayes on Breast Cancer Wisconsin (Original) Data Set . Here is the code :

from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, Model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Classifier (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

But I am getting this error :

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-60b675710c2f> in <module>
      3 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
      4                      np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
----> 5 plt.contourf(X1, X2, Model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
      6              alpha = 0.75, cmap = ListedColormap(('red', 'green')))
      7 plt.xlim(X1.min(), X1.max())

~\anaconda3\lib\site-packages\sklearn\naive_bayes.py in predict(self, X)
     76         check_is_fitted(self)
     77         X = self._check_X(X)
---> 78         jll = self._joint_log_likelihood(X)
     79         return self.classes_[np.argmax(jll, axis=1)]
     80 

~\anaconda3\lib\site-packages\sklearn\naive_bayes.py in _joint_log_likelihood(self, X)
    454             jointi = np.log(self.class_prior_[i])
    455             n_ij = - 0.5 * np.sum(np.log(2. * np.pi * self.sigma_[i, :]))
--> 456             n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) /
    457                                  (self.sigma_[i, :]), 1)
    458             joint_log_likelihood.append(jointi + n_ij)

ValueError: operands could not be broadcast together with shapes (257839,2) (9,) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.