NLTK庫并不是一個主要用于機器學習模型選擇的工具,它更多用于自然語言處理任務。但是,可以結合NLTK庫和其他機器學習庫(如scikit-learn)來進行模型選擇。以下是一個使用NLTK和scikit-learn庫進行模型選擇的示例:
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from nltk.corpus import movie_reviews
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
# Shuffle the documents
import random
random.shuffle(documents)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000]
def document_features(document):
document_words = set(document)
features = {}
for word in word_features:
features['contains({})'.format(word)] = (word in document_words)
return features
featuresets = [(document_features(d), c) for (d,c) in documents]
train_set, test_set = featuresets[100:], featuresets[:100]
nb_classifier = SklearnClassifier(MultinomialNB())
svm_classifier = SklearnClassifier(SVC())
nb_scores = cross_val_score(nb_classifier, train_set, cv=5)
svm_scores = cross_val_score(svm_classifier, train_set, cv=5)
print("Naive Bayes Classifier Accuracy:", nb_scores.mean())
print("SVM Classifier Accuracy:", svm_scores.mean())
通過以上步驟,可以使用NLTK和scikit-learn庫進行模型選擇,并選擇性能最佳的模型進行進一步優化和預測。