首頁 >後端開發 >Python教學 >使用 Matplotlib 在 Python 中視覺化情緒分析結果

使用 Matplotlib 在 Python 中視覺化情緒分析結果

Barbara Streisand
Barbara Streisand原創
2025-01-05 12:38:41689瀏覽

在本文中,我們將使用 Matplotlib 加入情緒分析結果的圖形表示。目標是可視化多個句子的情緒分數,並使用長條圖使用不同的顏色來區分正面和負面情緒。

先決條件

確保您安裝了以下程式庫:

pip install transformers torch matplotlib
  • Transformer:用於處理預先訓練的 NLP 模型。
  • torch:用於運行模型。
  • matplotlib:用於建立情緒分析結果的圖形表示。

視覺化 Python 程式碼

Visualizing Sentiment Analysis Results in Python using Matplotlib

這是更新後的 Python 程式碼,它將情緒分析與資料視覺化整合。

import matplotlib.pyplot as plt
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load pre-trained model and tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Initialize the sentiment-analysis pipeline
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# List of 10 sentences for sentiment analysis
sentences = [
    "I love you! I love you! I love you!",
    "I feel so sad today.",
    "This is the best day ever!",
    "I can't stand the rain.",
    "Everything is going so well.",
    "I hate waiting in line.",
    "The weather is nice, but it's cold.",
    "I'm so proud of my achievements.",
    "I am very upset with the decision.",
    "I am feeling optimistic about the future."
]

# Prepare data for the chart
scores = []
colors = []

for sentence in sentences:
    result = classifier(sentence)
    sentiment = result[0]['label']
    score = result[0]['score']
    scores.append(score)

    # Color bars based on sentiment: Positive -> green, Negative -> red
    if sentiment == "POSITIVE":
        colors.append("green")
    else:
        colors.append("red")

# Create a bar chart
plt.figure(figsize=(10, 6))
bars = plt.bar(sentences, scores, color=colors)

# Add labels and title with a line break
plt.xlabel('Sentences')
plt.ylabel('Sentiment Score')
plt.title('Sentiment Analysis of 10 Sentences\n')  # Added newline here
plt.xticks(rotation=45, ha="right")

# Adjust spacing with top margin (to add ceiling space)
plt.subplots_adjust(top=0.85)  # Adjust the top spacing (20px roughly equivalent to 0.1 top margin)

plt.tight_layout()  # Adjusts the rest of the layout

# Display the sentiment score on top of the bars
for bar in bars:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width() / 2, yval + 0.02, f'{yval:.2f}', ha='center', va='bottom', fontsize=9)

# Show the plot
plt.show()

代碼分解

導入必要的函式庫:
我們導入 matplotlib.pyplot 來建立繪圖和轉換器來執行情緒分析。

   import matplotlib.pyplot as plt
   from transformers import pipeline
   from transformers import AutoTokenizer, AutoModelForSequenceClassification

載入預訓練模型:
我們載入針對 SST-2 資料集進行情緒分析而微調的 DistilBERT 模型。我們也載入關聯的標記產生器,將文字轉換為模型可讀的標記。

   model_name = "distilbert-base-uncased-finetuned-sst-2-english"
   model = AutoModelForSequenceClassification.from_pretrained(model_name)
   tokenizer = AutoTokenizer.from_pretrained(model_name)

初始化情緒分析管道:
分類器管道是為情感分析而設定的。此管道負責對輸入文字進行標記、執行推理並傳回結果。

   classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

情緒分析句:
我們建立一個包含 10 個句子的清單來進行分析。每句話都是一種獨特的情感表達,從非常正面到負面。

   sentences = [
       "I love you! I love you! I love you!",
       "I feel so sad today.",
       "This is the best day ever!",
       "I can't stand the rain.",
       "Everything is going so well.",
       "I hate waiting in line.",
       "The weather is nice, but it's cold.",
       "I'm so proud of my achievements.",
       "I am very upset with the decision.",
       "I am feeling optimistic about the future."
   ]

處理情緒並準備資料:
對於每個句子,我們對其情緒進行分類並提取分數。根據情緒標籤(正面或負面),我們為圖表中的長條指定顏色。正面的句子將是綠色的,而負面的句子將是紅色的。

   scores = []
   colors = []

   for sentence in sentences:
       result = classifier(sentence)
       sentiment = result[0]['label']
       score = result[0]['score']
       scores.append(score)

       if sentiment == "POSITIVE":
           colors.append("green")
       else:
           colors.append("red")

建立長條圖:
我們使用 matplotlib 建立條形圖。每個長條的高度代表一個句子的情緒分數,顏色區分正面和負面情緒。

   plt.figure(figsize=(10, 6))
   bars = plt.bar(sentences, scores, color=colors)

新增標籤並調整版面:
我們透過旋轉 x 軸標籤以提高可讀性、添加標題以及調整佈局以獲得最佳間距來自訂繪圖的外觀。

   plt.xlabel('Sentences')
   plt.ylabel('Sentiment Score')
   plt.title('Sentiment Analysis of 10 Sentences\n')  # Added newline here
   plt.xticks(rotation=45, ha="right")
   plt.subplots_adjust(top=0.85)  # Adjust the top spacing
   plt.tight_layout()  # Adjusts the rest of the layout

在條形頂部顯示情緒分數:
我們還在每個長條的頂部顯示情緒得分,以使圖表提供更多資訊。

pip install transformers torch matplotlib

顯示圖:
最後,使用 plt.show() 顯示圖表,它渲染繪圖。

import matplotlib.pyplot as plt
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load pre-trained model and tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Initialize the sentiment-analysis pipeline
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# List of 10 sentences for sentiment analysis
sentences = [
    "I love you! I love you! I love you!",
    "I feel so sad today.",
    "This is the best day ever!",
    "I can't stand the rain.",
    "Everything is going so well.",
    "I hate waiting in line.",
    "The weather is nice, but it's cold.",
    "I'm so proud of my achievements.",
    "I am very upset with the decision.",
    "I am feeling optimistic about the future."
]

# Prepare data for the chart
scores = []
colors = []

for sentence in sentences:
    result = classifier(sentence)
    sentiment = result[0]['label']
    score = result[0]['score']
    scores.append(score)

    # Color bars based on sentiment: Positive -> green, Negative -> red
    if sentiment == "POSITIVE":
        colors.append("green")
    else:
        colors.append("red")

# Create a bar chart
plt.figure(figsize=(10, 6))
bars = plt.bar(sentences, scores, color=colors)

# Add labels and title with a line break
plt.xlabel('Sentences')
plt.ylabel('Sentiment Score')
plt.title('Sentiment Analysis of 10 Sentences\n')  # Added newline here
plt.xticks(rotation=45, ha="right")

# Adjust spacing with top margin (to add ceiling space)
plt.subplots_adjust(top=0.85)  # Adjust the top spacing (20px roughly equivalent to 0.1 top margin)

plt.tight_layout()  # Adjusts the rest of the layout

# Display the sentiment score on top of the bars
for bar in bars:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width() / 2, yval + 0.02, f'{yval:.2f}', ha='center', va='bottom', fontsize=9)

# Show the plot
plt.show()

樣本輸出

此程式碼的輸出將是一個顯示 10 個句子的情緒分數的長條圖。正面的句子將以綠色條表示,而負面的句子將顯示為紅色條。情緒分數將顯示在每個長條上方,顯示模型的置信水準。

結論

透過將情緒分析與資料視覺化結合,我們可以更好地解讀文字資料背後的情緒基調。本文中的圖形表示可以讓您更清楚地了解情緒分佈,使您可以輕鬆發現文字中的趨勢。您可以將此技術應用於各種用例,例如分析產品評論、社交媒體貼文或客戶回饋。

透過 Hugging Face 的轉換器和 matplotlib 的強大組合,可以擴展和自訂此工作流程以適應各種 NLP 任務。

以上是使用 Matplotlib 在 Python 中視覺化情緒分析結果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn