0
votes

I am trying to do a k-fold validation for my naive bayes classifier using sklearn

train = csv_io.read_data("../Data/train.csv")
target = np.array( [x[0] for x in train] )
train = np.array( [x[1:] for x in train] )

#In this case we'll use a random forest, but this could be any classifier
cfr = RandomForestClassifier(n_estimators=100)

#Simple K-Fold cross validation. 10 folds.
cv = cross_validation.KFold(len(train), k=10, indices=False)

#iterate through the training and test cross validation segments and
#run the classifier on each one, aggregating the results into a list
results = []
for traincv, testcv in cv:
    probas = cfr.fit(train[traincv], target[traincv]).predict_proba(train[testcv])
    results.append( myEvaluationFunc(target[testcv], [x[1] for x in probas]) )

#print out the mean of the cross-validated results
print "Results: " + str( np.array(results).mean() )

I found a code from this website, https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience/history/969. In the example the classifier is RandomForestClassifier, I would like to use my own naive bayes classifer, but I not very sure what the fit method do at this line probas = cfr.fit(train[traincv], target[traincv]).predict_proba(train[testcv])

1

1 Answers

0
votes

Seems like you just need to change cfr, for example:

cfr = sklearn.naive_bayes.GaussianNB()

and it should work the same.