【问题标题】:Hold out sample when loading data in Scikit-Learn with sklearn.datasets.load_files使用 sklearn.datasets.load_files 在 Scikit-Learn 中加载数据时保留样本
【发布时间】:2017-06-02 16:21:46
【问题描述】:

我正在用 Scikit-learn 试验一个简单的朴素贝叶斯。

基本上,我有两个文件夹,分别命名为 Cat A 和 Cat B,每个文件夹包含大约 1,500 个文本文件。

我正在加载这些文件,以便像这样训练分类器:

# Declare the categories
categories = ['CatA', 'CatB']

# Load the dataset
docs_to_train = sklearn.datasets.load_files("/Users/dh/Documents/Development/Python/Test_Data", description=None, categories=categories, load_content=True, shuffle=True, encoding='utf-8', decode_error='strict', random_state=0)

我正在使用短字符串测试分类器,例如

docs_new = ['This is test string 1.', 'This is test string 2.', 'This is test string 3.']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)

predicted = clf.predict(X_new_tfidf)

for doc, category in zip(docs_new, predicted):
    print('%r => %s' % (doc, docs_to_train.target_names[category]))

一切正常,但我真正想做的是在一些与训练数据非常相似的数据上测试分类器。理想情况下,我想在我使用训练分类器的数据中提取一个保留样本,然后与之交叉验证。

我想我可以将每个训练数据集中的 500 多个文档移动到不同的文件夹中,但我想知道是否有更好的方法来创建保留样本?

documentation 似乎没有提供这方面的指导。

完整代码如下。

import sklearn
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
import numpy as np
from sklearn import datasets
from pprint import pprint

# Declare the categories
categories = ['CatA', 'CatB']

# Load the dataset
docs_to_train = sklearn.datasets.load_files("/Users/dh/Documents/Development/Python/Test_Data", description=None, categories=categories, load_content=True, shuffle=True, encoding='utf-8', decode_error='strict', random_state=0)

print len(docs_to_train.data)

# Vectorise the dataset

count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(docs_to_train.data)

# Fit the estimator and transform the vector to tf-idf

tf_transformer = TfidfTransformer(use_idf=False).fit(X_train_counts)
X_train_tf = tf_transformer.transform(X_train_counts)
X_train_tf.shape

tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
print X_train_tfidf.shape

clf = MultinomialNB().fit(X_train_tfidf, docs_to_train.target)

docs_new = ['I am test string 1.', 'I am test string 2', 'I am test string 3']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)

predicted = clf.predict(X_new_tfidf)

for doc, category in zip(docs_new, predicted):
    print('%r => %s' % (doc, docs_to_train.target_names[category])) 

【问题讨论】:

标签: python scikit-learn text-classification


【解决方案1】:

您要查找的内容称为“训练-测试拆分”。

使用sklearn.model_selection.train_test_split:

from sklearn.model_selection import train_test_split

train_X, test_X, train_y, test_y = train_test_split(docs_to_train.data, 
                               docs_to_train.target,
                               test_size = 500)

【讨论】:

  • 太棒了!感谢您的帮助。
猜你喜欢
  • 2015-03-01
  • 2016-03-17
  • 2017-10-10
  • 2021-05-28
  • 2018-09-21
  • 1970-01-01
  • 2019-05-06
  • 2016-07-13
  • 2013-01-29
相关资源
最近更新 更多