Matplotlib を使用した Python での感情分析結果の視覚化
この記事では、Matplotlib を使用して感情分析結果のグラフィック表現を追加します。目標は、複数の文の感情スコアを、異なる色を使用して肯定的な感情と否定的な感情を区別する棒グラフで視覚化することです。
前提条件
次のライブラリがインストールされていることを確認してください:
pip install transformers torch matplotlib
- トランスフォーマー: 事前トレーニングされた NLP モデルを処理するため。
- トーチ: モデルを実行するため。
- matplotlib: 感情分析結果のグラフ表現を作成します。
Python コードと視覚化
これは感情分析とデータ視覚化を統合する更新された 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 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Pythonは、データサイエンス、Web開発、自動化タスクに適していますが、Cはシステムプログラミング、ゲーム開発、組み込みシステムに適しています。 Pythonは、そのシンプルさと強力なエコシステムで知られていますが、Cは高性能および基礎となる制御機能で知られています。

PythonはゲームとGUI開発に優れています。 1)ゲーム開発は、2Dゲームの作成に適した図面、オーディオ、その他の機能を提供し、Pygameを使用します。 2)GUI開発は、TKINTERまたはPYQTを選択できます。 TKINTERはシンプルで使いやすく、PYQTは豊富な機能を備えており、専門能力開発に適しています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは学習と使用が簡単ですが、Cはより強力ですが複雑です。 1。Python構文は簡潔で初心者に適しています。動的なタイピングと自動メモリ管理により、使いやすくなりますが、ランタイムエラーを引き起こす可能性があります。 2.Cは、高性能アプリケーションに適した低レベルの制御と高度な機能を提供しますが、学習しきい値が高く、手動メモリとタイプの安全管理が必要です。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

限られた時間でPythonの学習効率を最大化するには、PythonのDateTime、時間、およびスケジュールモジュールを使用できます。 1. DateTimeモジュールは、学習時間を記録および計画するために使用されます。 2。時間モジュールは、勉強と休息の時間を設定するのに役立ちます。 3.スケジュールモジュールは、毎週の学習タスクを自動的に配置します。

Pythonは、自動化、スクリプト、およびタスク管理に優れています。 1)自動化:OSやShutilなどの標準ライブラリを介してファイルバックアップが実現されます。 2)スクリプトの書き込み:Psutilライブラリを使用してシステムリソースを監視します。 3)タスク管理:スケジュールライブラリを使用してタスクをスケジュールします。 Pythonの使いやすさと豊富なライブラリサポートにより、これらの分野で優先ツールになります。
