Faster Implementation of KModes and KPrototypes.
Project description
FasterKModes
Kmodes と KPrototypes を実装した。
この実装では、KModes および KPrototypes のより高速な実装を目的としており、精度などは既存実装と特に変わりはない。特に、処理時間のかかる距離行列計算と最頻値計算の処理を C コードで実装することにより高速化を図った。また現状、計算できる距離の種類はカテゴリ特徴量に対してはHamming距離、数値特徴量に対してはEuclid距離のみである。数値特徴量はGEMMによる実装で対処している。
初期化、カテゴリ/数値特徴量の距離行列計算はUser Defined Functionを設定することができる。
インストール方法
pip install FasterKModes
How to use
FasterKModes
# ライブラリ内で動的に生成したファイルの削除
from FasterKModes import delete_all_generated_so
delete_all_generated_so()
import numpy as np
from FasterKModes import FasterKModes
# データ作成
N = 1100 # 総データ数
K = 31 # 特徴量数
C = 8 # クラスタ数
X = np.random.randint(0, 256, (N, K)).astype(np.uint8)
X_train = X[:1000, :]
X_test = X[1000:, :]
# FasterKModes の実行(random 初期化)
kmodes = FasterKModes(n_clusters=C, init="random", n_init=10)
kmodes.fit(X_train)
labels = kmodes.predict(X_test)
print("KModes クラスタリング結果:", labels)
FasterKPrototypes
import numpy as np
from FasterKModes import FasterKPrototypes
# データ作成
N = 1100 # 総データ数
K = 31 # 特徴量数
C = 8 # クラスタ数
X = np.random.randint(0, 256, (N, K)).astype(np.uint8)
X_train = X[:1000, :]
X_test = X[1000:, :]
# カテゴリカル特徴量のインデックス例(例:2列目、4列目、6列目、8列目、10列目)
categorical_features = [1, 3, 5, 7, 9]
# FasterKPrototypes の実行(random 初期化)
kproto = FasterKPrototypes(n_clusters=C, init="random", n_init=10)
kproto.fit(X_train, categorical=categorical_features)
labels = kproto.predict(X_test)
print("KPrototypes クラスタリング結果:", labels)
FasterKModes: カスタム初期化・カスタム距離計算の例
import numpy as np
from FasterKModes import FasterKModes
# カスタム初期化関数の例
def custom_init(X, n_clusters):
# ランダムに n_clusters 個のサンプルを初期セントロイドとして選択
indices = np.random.choice(len(X), n_clusters, replace=False)
return X[indices, :]
def custom_caetgorical_measure(x_cat, c_cat):
# 変数名は固定
# x_cat は np.array かつ, x_cat.ndim = 1
# c_cat は np.array かつ, c_cat.ndim = 1
return np.sum(x_cat != c_cat)
# データ作成
N = 1100
K = 31
C = 8
X = np.random.randint(0, 256, (N, K)).astype(np.uint8)
X_train = X[:1000, :]
X_test = X[1000:, :]
# カスタム初期化を利用した KModes の実行
kmodes_custom = FasterKModes(
n_clusters = C,
n_init = 10,
init = custom_init,
categorical_measure = custom_caetgorical_measure
)
kmodes_custom.fit(X_train)
labels_custom = kmodes_custom.predict(X_test)
print("カスタム初期化 KModes 結果:", labels_custom)
FasterKPrototypes: カスタム初期化・カスタム距離計算の例
import numpy as np
from FasterKModes import FasterKPrototypes
# カスタム初期化関数の例
def custom_init(Xcat, Xnum, n_clusters):
# ランダムに n_clusters 個のサンプルを初期セントロイドとして選択
indices = np.random.choice(len(Xcat), n_clusters, replace=False)
return Xcat[indices, :], Xnum[indices, :]
def custom_caetgorical_measure(x_cat, c_cat):
# 変数名は固定
# x_cat は np.array かつ, x_cat.ndim = 1
# c_cat は np.array かつ, c_cat.ndim = 1
return np.sum(x_cat != c_cat)
def custom_numerical_measure(x_num, c_num):
# 変数名は固定
# x_num は np.array かつ, x_num.ndim = 1
# c_num は np.array かつ, c_num.ndim = 1
return np.linalg.norm(x_num - c_num)
# データ作成
N = 1100
K = 31
C = 8
X = np.random.randint(0, 256, (N, K)).astype(np.uint8)
X_train = X[:1000, :]
X_test = X[1000:, :]
# カテゴリカル特徴量のインデックス例(例:2列目、4列目、6列目、8列目、10列目)
categorical_features = [1, 3, 5, 7, 9]
# カスタム初期化を利用した kproto の実行
kproto_custom = FasterKPrototypes(
n_clusters = C,
n_init = 10,
init = custom_init,
categorical_measure = custom_caetgorical_measure,
numerical_measure = custom_numerical_measure
)
kproto_custom.fit(X_train, categorical=categorical_features)
labels_custom = kmodes_custom.predict(X_test)
print("カスタム初期化 KPrototypes 結果:", labels_custom)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fasterkmodes-0.1.2.tar.gz.
File metadata
- Download URL: fasterkmodes-0.1.2.tar.gz
- Upload date:
- Size: 23.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c952b5b217765c1f0784d628937703357469b12f93be15146f51d799880d0045
|
|
| MD5 |
2f327d4d70d543bab63b3b1c880987fc
|
|
| BLAKE2b-256 |
168b03a15a028fcc20d5f76855390d3b882351524cd5156577b2505a1201e0d3
|
File details
Details for the file FasterKModes-0.1.2-py3-none-any.whl.
File metadata
- Download URL: FasterKModes-0.1.2-py3-none-any.whl
- Upload date:
- Size: 29.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6b28f270e608fb3307e057db45e5fe45dbbf406557d9dd343fd3a4da2588366
|
|
| MD5 |
a0d6df40ce362c809c7214d6fdf2aa08
|
|
| BLAKE2b-256 |
c8e15c6f5d834e7620b8c001271e5be2798fa1addc7632dd4c25508dcdf01603
|