各次元削減手法（PCA, UMAP, Isomap）と自然言語処理【日記】


```Python
# Program Name: compare_pca_umap_isomap_2d_3d.py
# Creation Date: 20250606
# Overview: Visualize dummy English word vectors using PCA, UMAP, and Isomap in 2D and 3D
# Usage: Run in Google Colab or Jupyter

# -------------------- Install Required Libraries --------------------
!pip install matplotlib scikit-learn umap-learn --quiet

# -------------------- Import Libraries --------------------
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.manifold import Isomap
import umap
from mpl_toolkits.mplot3d import Axes3D

# -------------------- Step 1: Create Dummy Word Vectors --------------------
words = ['cat', 'dog', 'mouse', 'car', 'bus', 'train', 'love', 'sadness', 'joy', 'anger']
np.random.seed(42)
vectors = np.random.rand(len(words), 300)

# -------------------- Step 2: 2D Reduction --------------------
pca_2d = PCA(n_components=2).fit_transform(vectors)
umap_2d = umap.UMAP(n_components=2, random_state=42).fit_transform(vectors)
isomap_2d = Isomap(n_components=2).fit_transform(vectors)

# -------------------- Step 3: 3D Reduction --------------------
pca_3d = PCA(n_components=3).fit_transform(vectors)
umap_3d = umap.UMAP(n_components=3, random_state=42).fit_transform(vectors)
isomap_3d = Isomap(n_components=3).fit_transform(vectors)

# -------------------- Step 4: 2D Plotting Function --------------------
def plot_2d(data, title):
    plt.figure(figsize=(8, 5))
    for i, word in enumerate(words):
        plt.scatter(data[i, 0], data[i, 1])
        plt.annotate(word, (data[i, 0] + 0.1, data[i, 1]), fontsize=12)
    plt.title(title + " - 2D")
    plt.grid(True)
    plt.show()

# -------------------- Step 5: 3D Plotting Function --------------------
def plot_3d(data, title):
    fig = plt.figure(figsize=(9, 6))
    ax = fig.add_subplot(111, projection='3d')
    for i, word in enumerate(words):
        x, y, z = data[i]
        ax.scatter(x, y, z)
        ax.text(x + 0.05, y + 0.05, z, word, fontsize=10)
    ax.set_title(title + " - 3D")
    ax.set_xlabel('Dim 1')
    ax.set_ylabel('Dim 2')
    ax.set_zlabel('Dim 3')
    plt.show()

# -------------------- Step 6: Visualize All Methods in 2D and 3D --------------------
plot_2d(pca_2d, "PCA")
plot_3d(pca_3d, "PCA")

plot_2d(umap_2d, "UMAP")
plot_3d(umap_3d, "UMAP")

plot_2d(isomap_2d, "Isomap")
plot_3d(isomap_3d, "Isomap")

```
```Python
# Program Name: japanese_sentiment_analysis_line_by_line.py  
# Creation Date: 20250606  
# Overview: Perform sentence-wise Japanese sentiment analysis using a Hugging Face BERT model  
# Usage: Run in Google Colab with internet access and transformers installed  

# -------------------- Install Required Libraries --------------------
!pip install transformers fugashi unidic-lite --quiet

# -------------------- Import Libraries --------------------
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# -------------------- Step 1: モデルのロード / Load Pretrained Japanese Sentiment Model --------------------
model_name = "jarvisx17/japanese-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# -------------------- Step 2: 各文ごとの歌詞 / Define Lyrics Line by Line --------------------
lines = [
    "都会を走る猫に名前はいらない",
    "公園で群れるハトにパンを投げる投げる投げる",
    "池のコイは太い コウモリが飛んでいく",
    "もうすぐ帰れるさって思うもうすぐ家まで",
    "世界が歌っている世界が歌っている"
]

# -------------------- Step 3: 文ごとに感情分析を実行 / Per-line Sentiment Analysis --------------------
label_map = model.config.id2label

for i, line in enumerate(lines):
    inputs = tokenizer(line, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        probs = torch.nn.functional.softmax(logits, dim=-1)
        pred = torch.argmax(probs, dim=1).item()
    
    print(f"[{i+1}] {line}")
    print(f"    → 感情: {label_map[pred]} (確信度: {probs[0][pred].item():.2f})")

```
```Python
# Program Name: word2vec_visualize_google_news.py
# Creation Date: 20250606
# Overview: Visualize 10 English word vectors using GoogleNews Word2Vec model with PCA, UMAP, Isomap
# Usage: Run in Google Colab. Download model if not available.

# -------------------- Install Required Libraries --------------------
!pip install gensim umap-learn scikit-learn matplotlib --quiet

# -------------------- Import Libraries --------------------
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.manifold import Isomap
import umap
from gensim.models import KeyedVectors
from mpl_toolkits.mplot3d import Axes3D

# -------------------- Download GoogleNews Word2Vec Model --------------------
import os
if not os.path.exists("GoogleNews-vectors-negative300.bin.gz"):
    !wget -q "https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz"

# -------------------- Load Pretrained Word2Vec --------------------
model_path = 'GoogleNews-vectors-negative300.bin.gz'
word2vec_model = KeyedVectors.load_word2vec_format(model_path, binary=True)

# -------------------- Define Words --------------------
words = ['cat', 'dog', 'mouse', 'car', 'bus', 'train', 'love', 'sadness', 'joy', 'anger']
vectors = np.array([word2vec_model[word] for word in words])

# -------------------- 2D Reduction --------------------
pca_2d = PCA(n_components=2).fit_transform(vectors)
umap_2d = umap.UMAP(n_components=2, random_state=42).fit_transform(vectors)
isomap_2d = Isomap(n_components=2).fit_transform(vectors)

# -------------------- 3D Reduction --------------------
pca_3d = PCA(n_components=3).fit_transform(vectors)
umap_3d = umap.UMAP(n_components=3, random_state=42).fit_transform(vectors)
isomap_3d = Isomap(n_components=3).fit_transform(vectors)

# -------------------- Plotting Functions --------------------
def plot_2d(data, title):
    plt.figure(figsize=(8, 5))
    for i, word in enumerate(words):
        plt.scatter(data[i, 0], data[i, 1])
        plt.annotate(word, (data[i, 0] + 0.1, data[i, 1]), fontsize=12)
    plt.title(title + " - 2D")
    plt.grid(True)
    plt.show()

def plot_3d(data, title):
    fig = plt.figure(figsize=(9, 6))
    ax = fig.add_subplot(111, projection='3d')
    for i, word in enumerate(words):
        x, y, z = data[i]
        ax.scatter(x, y, z)
        ax.text(x + 0.05, y + 0.05, z, word, fontsize=10)
    ax.set_title(title + " - 3D")
    ax.set_xlabel('Dim 1')
    ax.set_ylabel('Dim 2')
    ax.set_zlabel('Dim 3')
    plt.show()

# -------------------- Plot All --------------------
plot_2d(pca_2d, "PCA")
plot_3d(pca_3d, "PCA")

plot_2d(umap_2d, "UMAP")
plot_3d(umap_3d, "UMAP")

plot_2d(isomap_2d, "Isomap")
plot_3d(isomap_3d, "Isomap")

```