Scikit-Learn におけるバイクラスタリング

Machine LearningMachine LearningBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

バイクラスタリングは、データ行列の行と列を同時にクラスタリングする手法です。これにより、データ行列内に特定の特性を持つサブ行列を識別することができます。バイクラスタリングは、データ分析、画像処理、バイオインフォマティクスなど、さまざまな分野で役立ちます。

この実験では、scikit-learnのsklearn.cluster.biclusterモジュールを使用してバイクラスタリングを行う方法を学びます。2つの一般的なバイクラスタリングアルゴリズム:スペクトラルコクラスタリングとスペクトラルバイクラスタリングを探ります。これらのアルゴリズムは、バイクラスタに行と列を定義および割り当てる方法が異なります。

VMのヒント

VMの起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebookを使って練習しましょう。

Jupyter Notebookの読み込みには数秒かかる場合があります。Jupyter Notebookの制限により、操作の検証を自動化することはできません。

学習中に問題があった場合は、Labbyにお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/cluster("Clustering") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/cluster -.-> lab-71117{{"Scikit-Learn におけるバイクラスタリング"}} ml/sklearn -.-> lab-71117{{"Scikit-Learn におけるバイクラスタリング"}} end

必要なライブラリとデータセットをインポートする

まず、必要なライブラリをインポートして、バイクラスタリングに使用するサンプルデータセットを読み込みましょう。

import numpy as np
from sklearn.cluster import SpectralCoclustering, SpectralBiclustering

## Load sample data
data = np.arange(100).reshape(10, 10)

スペクトラルコクラスタリングを実行する

次に、スペクトラルコクラスタリングアルゴリズムを使用してバイクラスタリングを実行しましょう。このアルゴリズムは、他の行と列と比較して値の高いバイクラスタを見つけます。

## Initialize and fit the Spectral Co-Clustering model
model_co = SpectralCoclustering(n_clusters=3, random_state=0)
model_co.fit(data)

## Get row and column cluster membership
row_clusters_co = model_co.row_labels_
column_clusters_co = model_co.column_labels_

スペクトラルバイクラスタリングを実行する

次に、スペクトラルバイクラスタリングアルゴリズムを使用してバイクラスタリングを実行しましょう。このアルゴリズムは、データ行列が隠されたチェッカーボード構造を持っていることを前提としています。

## Initialize and fit the Spectral Biclustering model
model_bi = SpectralBiclustering(n_clusters=(2, 3), random_state=0)
model_bi.fit(data)

## Get row and column cluster membership
row_clusters_bi = model_bi.row_labels_
column_clusters_bi = model_bi.column_labels_

結果を可視化する

最後に、スペクトラルコクラスタリングとスペクトラルバイクラスタリングアルゴリズムから得られたバイクラスタ構造を可視化しましょう。

## Visualization for Spectral Co-Clustering
print("Spectral Co-Clustering:")
print("Row clusters:")
print(row_clusters_co)
print("Column clusters:")
print(column_clusters_co)

## Visualization for Spectral Biclustering
print("\nSpectral Biclustering:")
print("Row clusters:")
print(row_clusters_bi)
print("Column clusters:")
print(column_clusters_bi)

まとめ

この実験では、scikit-learn のスペクトラルコクラスタリングとスペクトラルバイクラスタリングアルゴリズムを使ってバイクラスタリングを行う方法を学びました。バイクラスタリングは、データ行列の行と列を同時にクラスタリングして、特定の特性を持つ部分行列を識別することができます。バイクラスタリングは、遺伝子発現データのパターンを識別したり、画像データセットの構造を見つけたりするなど、さまざまなデータ分析タスクで役立つことができます。