0
我想为我的朴素贝叶斯分类器做一个k-折验证使用sklearnK-折交叉验证的朴素贝叶斯
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])