1
votes

This is my first time using Scikit, and apologies if the question is stupid. I'm trying to implement a naive bayes classifier on UCI's mushroom dataset to test the results against my own NB classifier coded from scratch.

The dataset is categorical and each feature has more than 2 possible attributes so I used a multinomial NB instead of a Gaussian or Bernouilli NB.

However, I keep getting the following error ValueError: could not convert string to float: 'l' , and am not sure what to do. Shouldn't a multinomial NB be able to take string data?

Example line of data - 0th column is the class (p for poisonous and e for edible) and the remaining 22 columns are the features.
p,x,s,n,t,p,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,s,u

# based off UCI's mushroom dataset http://archive.ics.uci.edu/ml/datasets/Mushroom

df = pd.DataFrame(data)
msk = np.random.rand(df.shape[0]) <= training_percent
train = data[msk]
test =  data[~msk] 

clf = MultinomialNB()
clf.fit(train.iloc[:, 1:], train.iloc[:, 0])
1

1 Answers

2
votes

In short, no it shouldn't be able to take a string as an input. You will have to do some preprocessing, but luckily sklearn is really good for that too.

from sklearn import preprocessing
enc = preprocessing.LabelEncoder()
mushrooms = ['p','x','s','n','t','p','f','c','n','k','e','e','s','s','w','w','p','w','o']
enc.fit(mushrooms)
classes = enc.transform(mushrooms)
print classes
print enc.inverse_transform(classes)

Which outputs

[ 6 10  7  4  8  6  2  0  4  3  1  1  7  7  9  9  6  9  5]
['p' 'x' 's' 'n' 't' 'p' 'f' 'c' 'n' 'k' 'e' 'e' 's' 's' 'w' 'w' 'p' 'w''o']

Then train on the transformed data

clf.fit(enc.tranform(train.iloc[:, 1:], train.iloc[:, 0]))

Remember: The LabelEncoder will only transform strings it has been trained on, so ensure you preprocess your data properly.