hossam 0.3.19__py3-none-any.whl → 0.4__py3-none-any.whl
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/__init__.py +19 -22
- hossam/data_loader.py +16 -10
- hossam/hs_classroom.py +69 -44
- hossam/hs_gis.py +10 -6
- hossam/hs_plot.py +153 -150
- hossam/hs_prep.py +95 -85
- hossam/hs_stats.py +426 -548
- hossam/hs_timeserise.py +161 -152
- hossam/hs_util.py +44 -17
- {hossam-0.3.19.dist-info → hossam-0.4.dist-info}/METADATA +6 -107
- hossam-0.4.dist-info/RECORD +16 -0
- hossam/mcp/__init__.py +0 -12
- hossam/mcp/hs_classroom.py +0 -22
- hossam/mcp/hs_gis.py +0 -30
- hossam/mcp/hs_plot.py +0 -53
- hossam/mcp/hs_prep.py +0 -61
- hossam/mcp/hs_stats.py +0 -25
- hossam/mcp/hs_timeserise.py +0 -22
- hossam/mcp/hs_util.py +0 -30
- hossam/mcp/loader.py +0 -29
- hossam/mcp/server.py +0 -675
- hossam-0.3.19.dist-info/RECORD +0 -27
- hossam-0.3.19.dist-info/entry_points.txt +0 -2
- {hossam-0.3.19.dist-info → hossam-0.4.dist-info}/WHEEL +0 -0
- {hossam-0.3.19.dist-info → hossam-0.4.dist-info}/licenses/LICENSE +0 -0
- {hossam-0.3.19.dist-info → hossam-0.4.dist-info}/top_level.txt +0 -0
hossam/hs_prep.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
# ===================================================================
|
|
3
|
-
#
|
|
3
|
+
# 패키지 참조
|
|
4
4
|
# ===================================================================
|
|
5
5
|
import joblib
|
|
6
6
|
import numpy as np
|
|
7
7
|
from itertools import combinations
|
|
8
8
|
|
|
9
|
-
# ===================================================================
|
|
10
|
-
#
|
|
11
|
-
# ===================================================================
|
|
12
9
|
import pandas as pd
|
|
13
10
|
import jenkspy
|
|
14
11
|
from pandas import DataFrame
|
|
15
12
|
from sklearn.preprocessing import StandardScaler, MinMaxScaler
|
|
16
13
|
from sklearn.impute import SimpleImputer
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
#
|
|
20
|
-
# ===================================================================
|
|
15
|
+
|
|
21
16
|
from .hs_util import pretty_table
|
|
22
17
|
|
|
23
18
|
# ===================================================================
|
|
@@ -42,8 +37,10 @@ def standard_scaler(
|
|
|
42
37
|
DataFrame | ndarray: 스케일링된 데이터(입력 타입과 동일).
|
|
43
38
|
|
|
44
39
|
Examples:
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
```python
|
|
41
|
+
from hossam import *
|
|
42
|
+
std_df = hs_prep.standard_scaler(df, yname="y", save_path="std.pkl")
|
|
43
|
+
```
|
|
47
44
|
"""
|
|
48
45
|
|
|
49
46
|
is_df = isinstance(data, DataFrame)
|
|
@@ -110,8 +107,10 @@ def minmax_scaler(
|
|
|
110
107
|
DataFrame | ndarray: 스케일링된 데이터(입력 타입과 동일).
|
|
111
108
|
|
|
112
109
|
Examples:
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
```python
|
|
111
|
+
from hossam import *
|
|
112
|
+
mm_df = hs_prep.minmax_scaler(df, yname="y")
|
|
113
|
+
```
|
|
115
114
|
"""
|
|
116
115
|
|
|
117
116
|
is_df = isinstance(data, DataFrame)
|
|
@@ -198,12 +197,16 @@ def unmelt(
|
|
|
198
197
|
DataFrame: 변환된 데이터프레임 (각 그룹이 개별 컬럼으로 구성)
|
|
199
198
|
|
|
200
199
|
Examples:
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
200
|
+
```python
|
|
201
|
+
df = pd.DataFrame({
|
|
202
|
+
'group': ['A', 'A', 'B', 'B', 'B'],
|
|
203
|
+
'value': [1, 2, 3, 4, 5]
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
from hossam import *
|
|
207
|
+
result = hs_prep.unmelt(df, id_vars='group', value_vars='value')
|
|
208
|
+
# 결과: A 컬럼에는 [1, 2, NaN], B 컬럼에는 [3, 4, 5]
|
|
209
|
+
```
|
|
207
210
|
"""
|
|
208
211
|
# 그룹별로 값들을 리스트로 모음
|
|
209
212
|
grouped = data.groupby(id_vars, observed=True)[value_vars].apply(lambda x: x.tolist())
|
|
@@ -230,8 +233,8 @@ def outlier_table(data: DataFrame, *fields: str) -> DataFrame:
|
|
|
230
233
|
DataFrame: Q1, Q2(중앙값), Q3, IQR, 하한, 상한을 포함한 통계표.
|
|
231
234
|
|
|
232
235
|
Examples:
|
|
233
|
-
|
|
234
|
-
|
|
236
|
+
from hossam import *
|
|
237
|
+
hs_prep.outlier_table(df, "value")
|
|
235
238
|
"""
|
|
236
239
|
|
|
237
240
|
target_fields = list(fields) if fields else list(data.select_dtypes(include=[np.number]).columns)
|
|
@@ -366,13 +369,15 @@ def get_dummies(data: DataFrame, *args: str, drop_first=True, dtype="int") -> Da
|
|
|
366
369
|
DataFrame: 더미 변수로 변환된 데이터프레임
|
|
367
370
|
|
|
368
371
|
Examples:
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
372
|
+
```python
|
|
373
|
+
from hossam import *
|
|
374
|
+
# 전체 비숫자 컬럼 자동 변환
|
|
375
|
+
result = hs_prep.get_dummies(df)
|
|
376
|
+
# 특정 컬럼만 변환
|
|
377
|
+
result = hs_prep.get_dummies(df, 'cut', 'color', 'clarity')
|
|
378
|
+
# 옵션 지정
|
|
379
|
+
result = hs_prep.get_dummies(df, 'col1', drop_first=False, dtype='bool')
|
|
380
|
+
```
|
|
376
381
|
"""
|
|
377
382
|
if not args:
|
|
378
383
|
# args가 없으면 숫자 타입이 아닌 모든 컬럼 자동 선택
|
|
@@ -479,31 +484,35 @@ def bin_continuous(
|
|
|
479
484
|
DataFrame: 원본에 구간화된 명목형 컬럼이 추가된 데이터프레임
|
|
480
485
|
|
|
481
486
|
Examples:
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
487
|
+
```python
|
|
488
|
+
from hossam import *
|
|
489
|
+
|
|
490
|
+
# 동일 간격으로 5개 구간 생성 (숫자 인덱스):
|
|
491
|
+
df = pd.DataFrame({'age': [20, 35, 50, 65]})
|
|
492
|
+
result = hs_prep.bin_continuous(df, 'age', method='equal_width', bins=5)
|
|
493
|
+
print(result['age_bin']) # 0, 1, 2, ... (숫자 인덱스)
|
|
494
|
+
|
|
495
|
+
# 문자 레이블 사용:
|
|
496
|
+
result = hs_prep.bin_continuous(df, 'age', method='equal_width', bins=5, apply_labels=False)
|
|
497
|
+
print(result['age_bin']) # 20~30, 30~40, ... (문자 레이블)
|
|
498
|
+
|
|
499
|
+
# 생애주기 기반 구간화:
|
|
500
|
+
result = hs_prep.bin_continuous(df, 'age', method='lifecourse')
|
|
501
|
+
print(result['age_bin']) # 0, 1, 2, 3, 4 (숫자 인덱스)
|
|
502
|
+
|
|
503
|
+
# 생애주기 문자 레이블:
|
|
504
|
+
result = hs_prep.bin_continuous(df, 'age', method='lifecourse', apply_labels=False)
|
|
505
|
+
print(result['age_bin']) # 아동, 청소년, 청년, 중년, 노년
|
|
506
|
+
|
|
507
|
+
# 의료비 위험도 기반 연령대 (health_band):
|
|
508
|
+
result = hs_prep.bin_continuous(df, 'age', method='health_band', apply_labels=False)
|
|
509
|
+
print(result['age_bin']) # 18-29, 30-39, 40-49, 50-64, 65+
|
|
510
|
+
|
|
511
|
+
# 로그 변환된 컬럼 역변환 후 구간화:
|
|
512
|
+
df_log = pd.DataFrame({'charges_log': [np.log(1000), np.log(5000), np.log(50000)]})
|
|
513
|
+
result = hs_prep.bin_continuous(df_log, 'charges_log', method='equal_width', is_log_transformed=True)
|
|
514
|
+
print(result['charges_log_bin']) # 0, 1, 2 (숫자 인덱스)
|
|
515
|
+
```
|
|
507
516
|
"""
|
|
508
517
|
|
|
509
518
|
if field not in data.columns:
|
|
@@ -738,18 +747,19 @@ def log_transform(data: DataFrame, *fields: str) -> DataFrame:
|
|
|
738
747
|
DataFrame: 로그 변환된 데이터프레임.
|
|
739
748
|
|
|
740
749
|
Examples:
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
>>> df = pd.DataFrame({'x': [1, 10, 100], 'y': [2, 20, 200], 'z': ['a', 'b', 'c']})
|
|
746
|
-
>>> result = log_transform(df)
|
|
747
|
-
>>> print(result)
|
|
750
|
+
```python
|
|
751
|
+
from hossam import *
|
|
752
|
+
from pandas import DataFrame
|
|
753
|
+
df = DataFrame({'x': [1, 10, 100], 'y': [2, 20, 200], 'z': ['a', 'b', 'c']})
|
|
748
754
|
|
|
749
|
-
|
|
755
|
+
# 전체 수치형 컬럼에 대한 로그 변환:
|
|
756
|
+
result = hs_prep.log_transform(df)
|
|
757
|
+
print(result)
|
|
750
758
|
|
|
751
|
-
|
|
752
|
-
|
|
759
|
+
# 특정 컬럼만 변환:
|
|
760
|
+
result = hs_prep.log_transform(df, 'x', 'y')
|
|
761
|
+
print(result)
|
|
762
|
+
```
|
|
753
763
|
|
|
754
764
|
Notes:
|
|
755
765
|
- 수치형이 아닌 컬럼은 자동으로 제외됩니다.
|
|
@@ -794,39 +804,39 @@ def add_interaction(data: DataFrame, pairs: list[tuple[str, str]] | None = None)
|
|
|
794
804
|
"""데이터프레임에 상호작용(interaction) 항을 추가한다.
|
|
795
805
|
|
|
796
806
|
수치형 및 명목형 변수 간의 상호작용 항을 생성하여 데이터프레임에 추가한다.
|
|
797
|
-
-
|
|
798
|
-
-
|
|
799
|
-
-
|
|
807
|
+
- `수치형 * 수치형`: 두 변수의 곱셈 (col1*col2)
|
|
808
|
+
- `수치형 * 명목형`: 명목형의 각 카테고리별 수치형 변수 생성 (col1*col2_category)
|
|
809
|
+
- `명목형 * 명목형`: 두 명목형을 결합한 새 명목형 변수 생성 (col1_col2)
|
|
800
810
|
|
|
801
811
|
Args:
|
|
802
812
|
data (DataFrame): 원본 데이터프레임.
|
|
803
813
|
pairs (list[tuple[str, str]], optional): 직접 지정할 교호작용 쌍의 리스트.
|
|
804
|
-
|
|
805
|
-
|
|
814
|
+
예: [("age", "gender"), ("color", "cut")]
|
|
815
|
+
None이면 모든 수치형 컬럼의 2-way 상호작용을 생성.
|
|
806
816
|
|
|
807
817
|
Returns:
|
|
808
818
|
DataFrame: 상호작용 항이 추가된 새 데이터프레임.
|
|
809
819
|
|
|
810
820
|
Examples:
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
명목형끼리의 상호작용:
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
821
|
+
```python
|
|
822
|
+
from hossam import *
|
|
823
|
+
from padas import DataFrame
|
|
824
|
+
|
|
825
|
+
# 수치형 변수들의 상호작용:
|
|
826
|
+
df = DataFrame({'x1': [1, 2, 3], 'x2': [4, 5, 6]})
|
|
827
|
+
result = hs_prep.add_interaction(df)
|
|
828
|
+
print(result.columns) # x1, x2, x1*x2
|
|
829
|
+
|
|
830
|
+
# 수치형과 명목형의 상호작용:
|
|
831
|
+
df = DataFrame({'age': [20, 30, 40], 'gender': ['M', 'F', 'M']})
|
|
832
|
+
result = hs_prep.add_interaction(df, pairs=[('age', 'gender')])
|
|
833
|
+
print(result.columns) # age, gender, age*gender_M, age*gender_F
|
|
834
|
+
|
|
835
|
+
# 명목형끼리의 상호작용:
|
|
836
|
+
df = DataFrame({'color': ['R', 'G', 'B'], 'cut': ['A', 'B', 'A']})
|
|
837
|
+
result = hs_prep.add_interaction(df, pairs=[('color', 'cut')])
|
|
838
|
+
print(result.columns) # color, cut, color_cut
|
|
839
|
+
```
|
|
830
840
|
"""
|
|
831
841
|
df = data.copy()
|
|
832
842
|
|