tfidfvectorizer tfidftransformer spanish sklearn hashingvectorizer countvectorizer python scikit-learn classification stop-words text-classification

python - spanish - tfidftransformer



agregar palabras a la lista de stop_words en TfidfVectorizer en sklearn (2)

Deseo agregar algunas palabras más a stop_words en TfidfVectorizer. Seguí la solución en Agregar palabras a la lista de detención de CountVectorizer de scikit-learn . Mi lista de palabras para detener ahora contiene las palabras de alto "inglés" y las palabras de finalización que especifiqué. Pero todavía TfidfVectorizer no acepta mi lista de palabras de finalización y todavía puedo ver esas palabras en mi lista de características. A continuación está mi código

from sklearn.feature_extraction import text my_stop_words = text.ENGLISH_STOP_WORDS.union(my_words) vectorizer = TfidfVectorizer(analyzer=u''word'',max_df=0.95,lowercase=True,stop_words=set(my_stop_words),max_features=15000) X= vectorizer.fit_transform(text)

También he intentado establecer stop_words en TfidfVectorizer como stop_words = my_stop_words. Pero aún así no funciona. Por favor ayuda.


Esto se responde aquí: https://.com/a/24386751/732396

Aunque sklearn.feature_extraction.text.ENGLISH_STOP_WORDS es un sklearn.feature_extraction.text.ENGLISH_STOP_WORDS frozenset, puede hacer una copia y agregar sus propias palabras, luego pasar esa variable al argumento stop_words como una lista.


Aquí hay un ejemplo:

from sklearn.feature_extraction import text from sklearn.feature_extraction.text import TfidfVectorizer my_stop_words = text.ENGLISH_STOP_WORDS.union(["book"]) vectorizer = TfidfVectorizer(ngram_range=(1,1), stop_words=my_stop_words) X = vectorizer.fit_transform(["this is an apple.","this is a book."]) idf_values = dict(zip(vectorizer.get_feature_names(), vectorizer.idf_)) # printing the tfidf vectors print(X) # printing the vocabulary print(vectorizer.vocabulary_)

En este ejemplo, creé los vectores tfidf para dos documentos de muestra:

"This is a green apple." "This is a machine learning book."

Por defecto, this , is , a y an están todos en la lista de ENGLISH_STOP_WORDS . Y, también agregué un book a la lista de palabras finales. Este es el resultado:

(0, 1) 0.707106781187 (0, 0) 0.707106781187 (1, 3) 0.707106781187 (1, 2) 0.707106781187 {''green'': 1, ''machine'': 3, ''learning'': 2, ''apple'': 0}

Como podemos ver, la palabra book también se elimina de la lista de características porque la enumeró como palabra de finalización. Como resultado, tfidfvectorizer aceptó la palabra agregada manualmente como palabra de reposo e ignoró la palabra en el momento de crear los vectores.