简体   繁体   English

Python中的示例K折交叉验证

[英]Sample k-fold cross validation in Python

I want to test k-fold (k=3) cross-validation in Python 我想在Python中测试k倍(k = 3)交叉验证

I got this code from the web 我从网上得到了这段代码

import nltk # needed for Naive-Bayes
import numpy as np
from sklearn.model_selection import KFold

# data is an array with our already pre-processed dataset examples
kf = KFold(n_splits=3)
sum = 0
for train, test in kf.split(data):
    train_data = np.array(data)[train]
    test_data = np.array(data)[test]
    classifier = nltk.NaiveBayesClassifier.train(train_data)
    sum += nltk.classify.accuracy(classifier, test_data)
average = sum/3

and add: 并添加:

data = [10, 20, 30, 40, 50]

error result: 错误结果:

Traceback (most recent call last):
  File "/Users/david/PycharmProjects/iranian-01/pandas_test.py", line 12, in <module>
classifier = nltk.NaiveBayesClassifier.train(train_data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/classify/naivebayes.py", line 194, in train
for featureset, label in labeled_featuresets:
TypeError: 'numpy.int64' object is not iterable

please help me to solve this 请帮我解决这个问题

You should to fit your data before training and testing. 在培训和测试之前,您应该先调整数据。 ie you are going to make a 3-fold cross validation to these data 即您将对这些数据进行三重交叉验证

data = [10, 20, 30, 40, 50]

so, the result will be a floating point number when the compiler splitting it. 因此,结果将是编译器拆分时的浮点数。 I advice you to work with numpy rather than a Python purely arrays, to be able to use the pre-defined functions in this library. 我建议您使用numpy而不是Python纯数组,以便能够使用此库中的预定义函数。

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

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