hossam 0.4.17__tar.gz → 0.4.18__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hossam-0.4.17/hossam.egg-info → hossam-0.4.18}/PKG-INFO +1 -1
- {hossam-0.4.17 → hossam-0.4.18}/hossam/__init__.py +19 -0
- hossam-0.4.17/hossam/hs_cluster.py → hossam-0.4.18/hossam/hs_cluster copy.py +51 -10
- hossam-0.4.18/hossam/hs_cluster.py +1097 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_plot.py +23 -11
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_stats.py +39 -2
- {hossam-0.4.17 → hossam-0.4.18/hossam.egg-info}/PKG-INFO +1 -1
- {hossam-0.4.17 → hossam-0.4.18}/hossam.egg-info/SOURCES.txt +1 -0
- {hossam-0.4.17 → hossam-0.4.18}/pyproject.toml +1 -1
- {hossam-0.4.17 → hossam-0.4.18}/LICENSE +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/MANIFEST.in +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/README.md +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/NotoSansKR-Regular.ttf +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_classroom.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_gis.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_prep.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_study.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_timeserise.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/hs_util.py +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam/leekh.png +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam.egg-info/dependency_links.txt +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam.egg-info/requires.txt +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/hossam.egg-info/top_level.txt +0 -0
- {hossam-0.4.17 → hossam-0.4.18}/setup.cfg +0 -0
|
@@ -119,10 +119,29 @@ def _init():
|
|
|
119
119
|
|
|
120
120
|
_init_korean_font()
|
|
121
121
|
|
|
122
|
+
pd.set_option("display.max_colwidth", None)
|
|
123
|
+
pd.set_option("display.width", None)
|
|
122
124
|
# 컬럼 생략 금지
|
|
123
125
|
pd.set_option("display.max_columns", None)
|
|
124
126
|
# 행 최대 출력 수 100개로 수정
|
|
125
127
|
pd.set_option("display.max_rows", 100)
|
|
126
128
|
|
|
129
|
+
from IPython.display import display, HTML
|
|
130
|
+
|
|
131
|
+
display(HTML("""
|
|
132
|
+
<style>
|
|
133
|
+
.dataframe td, .dataframe th {
|
|
134
|
+
white-space: nowrap;
|
|
135
|
+
font-size: 0.85em;
|
|
136
|
+
padding: 2px 3px;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.dataframe tr:hover {
|
|
140
|
+
background-color: #ffff99 !important;
|
|
141
|
+
border: 1px solid #ffcc00;
|
|
142
|
+
}
|
|
143
|
+
</style>
|
|
144
|
+
"""))
|
|
145
|
+
|
|
127
146
|
|
|
128
147
|
_init()
|
|
@@ -23,6 +23,7 @@ from sklearn.metrics import silhouette_score, adjusted_rand_score
|
|
|
23
23
|
# hossam 패키지 참조
|
|
24
24
|
# ===================================================================
|
|
25
25
|
from . import hs_plot
|
|
26
|
+
from .hs_util import is_2d
|
|
26
27
|
|
|
27
28
|
RANDOM_STATE = 52
|
|
28
29
|
|
|
@@ -32,10 +33,11 @@ RANDOM_STATE = 52
|
|
|
32
33
|
# ===================================================================
|
|
33
34
|
def kmeans_fit(
|
|
34
35
|
data: DataFrame,
|
|
35
|
-
n_clusters: int,
|
|
36
|
+
n_clusters: int | None = None,
|
|
37
|
+
k_range: list | tuple = [2, 11],
|
|
36
38
|
random_state: int = RANDOM_STATE,
|
|
37
39
|
plot: bool = False,
|
|
38
|
-
fields: list[list[str]] | None = None,
|
|
40
|
+
fields: list[str] | tuple[str] | tuple[tuple[str]] | list[list[str]] | None = None,
|
|
39
41
|
**params,
|
|
40
42
|
) -> tuple[KMeans, DataFrame, float]:
|
|
41
43
|
"""
|
|
@@ -43,7 +45,7 @@ def kmeans_fit(
|
|
|
43
45
|
|
|
44
46
|
Args:
|
|
45
47
|
data (DataFrame): 군집화할 데이터프레임.
|
|
46
|
-
n_clusters (int): 군집 개수.
|
|
48
|
+
n_clusters (int | None): 군집 개수.
|
|
47
49
|
random_state (int, optional): 랜덤 시드. 기본값은 RANDOM_STATE.
|
|
48
50
|
plot (bool, optional): True면 결과를 시각화함. 기본값 False.
|
|
49
51
|
fields (list[list[str]] | None, optional): 시각화할 필드 쌍 리스트. 기본값 None이면 수치형 컬럼의 모든 조합 사용.
|
|
@@ -55,18 +57,36 @@ def kmeans_fit(
|
|
|
55
57
|
float: 실루엣 점수
|
|
56
58
|
"""
|
|
57
59
|
df = data.copy()
|
|
60
|
+
|
|
61
|
+
if n_clusters is None:
|
|
62
|
+
n_clusters = kmeans_best_k(data=df, k_range=k_range, random_state=random_state, plot=False)
|
|
63
|
+
print(f"Best k found: {n_clusters}")
|
|
64
|
+
|
|
58
65
|
kmeans = KMeans(n_clusters=n_clusters, random_state=random_state, **params)
|
|
59
66
|
kmeans.fit(data)
|
|
60
67
|
df["cluster"] = kmeans.predict(df)
|
|
61
68
|
score = float(silhouette_score(X=data, labels=df["cluster"]))
|
|
62
69
|
|
|
63
70
|
if plot:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
|
|
72
|
+
if not is_2d(fields):
|
|
73
|
+
fields = [fields] # type: ignore
|
|
74
|
+
|
|
75
|
+
# cluster_plot(
|
|
76
|
+
# estimator=kmeans,
|
|
77
|
+
# data=data,
|
|
78
|
+
# fields=fields,
|
|
79
|
+
# title=f"K-Means Clustering (k={n_clusters})",
|
|
80
|
+
# )
|
|
81
|
+
for f in fields: # type: ignore
|
|
82
|
+
hs_plot.visualize_silhouette(
|
|
83
|
+
estimator=kmeans,
|
|
84
|
+
data=data,
|
|
85
|
+
xname=f[0], # type: ignore
|
|
86
|
+
yname=f[1], # type: ignore
|
|
87
|
+
title=f"K-Means Clustering (k={n_clusters})",
|
|
88
|
+
outline=True,
|
|
89
|
+
)
|
|
70
90
|
|
|
71
91
|
return kmeans, df, score
|
|
72
92
|
|
|
@@ -779,7 +799,9 @@ def dbscan_eps(
|
|
|
779
799
|
|
|
780
800
|
return best_eps, eps_grid
|
|
781
801
|
|
|
782
|
-
|
|
802
|
+
# ===================================================================
|
|
803
|
+
# DBSCAN 군집화 모델을 적합하고 최적의 eps 값을 탐지하는 함수.
|
|
804
|
+
# ===================================================================
|
|
783
805
|
def dbscan_fit(
|
|
784
806
|
data: DataFrame,
|
|
785
807
|
eps: float | list | np.ndarray | None = None,
|
|
@@ -789,6 +811,25 @@ def dbscan_fit(
|
|
|
789
811
|
plot: bool = True,
|
|
790
812
|
**params,
|
|
791
813
|
) -> tuple[DBSCAN, DataFrame, DataFrame]:
|
|
814
|
+
"""
|
|
815
|
+
DBSCAN 군집화 모델을 적합하고 최적의 eps 값을 탐지하는 함수.
|
|
816
|
+
|
|
817
|
+
Args:
|
|
818
|
+
data (DataFrame): 군집화할 데이터프레임.
|
|
819
|
+
eps (float | list | np.ndarray | None, optional): eps 값 또는 리스트.
|
|
820
|
+
None이면 최적의 eps 값을 탐지함. 기본값 None.
|
|
821
|
+
min_samples (int, optional): 핵심점이 되기 위한 최소 샘플수. 기본값 5.
|
|
822
|
+
ari_threshold (float, optional): 안정 구간 탐지를 위한 ARI 임계값. 기본값 0.9.
|
|
823
|
+
noise_diff_threshold (float, optional): 안정 구간 탐지를 위한 노이즈 비율 변화 임계값. 기본값 0.05.
|
|
824
|
+
plot (bool, optional): True면 결과를 시각화함. 기본값 True.
|
|
825
|
+
**params: DBSCAN에 전달할 추가 파라미터.
|
|
826
|
+
|
|
827
|
+
Returns:
|
|
828
|
+
tuple: (estimator, cluster_df, result_df)
|
|
829
|
+
- estimator: 적합된 DBSCAN 모델 또는 모델 리스트(최적 eps가 여러 개인 경우).
|
|
830
|
+
- cluster_df: 클러스터 및 벡터 유형이 포함된 데이터 프레임 또는 데이터 프레임 리스트(최적 eps가 여러 개인 경우).
|
|
831
|
+
- result_df: eps 값에 따른 군집화 요약 통계 데이터 프레임.
|
|
832
|
+
"""
|
|
792
833
|
|
|
793
834
|
# eps 값이 지정되지 않은 경우 최적의 eps 탐지
|
|
794
835
|
if eps is None:
|