Gaussian Naive Bayes in python/ipynb

40 Views Asked by At

I am using GaussianNB in sklearn library. But I have ValueError. How to fix it?

sns.set()
#Load the data set
iris = sns.load_dataset("iris")
iris = iris.rename(index = str, columns = {'sepal_length':'1_sepal_length','sepal_width':'2_sepal_width', 'petal_length':'3_petal_length', 'petal_width':'4_petal_width'})
#Plot the scatter of sepal length vs sepal width
sns.FacetGrid(iris, hue="species", height=7).map(plt.scatter, "1_sepal_length", "2_sepal_width", ).add_legend()
plt.title('Scatter plot')

#Setup X and y data
df1 = iris[["1_sepal_length", "2_sepal_width",'species']]
X_data = df1.iloc[:,0:2]
y_labels = df1.iloc[:,2].replace({'setosa':0,'versicolor':1,'virginica':2}).copy()
#Fit model
model_sk = GaussianNB(priors = None)
model_sk.fit(X_data, y_labels.values)

This is content of error:

Text(0.5, 1.0, 'Scatter plot')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[33], line 2
      1 model_sk = GaussianNB(priors = None)
----> 2 model_sk.fit(X_data, y_labels.values)

ValueError: Unknown label type: (array([0, 1, 2], dtype=object),)
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

I try to change model_sk.fit(X_data, y_labels.values) to model_sk.fit(X_data.values, y_labels.values), but it still error and I don't known how to fix it.

1

There are 1 best solutions below

0
Muhammed Yunus On

The elements of y_label are an object type, which is causing the error. Converting the elements to a numeric type like y_labels = y_labels.astype(int) will work.