Naive Bayes的K-Fold交叉验证

时间:2013-04-20 14:00:52

标签: python cross-validation

我正在尝试使用sklearn

对我的朴素贝叶斯分类器进行k倍验证
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() )

我从这个网站https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience/history/969找到了一个代码。在示例中,分类器是RandomForestClassifier,我想使用我自己的朴素贝叶斯分类器,但我不太确定拟合方法在此行做什么probas = cfr.fit(train [traincv],target [traincv])。predict_proba (列车[testcv])

1 个答案:

答案 0 :(得分:0)

好像你只需要改变cfr,例如:

cfr = sklearn.naive_bayes.GaussianNB()

它应该也一样。