Metadata-Version: 2.1
Name: ifw-kmeans
Version: 0.1.1
Summary: Inverse-Frequency-Weighted K-Means clustering, scikit-learn compatible.
Author-email: Li Zhenxian <w2kmeu@163.com>
License: Apache License 2.0
Project-URL: Homepage, https://github.com/w2kme/ifw-kmeans
Project-URL: Repository, https://github.com/w2kme/ifw-kmeans
Keywords: clustering,kmeans,imbalanced,sklearn,ifw
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21
Requires-Dist: pandas>=1.3
Requires-Dist: scipy>=1.7
Requires-Dist: scikit-learn>=1.0
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=4.0; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"

# ifw-kmeans

Inverse-Frequency-Weighted K-Means clustering（对数平滑逆频率加权聚类）。

## 安装

```bash
pip install -e ".[dev]"        # 本地开发
# 或
python -m build && pip install dist/*.whl


快速开始

from ifw_kmeans import IFWKmeans

# 模式1：端到端（宽表 + 画像表）
model = IFWKmeans(n_clusters=4, random_state=42)
model.fit(
    wide_table=df_wide,
    profile_df=df_profile,
    feat_cols=["f1", "f2", "f3"],
    id_col="entity_id",
)
res = model.get_results()
print(res["metrics"])

# 模式2：直接喂数据
model2 = IFWKmeans(n_clusters=5)
labels = model2.fit_predict(X_matrix, freq=freq_array)

# 预测
new_labels = model2.predict(X_new, freq=freq_new)


API
方法	说明
fit(X=None, y=None, *, wide_table, profile_df, feat_cols, id_col, freq)	三种模式训练
fit_predict(X, freq)	训练并返回 labels
predict(X, freq=None)	预测
get_results()	返回业务结果字典
注意

本包提供业务 API，继承 BaseEstimator, ClusterMixin 仅为接口占位。
不保证 sklearn.clone / Pipeline / GridSearchCV 兼容（因 __init__ 预置拟合属性）。
若需 sklearn 工具链，请直接用 sklearn.cluster.KMeans 或参考 estimator.py 中注释做最小适配。



## License

Licensed under the Apache License, Version 2.0.
See [LICENSE](LICENSE) for the full text.

Copyright 2026 Your Name
