Я хотел бы знать, как обучить SVM, предоставляя в качестве входных данных весь документ и одну метку для этого входного документа. Я пометил только слово за словом до сих пор. например, входной документ может содержать от 6 до 10 предложений, и весь документ должен быть помечен одним классом для обучения.
Предоставление единого тега всему документу с использованием SVM
Ответы (1)
Основной метод будет следующим:
- Создайте список ваших учебных документов и меток/классов.
- Токенизируйте свои учебные документы.
- Удалите стоп-слова из ваших документов.
- Создайте значения TF-IDF для ваших документов.
- Ограничьте ваши значения TF-IDF N наиболее распространенными, например. с N=1000.
- Обучите SVM на ограниченных данных TF-IDF и ваших метках.
Затем у вас есть классификатор, который может сопоставлять документы в форме TF-IDF с метками классов. Поэтому вы можете затем классифицировать свои тестовые документы после преобразования их в аналогичную форму TF-IDF.
Вот пример на Python с scikit для SVM, который классифицирует документы как либо о лисиках, либо о городах:
from sklearn import svm
from sklearn.feature_extraction.text import TfidfVectorizer
# Training examples (already tokenized, 6x fox and 6x city)
docs_train = [
"The fox jumped over the fence .",
"The fox sleeps under the tree .",
"A fox walks through the high grass .",
"Didn 't see a single fox today .",
"I saw a fox yesterday near the lake .",
"You might encounter foxes at the lake .",
"New York City is full of skyscrapers .",
"Los Angeles is a city on the west coast .",
"I 've been to Los Angeles before .",
"Let 's travel to Mexico City .",
"There are no skyscrapers in Washington .",
"Washington is a beautiful city ."
]
# Test examples (already tokenized, 2x fox and 2x city)
docs_test = [
"There 's a fox in the garden .",
"Did you see the fox next to the tree ?",
"What 's the shortest way to Los Alamos ?",
"Traffic in New York is a pain"
]
# Labels of training examples (6x fox and 6x city)
y_train = ["fox", "fox", "fox", "fox", "fox", "fox",
"city", "city", "city", "city", "city", "city"]
# Convert training and test examples to TFIDF
# The vectorizer also removes stopwords and converts the texts to lowercase.
vectorizer = TfidfVectorizer(max_df=1.0, max_features=10000,
min_df=0, stop_words='english')
vectorizer.fit(docs_train + docs_test)
X_train = vectorizer.transform(docs_train)
X_test = vectorizer.transform(docs_test)
# Train an SVM on TFIDF data of the training documents
clf = svm.SVC()
clf.fit(X_train, y_train)
# Test the SVM on TFIDF data of the test documents
print clf.predict(X_test)
Вывод соответствует ожидаемому (2x fox и 2x city):
['fox' 'fox' 'city' 'city']
person
aleju
schedule
06.04.2015