Тфидф пустой словарный запас; возможно, документы содержат только стоп-слова - PullRequest
0 голосов
/ 06 ноября 2018

В настоящее время я работаю над проектом и использую Tfidf для преобразования данных X_train, которые содержат текстовые данные. Когда я использую count_vectorizer.fit_transform(X_train), я получаю эту ошибку:

Traceback (most recent call last):
  File "train.py", line 100, in <module>
    counts = count_vectorizer.fit_transform(X_train)
  File "/home/vishalthadari/Documents/Seperation 1/API's/Confirmation API/python 3 /env/lib/python3.6/site-packages/sklearn/feature_extraction/text.py", line 869, in fit_transform
    self.fixed_vocabulary_)
  File "/home/vishalthadari/Documents/Seperation 1/API's/Confirmation API/python 3 /env/lib/python3.6/site-packages/sklearn/feature_extraction/text.py", line 811, in _count_vocab
    raise ValueError("empty vocabulary; perhaps the documents only"
ValueError: empty vocabulary; perhaps the documents only contain stop words

Я прочитал другие вопросы о стековом потоке, подобные этому Ссылка Но я не могу понять, как разделить данные X_train

Вот мой файл Train.py

import os
import numpy
from pandas import DataFrame
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer

NEWLINE = '\n'

TRAVEL = 'Travel'
OTHER = 'Other'

SOURCES = [
    ('data/travel',    TRAVEL),
    ('data/other',    OTHER),
]

SKIP_FILES = {'cmds', '.DS_Store'}

SEED = 0 # for reproducibility

def read_files(path):
    #Reads all files in all directories mentioned in SOURCES
    for root, dir_names, file_names in os.walk(path):
        for path in dir_names:
            read_files(os.path.join(root, path))
        for file_name in file_names:
            if file_name not in SKIP_FILES:
                file_path = os.path.join(root, file_name)
                if os.path.isfile(file_path):
                    past_header, lines = False, []
                    f = open(file_path, encoding="latin-1")
                    for line in f:
                        if past_header:
                            lines.append(line)
                        elif line == NEWLINE:
                            past_header = True
                    f.close()
                    content = NEWLINE.join(lines)
                    yield file_path, content

def build_data_frame(path, classification):
    #Returns a data frame of all the files read using read_files()
  data_frame = DataFrame({'text': [], 'class': []})
  for file_name, text in read_files(path):
    data_frame = data_frame.append(
        DataFrame({'text': [text], 'class': [classification]}, index=[file_name]))
  return data_frame

data = DataFrame({'text': [], 'class': []})
for path, classification in SOURCES:
    data = data.append(build_data_frame(path, classification))

data = data.reindex(numpy.random.permutation(data.index))

#Training data
X_train = numpy.asarray(data['text'])
count_vectorizer = CountVectorizer()
counts = count_vectorizer.fit_transform(X_train)

Я следовал всем решениям, но все еще не решил проблему. Если я делаю неправильную проверку для преобразования данных, если я делаю правильно, то почему я получаю эту ошибку.

Заранее спасибо

...