朴素贝叶斯的KFold交叉验证

2024-10-17 08:23:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用sklearn对我的naivebayes分类器进行k-fold验证

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,我想使用我自己的naivebayes classifier,但我不太确定fit方法在probas=成本加运费(列车[traincv],目标[traincv])。预测概率(列车[testcv])


Tags: theintargetfornptrainarrayresults