Ошибка имени: имя «text_column» не определено, синтаксическая ошибка?

#python #syntax #xgboost #nameerror

Вопрос:

Я пытаюсь запустить эту модель XG Boost для классификации текста, однако я сталкиваюсь с проблемой определения. Вот мой код:

 def preprocess(text_column):

    """
    Function:    This function aims to remove links, special 
                 characters, symbols, stop words and thereafter 
                 lemmatise each word in the sentence to transform 
                 the dataset into something more usable for a 
                 machine learning model.
    Input:       A text column
    Returns:     A text column (but transformed)
    """
new_review = [] 
for review in text_column:
    text = re.sub("@S |https?:S|[^A-Za-z0-9] ",'',str(review).lower()).strip()
    text = [wnl.lemmatize(i) for i in text.split ('') if i not in stop_words]
    new_review.append(''.join(text))
return new_review

# actually transforming the datasets
train['review'] = preprocess(train['review'])
test['review'] = preprocess(test['review'])

 

Ошибка:

 NameError                                 Traceback (most recent call last)
<ipython-input-43-c0c3b2a57d42> in <module>()
      1 new_review = []
----> 2 for review in text_column:
      3     text = re.sub("@S |https?:S|[^A-Za-z0-9] ",'',str(review).lower()).strip()
      4     text = [wnl.lemmatize(i) for i in text.split ('') if i not in stop_words]
      5     new_review.append(''.join(text))

NameError: name 'text_column' is not defined

 

Пожалуйста, дайте мне знать, если я смогу что-нибудь сделать, чтобы это исправить. Спасибо.

Комментарии:

1. Код должен быть с отступом, чтобы сделать его частью функции, в настоящее время это не так.

Ответ №1:

Вам нужно сделать отступ в теле preprocess .

 def preprocess(text_column):

    """
    Function:    This function aims to remove links, special 
                 characters, symbols, stop words and thereafter 
                 lemmatise each word in the sentence to transform 
                 the dataset into something more usable for a 
                 machine learning model.
    Input:       A text column
    Returns:     A text column (but transformed)
    """
    new_review = [] 
    for review in text_column:
        text = re.sub("@S |https?:S|[^A-Za-z0-9] ",'',str(review).lower()).strip()
        text = [wnl.lemmatize(i) for i in text.split ('') if i not in stop_words]
        new_review.append(''.join(text))
    return new_review