繁体   English   中英

朴素贝叶斯的K折交叉验证

[英]K-Fold Cross Validation for Naive Bayes

我正在尝试使用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,我想使用我自己的朴素贝叶斯分类器,但是我不太确定fit方法在这行上的作用probas = cfr.fit(train [traincv],target [traincv])。predict_proba (火车[testcv])

似乎您只需要更改cfr,例如:

cfr = sklearn.naive_bayes.GaussianNB()

它应该工作相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM