Реализация n-граммы в коде Python для многоклассовой классификации текста - PullRequest
1 голос
/ 07 апреля 2019

Я новичок в Python и работаю над многоклассовой текстовой классификацией договорных документов строительной отрасли.Я сталкиваюсь с проблемами в реализации n-граммов в моем коде, который я создал, получая помощь из разных онлайн-источников.Я хочу реализовать в своем коде униграмму, биграмму и триграмм.Любая помощь в этом отношении будет высоко оценена.

Я пробовал биграммы и триграммы в моей части кода Tfidf, но она работает.

    df = pd.read_csv('projectdataayes.csv')
    df = df[pd.notnull(df['types'])]
    my_types = ['Requirement','Non-Requirement']

    #converting to lower case
    df['description'] = df.description.map(lambda x: x.lower()) 

    #Removing the punctuation
    df['description'] = df.description.str.replace('[^\w\s]', '')  

    #splitting the word into tokens
    df['description'] = df['description'].apply(tokenize.word_tokenize) 

    #stemming
    stemmer = PorterStemmer()
    df['description'] = df['description'].apply(lambda x: [stemmer.stem(y) for y in x]) 

    print(df[:10])

    ## This converts the list of words into space-separated strings
    df['description'] = df['description'].apply(lambda x: ' '.join(x))
    count_vect = CountVectorizer()  
    counts = count_vect.fit_transform(df['description']) 


    X_train, X_test, y_train, y_test = train_test_split(counts, df['types'], test_size=0.3, random_state=39) 

    tfidf_vect_ngram = TfidfVectorizer(analyzer='word', 
    token_pattern=r'\w{1,}', ngram_range=(2,3), max_features=5000)
    tfidf_vect_ngram.fit(df['description'])
    X_train_Tfidf =  tfidf_vect_ngram.transform(X_train)
    X_test_Tfidf =  tfidf_vect_ngram.transform(X_test)

    model = MultinomialNB().fit(X_train, y_train)

Файл "C: \ Users \"fhassan \ anaconda3 \ lib \ site-packages \ sklearn \ feature_extraction \ text.py ", строка 328, в tokenize (предварительный процесс (self.decode (doc))), stop_words)

файл" C: \ Users "\ fhassan \ anaconda3 \ lib \ site-packages \ sklearn \ feature_extraction \ text.py ", строка 256, взамен лямбда x: strip_accents (x.lower ())

Файл" C: \ Users \ fhassan "\ anaconda3 \ lib \ site-packages \ scipy \ sparse \ base.py ", строка 686, в getattr повышение AttributeError (attr +" не найдено ")

AttributeError: нижнее значение не найдено

1 Ответ

0 голосов
/ 07 апреля 2019

Сначала вы подгоняете векторизатор к текстам:

tfidf_vect_ngram.fit(df['description']) 

И затем пытаетесь применить его к счетам:

counts = count_vect.fit_transform(df['description'])
X_train, X_test, y_train, y_test = train_test_split(counts, df['types'], test_size=0.3, random_state=39) 
tfidf_vect_ngram.transform(X_train)

Вам необходимо применять векторизатор к текстам, а не к счетам:

X_train, X_test, y_train, y_test = train_test_split(df['description'], df['types'], test_size=0.3, random_state=39)
tfidf_vect_ngram.transform(X_train)
...