NLTK庫主要用于自然語言處理任務,不直接提供計算模型評估指標的功能。一般來說,要計算模型評估指標,可以使用其他庫如scikit-learn或者直接編寫代碼來計算。以下是一個示例代碼,演示如何使用scikit-learn庫計算模型評估指標:
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import nltk
from nltk.corpus import movie_reviews
# Load movie reviews dataset
nltk.download('movie_reviews')
documents = [(list(movie_reviews.words(fileid)), category) for category in movie_reviews.categories() for fileid in movie_reviews.fileids(category)]
text = [" ".join(document) for document, category in documents]
labels = [category for document, category in documents]
# Vectorize the text data
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(text)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=42)
# Train a logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Calculate evaluation metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
print(f"Accuracy: {accuracy}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
上述代碼使用scikit-learn庫加載電影評論數據集,訓練了一個邏輯回歸模型,并計算了準確率、精確率、召回率和F1分數等模型評估指標。您可以根據實際需求修改代碼以適應不同的數據集和模型。