Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的十重分类和lib-svm计算精度_Python_Machine Learning_Nlp_Scikit Learn_Libsvm - Fatal编程技术网

python中的十重分类和lib-svm计算精度

python中的十重分类和lib-svm计算精度,python,machine-learning,nlp,scikit-learn,libsvm,Python,Machine Learning,Nlp,Scikit Learn,Libsvm,我有一个术语文档矩阵和相应的标签矩阵,我必须将数据集分成10个部分,并使用任意7个部分来训练libsvm分类器,并对其余3个部分进行测试。 对于所有可能的情况,如10C7,我必须这样做。 下面是使用SVM进行训练和测试的代码,我无法理解如何对所有情况进行分类和迭代 m = svm_train(labels[0:2000], rows_1[0:2000], '-c '+str(k)+' -g '+str(g)) p_label, p_acc, p_val = svm_predict(labels

我有一个术语文档矩阵和相应的标签矩阵,我必须将数据集分成10个部分,并使用任意7个部分来训练libsvm分类器,并对其余3个部分进行测试。 对于所有可能的情况,如10C7,我必须这样做。 下面是使用SVM进行训练和测试的代码,我无法理解如何对所有情况进行分类和迭代

m = svm_train(labels[0:2000], rows_1[0:2000], '-c '+str(k)+' -g '+str(g))

p_label, p_acc, p_val = svm_predict(labels[2000:0], rows_1[2000:0], m)
acc.append(p_acc)
其中“labels”是标签数组,“rows_1”是术语文档矩阵的行。
我是新手,请帮帮我

您必须对数据进行洗牌,并为训练和测试创建索引。例如,如果您有2000个培训示例,并且希望使用10倍,那么您将有:

fold1
  test[0:200]
  train[200:2000]
fold2
  test[200:400]
  train[0:200, 400:2000]
etc
以下是Python中的示例代码:

import numpy as np
indices = np.random.permutation(2000)  # create a list of 2000 unique numbers in random order
n_folds = 10
fold_step = int(2000 / n_folds)
acc = []
for fold in range(0, 2000, fold_step):
    test_labels = [labels[i] for i in indices[fold:fold+fold_step]]
    train_labels = [l for l in labels if l not in test_labels]
    test_rows = [rows_1[i] for i in indices[fold:fold+fold_step]]
    train_rows = [r for r in rows_1 if r not in test_rows]

    m = svm_train(train_labels, train_rows, '-c '+str(k)+' -g '+str(g))
    p_label, p_acc, p_val = svm_predict(test_labels, test_rows, m)
    acc.append(p_acc)

print("Accuracy: {}%".format(np.mean(acc)))

你必须洗牌你的数据,为训练和测试创建索引。例如,如果您有2000个培训示例,并且希望使用10倍,那么您将有:

fold1
  test[0:200]
  train[200:2000]
fold2
  test[200:400]
  train[0:200, 400:2000]
etc
以下是Python中的示例代码:

import numpy as np
indices = np.random.permutation(2000)  # create a list of 2000 unique numbers in random order
n_folds = 10
fold_step = int(2000 / n_folds)
acc = []
for fold in range(0, 2000, fold_step):
    test_labels = [labels[i] for i in indices[fold:fold+fold_step]]
    train_labels = [l for l in labels if l not in test_labels]
    test_rows = [rows_1[i] for i in indices[fold:fold+fold_step]]
    train_rows = [r for r in rows_1 if r not in test_rows]

    m = svm_train(train_labels, train_rows, '-c '+str(k)+' -g '+str(g))
    p_label, p_acc, p_val = svm_predict(test_labels, test_rows, m)
    acc.append(p_acc)

print("Accuracy: {}%".format(np.mean(acc)))

你必须洗牌你的数据,为训练和测试创建索引。例如,如果您有2000个培训示例,并且希望使用10倍,那么您将有:

fold1
  test[0:200]
  train[200:2000]
fold2
  test[200:400]
  train[0:200, 400:2000]
etc
以下是Python中的示例代码:

import numpy as np
indices = np.random.permutation(2000)  # create a list of 2000 unique numbers in random order
n_folds = 10
fold_step = int(2000 / n_folds)
acc = []
for fold in range(0, 2000, fold_step):
    test_labels = [labels[i] for i in indices[fold:fold+fold_step]]
    train_labels = [l for l in labels if l not in test_labels]
    test_rows = [rows_1[i] for i in indices[fold:fold+fold_step]]
    train_rows = [r for r in rows_1 if r not in test_rows]

    m = svm_train(train_labels, train_rows, '-c '+str(k)+' -g '+str(g))
    p_label, p_acc, p_val = svm_predict(test_labels, test_rows, m)
    acc.append(p_acc)

print("Accuracy: {}%".format(np.mean(acc)))

你必须洗牌你的数据,为训练和测试创建索引。例如,如果您有2000个培训示例,并且希望使用10倍,那么您将有:

fold1
  test[0:200]
  train[200:2000]
fold2
  test[200:400]
  train[0:200, 400:2000]
etc
以下是Python中的示例代码:

import numpy as np
indices = np.random.permutation(2000)  # create a list of 2000 unique numbers in random order
n_folds = 10
fold_step = int(2000 / n_folds)
acc = []
for fold in range(0, 2000, fold_step):
    test_labels = [labels[i] for i in indices[fold:fold+fold_step]]
    train_labels = [l for l in labels if l not in test_labels]
    test_rows = [rows_1[i] for i in indices[fold:fold+fold_step]]
    train_rows = [r for r in rows_1 if r not in test_rows]

    m = svm_train(train_labels, train_rows, '-c '+str(k)+' -g '+str(g))
    p_label, p_acc, p_val = svm_predict(test_labels, test_rows, m)
    acc.append(p_acc)

print("Accuracy: {}%".format(np.mean(acc)))

为什么7用于培训,3用于测试?当您标记此scikit学习时,交叉验证的标准版本将是
cross_val_score(SVM(),rows_1,labels,cv=10)
这将进行10倍分层交叉验证。您没有使用分层,这很可能会给您带来更多的噪声估计,因为不同的折叠将有不同的类别平衡。为什么7用于培训,3用于测试?当您标记此scikit学习时,交叉验证的标准版本将是
cross_val_score(SVM(),rows_1,labels,cv=10)
这将进行10倍的分层交叉验证。您没有使用分层,这很可能会给您带来更多的噪声估计,因为不同的折叠将有不同的类别平衡。为什么7用于培训,3用于测试?当您标记此scikit学习时,交叉验证的标准版本将是
cross_val_score(SVM(),rows_1,labels,cv=10)
这将进行10倍的分层交叉验证。您没有使用分层,这很可能会给您带来更多的噪声估计,因为不同的折叠将有不同的类别平衡。为什么7用于培训,3用于测试?当您标记此scikit学习时,交叉验证的标准版本将是
cross_val_score(SVM(),rows_1,labels,cv=10)
这将进行10倍的分层交叉验证。您没有使用分层,这很可能会给您带来更多的噪音估计,因为不同的折叠将有不同的阶级平衡。