staran 0.5.0__py3-none-any.whl → 0.6.1__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.
staran/__init__.py CHANGED
@@ -1,261 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
-
4
- """
5
- Staran - 简化的Python工具库
6
- =========================
7
-
8
- 基于Python标准库的轻量级实用工具集,提供日期处理、SQL生成等常用功能。
9
-
10
- 主要特性:
11
- - 零依赖:仅基于Python标准库
12
- - 智能格式:自动记忆输入格式并保持一致性
13
- - 直观API:符合Python习惯的设计
14
- - 类型安全:完整的参数验证
15
-
16
- 工具模块:
17
- ---------
18
- 1. Date工具 - 智能日期处理
19
- 2. SQL工具 - 数据分析SQL生成
20
-
21
-
22
- 快速开始:
23
- ---------
24
-
25
- Date工具基本用法::
26
-
27
- from staran import Date
28
-
29
- # 智能格式记忆
30
- date1 = Date('202504') # 输出: 202504 (年月格式)
31
- date2 = Date('20250415') # 输出: 20250415 (完整格式)
32
- date3 = Date(2025, 4, 15) # 输出: 2025-04-15
33
-
34
- # 格式保持
35
- new_date = date1.add_months(2) # 输出: 202506 (保持原格式)
36
-
37
- SQL工具基本用法::
38
-
39
- from staran import TableSchema, FeatureGenerator, SQLManager, DatabaseType
40
-
41
- # 创建SQL管理器
42
- sql_manager = SQLManager('my_database', DatabaseType.SPARK)
43
-
44
- # 定义表结构
45
- schema = TableSchema('user_behavior')
46
- schema.add_primary_key('user_id', 'string')
47
- schema.add_date_field('date', 'date')
48
- schema.add_field('amount', 'decimal', aggregatable=True)
49
- schema.add_field('status', 'string')
50
- schema.set_monthly_unique(True)
51
-
52
- # 创建特征生成器
53
- generator = FeatureGenerator(schema, sql_manager)
54
-
55
- # 按类型生成特征表
56
- table_name = generator.create_feature_table('aggregation', 2025, 7, 1)
57
- print(f"生成特征表: {table_name}")
58
-
59
- 特征类型控制::
60
-
61
- from staran.sql import FeatureConfig, FeatureType
62
-
63
- # 配置只生成基础特征
64
- config = FeatureConfig()
65
- config.enable_feature(FeatureType.MOM) # 启用环比特征
66
- config.set_mom_periods([1]) # 只生成1个月环比
67
-
68
- generator = FeatureGenerator(schema, sql_manager, config)
69
-
70
- # 分别生成不同类型的特征表
71
- agg_table = generator.create_feature_table('aggregation', 2025, 7, 1)
72
- mom_table = generator.create_feature_table('mom', 2025, 7, 2)
73
-
74
- 多种输出格式::
75
-
76
- date = Date('202504')
77
-
78
- print(date) # 202504 (默认格式)
79
- print(date.format_full()) # 2025-04-01
80
- print(date.format_chinese()) # 2025年04月01日
81
- print(date.format_compact()) # 20250401
82
-
83
- SQL特征生成::
84
-
85
- # 配置特征生成
86
- from staran.sql import FeatureConfig
87
-
88
- config = FeatureConfig()
89
- config.set_aggregation_types(['sum', 'avg', 'max'])
90
- config.set_mom_months([1, 3]) # 1个月、3个月环比
91
-
92
- generator = FeatureGenerator(schema, config)
93
- generator.print_feature_summary()
94
-
95
- 日期运算::
96
-
97
- date = Date(2025, 4, 15)
98
-
99
- # 日期运算
100
- tomorrow = date.add_days(1) # 2025-04-16
101
- next_month = date.add_months(1) # 2025-05-15
102
-
103
- # 日期比较
104
- other = Date(2025, 4, 20)
105
- diff = other.difference(date) # 5 (天数差)
106
- print(date < other) # True
107
-
108
- 创建方式::
109
-
110
- # 多种创建方式
111
- today = Date() # 今日
112
- birthday = Date(1990, 5, 15) # 指定日期
113
- event = Date('20240615') # 从字符串
114
- deadline = Date(year=2024, month=12, day=31) # 关键字参数
115
-
116
- 格式化选项::
117
-
118
- date = Date('202504')
119
-
120
- # 常用格式
121
- date.format_default() # 202504 (默认)
122
- date.format_full() # 2025-04-01 (完整)
123
- date.format_year_month() # 2025-04 (年月)
124
- date.format_chinese() # 2025年04月01日 (中文)
125
- date.format_iso() # 2025-04-01 (ISO)
126
- date.format_us() # 04/01/2025 (美式)
127
- date.format_european() # 01/04/2025 (欧式)
128
-
129
- 核心功能:
130
- ---------
131
- Date工具:
132
- - Date: 智能日期类,支持格式记忆和多种输出
133
- - 日期运算:add_days(), add_months()
134
- - 格式化:多种预设格式方法
135
- - 比较:支持标准比较操作符
136
- - 转换:to_timestamp(), to_date(), to_datetime()
137
- - 信息:is_leap_year(), weekday(), days_in_month()
138
-
139
- SQL工具:
140
- - TableSchema: 表结构定义,支持字段类型和特性配置
141
- - FeatureGenerator: 特征生成器,支持聚合、环比、同比特征
142
- - SparkSQLGenerator: Spark SQL代码生成
143
- - 支持每人每月唯一数据的特征工程
144
- - 可配置的聚合类型和时间窗口
145
-
146
- 版本信息:
147
- ---------
148
- - 版本: 1.0.0
149
- - 许可: MIT
150
- - 作者: Staran Team
151
- """
152
-
153
- # 导入主要功能
154
- from .tools import Date
155
- from .features import (
156
- TableSchema, FeatureGenerator, FeatureManager, FeatureTableManager,
157
- FeatureConfig, DatabaseType, FeatureType, Field, FieldType
158
- )
159
- from .engines import SparkEngine, HiveEngine, TuringEngine, BaseEngine, create_engine, create_turing_engine
160
-
161
- # Examples模块 - 业务示例
162
- from .examples import create_aum_example, run_aum_example
163
-
164
- # Schemas模块 - 表结构定义与文档生成
165
- from .schemas import SchemaDocumentGenerator
166
- from .schemas.aum import get_aum_schemas, export_aum_docs
167
-
168
- # 图灵平台引擎 (可选导入,避免依赖问题)
169
- try:
170
- from .features import quick_create_and_download
171
- _TURING_AVAILABLE = True
172
- except ImportError:
173
- quick_create_and_download = None
174
- _TURING_AVAILABLE = False
175
-
176
- # 向后兼容
177
- SQLManager = FeatureManager
178
- SparkSQLGenerator = SparkEngine # 向后兼容
179
-
180
- # 主要导出
181
- __all__ = [
182
- 'Date',
183
- 'TableSchema',
184
- 'FeatureGenerator',
185
- 'FeatureConfig',
186
- 'FeatureType',
187
- 'Field',
188
- 'FieldType',
189
- 'FeatureManager',
190
- 'FeatureTableManager',
191
- 'DatabaseType',
192
- 'BaseEngine',
193
- 'SparkEngine',
194
- 'HiveEngine',
195
- 'TuringEngine',
196
- 'create_engine',
197
- 'create_turing_engine',
198
- # 示例模块
199
- 'create_aum_example',
200
- 'run_aum_example',
201
- # 表结构模块
202
- 'SchemaDocumentGenerator',
203
- 'get_aum_schemas',
204
- 'export_aum_docs',
205
- # 向后兼容
206
- 'SQLManager',
207
- 'SparkSQLGenerator'
208
- ]
209
-
210
- # 如果图灵引擎可用,添加到导出列表
211
- if _TURING_AVAILABLE:
212
- __all__.extend([
213
- 'quick_create_and_download'
214
- ])
215
-
216
- # 包信息
217
- __version__ = '0.3.0'
218
- __author__ = 'Staran Team'
219
- __description__ = 'Smart feature engineering toolkit with schema management, document generation and business examples'
220
- __license__ = 'MIT'
221
-
222
- # 便捷函数示例
223
- def help_examples():
224
- """
225
- 显示Staran使用示例
226
-
227
- Returns:
228
- None: 打印示例到控制台
229
- """
230
- print("""
231
- Staran 使用示例
232
- ===============
233
-
234
- 1. 基本创建:
235
- from staran import Date
236
-
237
- date1 = Date('202504') # 202504
238
- date2 = Date('20250415') # 20250415
239
- date3 = Date(2025, 4, 15) # 2025-04-15
240
-
241
- 2. 格式保持:
242
- date = Date('202504')
243
- new_date = date.add_months(2) # 202506 (保持格式)
244
-
245
- 3. 多种格式:
246
- date.format_full() # 2025-04-01
247
- date.format_chinese() # 2025年04月01日
248
- date.format_compact() # 20250401
249
-
250
- 4. 日期运算:
251
- date.add_days(30) # 增加30天
252
- date.add_months(2) # 增加2个月
253
- date.difference(other) # 计算天数差
254
-
255
- 更多信息请查看: help(Date)
256
- """)
257
-
258
- # 快捷访问帮助
259
- def examples():
260
- """显示使用示例的快捷方法"""
261
- help_examples()
@@ -0,0 +1,30 @@
1
+ """
2
+ staran.banks - 银行配置模块
3
+
4
+ 该模块包含不同银行的特定配置,包括:
5
+ - 数据库连接配置
6
+ - 表结构定义
7
+ - 业务规则设置
8
+ - 模型配置
9
+
10
+ 支持的银行:
11
+ - xinjiang_icbc: 新疆工行配置
12
+
13
+ 版本: 0.6.0
14
+ """
15
+
16
+ from .xinjiang_icbc import (
17
+ XinjiangICBCConfig,
18
+ get_xinjiang_icbc_tables,
19
+ get_xinjiang_icbc_models,
20
+ xinjiang_icbc_config
21
+ )
22
+
23
+ __all__ = [
24
+ 'XinjiangICBCConfig',
25
+ 'xinjiang_icbc_config',
26
+ 'get_xinjiang_icbc_tables',
27
+ 'get_xinjiang_icbc_models'
28
+ ]
29
+
30
+ __version__ = "0.6.0"
@@ -0,0 +1,90 @@
1
+ """
2
+ 新疆工行银行配置模块
3
+
4
+ 专门针对新疆工行代发长尾客户的配置:
5
+ - 数据库表结构定义(代发长尾客户专用)
6
+ - 业务规则配置
7
+ - 模型配置(提升模型和防流失模型)
8
+
9
+ 数据库: xinjiang_icbc_daifa_longtail
10
+ 业务范围: 代发长尾客户
11
+ """
12
+
13
+ from dataclasses import dataclass
14
+ from typing import Dict, List, Optional
15
+ from datetime import datetime
16
+
17
+
18
+ @dataclass
19
+ class XinjiangICBCConfig:
20
+ """新疆工行配置类"""
21
+
22
+ # 数据库配置
23
+ database_name: str = "xinjiang_icbc_daifa_longtail"
24
+ schema_name: str = "daifa_longtail"
25
+
26
+ # 业务配置
27
+ business_domain: str = "代发长尾客户"
28
+ customer_segment: str = "代发长尾"
29
+
30
+ # 模型配置
31
+ available_models: List[str] = None
32
+
33
+ # 业务规则
34
+ longtail_asset_min: float = 10000 # 长尾客户最小资产
35
+ longtail_asset_max: float = 100000 # 长尾客户最大资产
36
+ upgrade_target: float = 3000 # 提升目标金额
37
+ churn_threshold: float = 1500 # 流失阈值金额
38
+
39
+ def __post_init__(self):
40
+ if self.available_models is None:
41
+ self.available_models = [
42
+ "daifa_longtail_upgrade_3k", # 代发长尾提升3k模型
43
+ "daifa_longtail_churn_1_5k" # 代发长尾防流失1.5k模型
44
+ ]
45
+
46
+
47
+ def get_xinjiang_icbc_tables() -> Dict[str, str]:
48
+ """获取新疆工行代发长尾客户表配置"""
49
+ return {
50
+ # 代发长尾客户行为表
51
+ "daifa_longtail_behavior": "xinjiang_icbc_daifa_hlwj_dfcw_f1_f4_wy",
52
+
53
+ # 代发长尾客户资产平均表
54
+ "daifa_longtail_asset_avg": "xinjiang_icbc_daifa_hlwj_zi_chan_avg_wy",
55
+
56
+ # 代发长尾客户资产配置表
57
+ "daifa_longtail_asset_config": "xinjiang_icbc_daifa_hlwj_zi_chan_config_wy",
58
+
59
+ # 代发长尾客户月度统计表
60
+ "daifa_longtail_monthly_stat": "xinjiang_icbc_daifa_hlwj_monthly_stat_wy"
61
+ }
62
+
63
+
64
+ def get_xinjiang_icbc_models() -> Dict[str, Dict]:
65
+ """获取新疆工行代发长尾客户模型配置"""
66
+ return {
67
+ "daifa_longtail_upgrade_3k": {
68
+ "name": "代发长尾客户提升3k预测模型",
69
+ "description": "预测下个月代发长尾客户资产提升3000元的概率",
70
+ "target": "upgrade_3k_next_month",
71
+ "model_type": "binary_classification",
72
+ "business_objective": "识别有潜力提升资产的代发长尾客户",
73
+ "target_threshold": 3000,
74
+ "prediction_window": "1_month"
75
+ },
76
+
77
+ "daifa_longtail_churn_1_5k": {
78
+ "name": "代发长尾客户防流失1.5k预测模型",
79
+ "description": "预测下个月代发长尾客户流失1500元资产的风险",
80
+ "target": "churn_1_5k_next_month",
81
+ "model_type": "binary_classification",
82
+ "business_objective": "识别有流失风险的代发长尾客户",
83
+ "target_threshold": 1500,
84
+ "prediction_window": "1_month"
85
+ }
86
+ }
87
+
88
+
89
+ # 创建默认配置实例
90
+ xinjiang_icbc_config = XinjiangICBCConfig()
staran/models/__init__.py CHANGED
@@ -1,81 +1,72 @@
1
1
  """
2
- Staran Models Module - v0.5.0
2
+ staran.models v0.6.0 - 新疆工行代发长尾客户模型管理
3
3
 
4
- 专业的机器学习模型配置和管理模块,提供:
5
- - 模型配置管理 (ModelConfig)
6
- - 目标变量定义 (TargetDefinition)
7
- - 银行特定配置支持
8
- - SQL驱动的target生成
9
- - 模型部署和版本管理
4
+ 专门针对新疆工行代发长尾客户的两个核心模型:
5
+ 1. 代发长尾客户提升3k预测模型 (daifa_longtail_upgrade_3k)
6
+ 2. 代发长尾客户防流失1.5k预测模型 (daifa_longtail_churn_1_5k)
10
7
 
11
- 支持的模型类型:
12
- - 分类模型 (Classification)
13
- - 回归模型 (Regression)
14
- - 聚类模型 (Clustering)
15
- - 时间序列模型 (TimeSeries)
16
-
17
- 支持的银行:
18
- - 工商银行 (ICBC)
19
- - 通用配置 (Generic)
8
+ 主要功能:
9
+ - 模型配置管理
10
+ - SQL驱动的目标变量定义
11
+ - 模型注册和版本控制
12
+ - 新疆工行特定配置
20
13
  """
21
14
 
22
- from .config import ModelConfig, ModelType, create_model_config
23
- from .target import TargetDefinition, TargetType, create_target_definition
24
- from .registry import ModelRegistry, register_model, get_model_config, save_model_registry
25
- from .bank_configs import BankConfig, get_bank_config, register_bank_config
15
+ from .config import ModelConfig, create_model_config
16
+ from .target import TargetDefinition, create_target_definition
17
+ from .registry import ModelRegistry, register_model, save_model_registry
18
+ from .daifa_models import (
19
+ create_daifa_longtail_upgrade_model,
20
+ create_daifa_longtail_churn_model,
21
+ get_available_daifa_models,
22
+ create_both_daifa_models
23
+ )
24
+
25
+ # 便捷函数
26
+ def create_xinjiang_icbc_models(output_dir: str = "./xinjiang_models") -> dict:
27
+ """为新疆工行创建两个代发长尾客户模型"""
28
+ return create_both_daifa_models(output_dir)
26
29
 
27
- # 版本信息
28
- __version__ = "0.5.0"
30
+ def list_available_models() -> list:
31
+ """列出所有可用的代发长尾客户模型"""
32
+ return get_available_daifa_models()
33
+
34
+ def get_model_summary() -> dict:
35
+ """获取模型概述信息"""
36
+ return {
37
+ "version": "0.6.0",
38
+ "bank": "新疆工行",
39
+ "business_domain": "代发长尾客户",
40
+ "models": [
41
+ {
42
+ "name": "daifa_longtail_upgrade_3k",
43
+ "description": "预测下个月代发长尾客户资产提升3k的概率",
44
+ "target_amount": 3000,
45
+ "model_type": "binary_classification"
46
+ },
47
+ {
48
+ "name": "daifa_longtail_churn_1_5k",
49
+ "description": "预测下个月代发长尾客户流失1.5k资产的风险",
50
+ "target_amount": 1500,
51
+ "model_type": "binary_classification"
52
+ }
53
+ ]
54
+ }
29
55
 
30
- # 主要导出
31
56
  __all__ = [
32
- # 模型配置
33
- 'ModelConfig',
34
- 'ModelType',
35
- 'create_model_config',
57
+ # 核心组件
58
+ 'ModelConfig', 'TargetDefinition', 'ModelRegistry',
36
59
 
37
- # 目标定义
38
- 'TargetDefinition',
39
- 'TargetType',
40
- 'create_target_definition',
60
+ # 创建函数
61
+ 'create_model_config', 'create_target_definition', 'register_model',
41
62
 
42
- # 模型注册
43
- 'ModelRegistry',
44
- 'register_model',
45
- 'get_model_config',
46
- 'save_model_registry',
63
+ # 代发长尾模型
64
+ 'create_daifa_longtail_upgrade_model', 'create_daifa_longtail_churn_model',
65
+ 'create_both_daifa_models', 'get_available_daifa_models',
47
66
 
48
- # 银行配置
49
- 'BankConfig',
50
- 'get_bank_config',
51
- 'register_bank_config',
67
+ # 便捷函数
68
+ 'create_xinjiang_icbc_models', 'list_available_models', 'get_model_summary',
69
+ 'save_model_registry'
52
70
  ]
53
71
 
54
- # 便捷函数
55
- def create_icbc_model(model_name: str, model_type: str, target_sql: str, algorithm: str = "random_forest", **kwargs):
56
- """创建工商银行专用模型配置的便捷函数"""
57
- bank_config = get_bank_config('icbc')
58
- model_config = create_model_config(
59
- name=model_name,
60
- model_type=model_type,
61
- algorithm=algorithm,
62
- bank_code="icbc",
63
- **kwargs
64
- )
65
-
66
- target_config = create_target_definition(
67
- name=f"{model_name}_target",
68
- target_type="sql_based",
69
- sql_query=target_sql,
70
- bank_code="icbc"
71
- )
72
-
73
- return register_model(model_config, target_config)
74
-
75
- def list_available_models():
76
- """列出所有可用的模型配置"""
77
- return ModelRegistry.list_models()
78
-
79
- def get_model_summary(model_name: str):
80
- """获取模型配置摘要"""
81
- return ModelRegistry.get_model_summary(model_name)
72
+ __version__ = "0.6.0"