python - 了解机器学习、NLP : Text Classification using scikit-learn, python 和 NLTK

标签 python machine-learning scikit-learn

我正在尝试使用本文https://towardsdatascience.com/machine-learning-nlp-text-classification-using-scikit-learn-python-and-nltk-c52b92a7c73a中给出的示例除了使用本教程使用的 20newsgroups 数据集之外,我尝试使用自己的数据,该数据由/home/pi/train/中的文本文件组成,其中 train 下的每个子目录都是一个类似/home/pi 的标签/train/足球//home/pi/train/篮球/。我试图通过将其放入/home/pi/test/FOOTBALL/或/home/pi/test/BASKETBALL/并运行程序来一次测试一个文档。

# -*- coding: utf-8 -*-
import sklearn
from pprint import pprint
from sklearn.datasets import load_files
docs_to_train = sklearn.datasets.load_files("/home/pi/train/", description=None, categories=None, load_content=True, shuffle=True, encoding=None, decode_error='strict', random_state=0)
pprint(list(docs_to_train.target_names))
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(docs_to_train.data)
X_train_counts.shape
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_train_tfidf.shape
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
text_clf = Pipeline([('vect', CountVectorizer()), 
('tfidf', TfidfTransformer()),
('clf', MultinomialNB()),])
text_clf = text_clf.fit(docs_to_train.data, docs_to_train.target)
import numpy as np
docs_to_test = sklearn.datasets.load_files("/home/pi/test/", description=None, categories=None, load_content=True, shuffle=True, encoding=None, decode_error='strict', random_state=0)
predicted = text_clf.predict(docs_to_test.data)
np.mean(predicted == docs_to_test.target)
pprint(np.mean(predicted == docs_to_test.target))

如果我将足球文本文档放入/home/pi/test/FOOTBALL/文件夹并运行程序,我得到:

['FOOTBALL', 'BASKETBALL']
1.0

如果将有关足球的相同文档移动到/home/pi/test/BASKETBALL/文件夹中并运行我得到的程序:

['FOOTBALL', 'BASKETBALL']
0.0

这是 np.mean 应该如何工作的吗?有谁知道它想告诉我什么?

最佳答案

阅读 sklearn's load_files 上的文档,也许问题出在调用 X_train_counts = count_vect.fit_transform(docs_to_train.data) 中。您可能需要探索 docs_to_train.data 对象的结构来评估如何访问底层模块数据。不幸的是,这些文档对于数据的结构并没有多大帮助:

Dictionary-like object, the interesting attributes are: either data, the raw text data to learn, or ‘filenames’, the files holding it, ‘target’, the classification labels (integer index), ‘target_names’, the meaning of the labels, and ‘DESCR’, the full description of the dataset.

也可能是这样的情况 CountVectorizer() is expecting a single filepath or txt object ,而不是一个充满许多子数据类型的数据持有者。

关于python - 了解机器学习、NLP : Text Classification using scikit-learn, python 和 NLTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495693/

相关文章:

python-3.x - 使用自己的图像重新训练对象检测模型( tensorflow )

python - 分层KFold : IndexError: too many indices for array

python - 类型错误 : __init__() takes at least 3 arguments (2 given) when subclassing Model class

python - 如何通过多个条件选择某些列?

python - 在机器学习和深度学习项目中进行分析之前,我是否应该重新组合训练集和测试集?

machine-learning - 谷歌预测 API 是如何工作的

python - 将分类列转换为特定整数

python - 列 (0,1,3) 具有混合类型。在导入时指定 dtype 选项或设置 low_memory=False。导入 csv 文件时

python - 如何正确绘制训练集和验证集的损失曲线?

python - sklearn中的预定义Split函数