🚀 ツイートのスパム検出
このモデルは、X(旧Twitter)のツイートを「スパム」(1)または「良質」(0)に分類します。
🚀 クイックスタート
このモデルは、X(旧Twitter)からのツイートを「スパム」(1)または「良質」(0)に分類します。
✨ 主な機能
- X(旧Twitter)のツイートをスパムと良質に分類する機能。
- 高精度な分類性能を持ち、テストセットでの精度は0.974555に達します。
📦 インストール
このドキュメントには具体的なインストール手順が記載されていないため、このセクションをスキップします。
💻 使用例
基本的な使用法
def classify_texts(df, text_col, model_path="cja5553/xlm-roberta-Twitter-spam-classification", batch_size=24):
'''
Classifies texts as either "Quality" or "Spam" using a pre-trained sequence classification model.
Parameters:
-----------
df : pandas.DataFrame
DataFrame containing the texts to classify.
text_col : str
Name of the column in that contains the text data to be classified.
model_path : str, default="cja5553/xlm-roberta-Twitter-spam-classification"
Path to the pre-trained model for sequence classification.
batch_size : int, optional, default=24
Batch size for loading and processing data in batches. Adjust based on available GPU memory.
Returns:
--------
pandas.DataFrame
The original DataFrame with an additional column `spam_prediction`, containing the predicted labels ("Quality" or "Spam") for each text.
'''
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path).to("cuda")
model.eval()
df["text"] = df[text_col].astype(str)
text_dataset = Dataset.from_pandas(df)
def tokenize_function(example):
return tokenizer(
example["text"],
padding="max_length",
truncation=True,
max_length=512
)
text_dataset = text_dataset.map(tokenize_function, batched=True)
text_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask'])
text_loader = DataLoader(text_dataset, batch_size=batch_size)
predictions = []
with torch.no_grad():
for batch in tqdm_notebook(text_loader):
input_ids = batch['input_ids'].to("cuda")
attention_mask = batch['attention_mask'].to("cuda")
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs.logits
preds = torch.argmax(logits, dim=-1).cpu().numpy()
predictions.extend(preds)
id2label = {0: "Quality", 1: "Spam"}
predicted_labels = [id2label[pred] for pred in predictions]
df["spam_prediction"] = predicted_labels
return df
spam_df_classification = classify_texts(df, "text_col")
print(spam_df_classification)
📚 ドキュメント
学習データセット
このモデルは、UtkMlのTwitter Spam Detectionデータセットを使用して微調整され、ベースモデルとしてFacebookAI/xlm-roberta-large
を使用しています。
評価指標
80-10-10の学習-検証-テスト分割に基づいて、テストセットで以下の結果が得られました。
属性 |
详情 |
精度 (Accuracy) |
0.974555 |
適合率 (Precision) |
0.97457 |
再現率 (Recall) |
0.97455 |
F1値 (F1-Score) |
0.97455 |
コード
これらのモデルを学習するために使用されたコードは、GitHubのgithub.com/cja5553/Twitter_spam_detectionで入手できます。
質問
質問がある場合は、alba@wustl.eduまでご連絡ください。
📄 ライセンス
このプロジェクトはMITライセンスの下で公開されています。