staran 0.2.4__py3-none-any.whl → 0.4.0__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.
@@ -0,0 +1,250 @@
1
+ """
2
+ AUM代发长尾模型示例
3
+ 基于Staran v0.3.0架构,使用schemas模块的预定义表结构
4
+ """
5
+
6
+ from typing import Dict, Optional
7
+ from ..engines import create_turing_engine
8
+ from ..features import FeatureManager, FeatureConfig, FeatureType
9
+ from ..tools import Date
10
+ from ..schemas.aum import get_aum_schemas
11
+
12
+
13
+ class AUMLongtailExample:
14
+ """AUM代发长尾模型示例类"""
15
+
16
+ def __init__(self, database: str = "dwegdata03000"):
17
+ """
18
+ 初始化AUM长尾模型示例
19
+
20
+ Args:
21
+ database: 数据库名称,默认为dwegdata03000
22
+ """
23
+ self.database = database
24
+ self.engine = create_turing_engine(database)
25
+ self.schemas = get_aum_schemas() # 从schemas模块获取预定义的表结构
26
+
27
+ def run(self, feature_date: Optional[str] = None, output_path: str = "file:///nfsHome/aum_longtail") -> Dict:
28
+ """
29
+ 运行完整的AUM长尾模型特征工程
30
+
31
+ Args:
32
+ feature_date: 特征日期,格式为YYYYMM,默认为当前月
33
+ output_path: 输出路径,默认为file:///nfsHome/aum_longtail
34
+
35
+ Returns:
36
+ 包含所有结果的字典
37
+ """
38
+ if feature_date is None:
39
+ feature_date = Date.today().format_compact()[:6]
40
+
41
+ print(f"🚀 开始AUM长尾模型特征工程 - {feature_date}")
42
+ print("="*60)
43
+
44
+ results = {}
45
+
46
+ # 步骤1: 生成A表特征(行为特征表)- 只生成原始拷贝和聚合特征
47
+ print("📊 步骤1: 生成客户行为特征...")
48
+ results['behavior'] = self._generate_behavior_features('behavior', feature_date)
49
+
50
+ # 步骤2: 生成B表特征(资产平均值表)- 完整特征
51
+ print("💰 步骤2: 生成资产平均值特征...")
52
+ results['asset_avg'] = self._generate_full_features('asset_avg', feature_date)
53
+
54
+ # 步骤3: 生成C表特征(资产配置表)- 完整特征
55
+ print("📈 步骤3: 生成资产配置特征...")
56
+ results['asset_config'] = self._generate_full_features('asset_config', feature_date)
57
+
58
+ # 步骤4: 生成D表特征(月度统计表)- 完整特征
59
+ print("📋 步骤4: 生成月度统计特征...")
60
+ results['monthly_stat'] = self._generate_full_features('monthly_stat', feature_date)
61
+
62
+ # 步骤5: 导出特征表
63
+ print("💾 步骤5: 导出特征表...")
64
+ results['exports'] = self._export_features(feature_date, output_path)
65
+
66
+ print("="*60)
67
+ print("✅ AUM长尾模型特征工程完成!")
68
+ return results
69
+
70
+ def _generate_behavior_features(self, table_type: str, feature_date: str) -> Dict:
71
+ """生成行为特征(A表)- 只生成原始拷贝和聚合特征"""
72
+ schema = self.schemas[table_type]
73
+ manager = FeatureManager(self.engine, self.database)
74
+
75
+ # A表特征配置:只启用原始拷贝和聚合
76
+ config = FeatureConfig()
77
+ config.enable_feature(FeatureType.RAW_COPY)
78
+ config.enable_feature(FeatureType.AGGREGATION)
79
+ config.disable_feature(FeatureType.MOM) # 不生成环比
80
+ config.disable_feature(FeatureType.YOY) # 不生成同比
81
+
82
+ print(f" 🔧 生成{schema.table_name}的特征...")
83
+ result = manager.generate_features(
84
+ schema=schema,
85
+ config=config,
86
+ feature_date=feature_date
87
+ )
88
+
89
+ feature_count = manager.count_features(schema, config)
90
+ print(f" ✅ A表特征生成完成: {feature_count}个特征")
91
+ return result
92
+
93
+ def _generate_full_features(self, table_type: str, feature_date: str) -> Dict:
94
+ """生成完整特征(B、C、D表)- 聚合+5个月环比+1年同比"""
95
+ schema = self.schemas[table_type]
96
+ manager = FeatureManager(self.engine, self.database)
97
+
98
+ # B、C、D表特征配置:完整特征集
99
+ config = FeatureConfig()
100
+ config.enable_feature(FeatureType.AGGREGATION)
101
+ config.enable_feature(FeatureType.MOM, mom_windows=[5]) # 5个月环比
102
+ config.enable_feature(FeatureType.YOY, yoy_windows=[12]) # 1年同比
103
+
104
+ print(f" 🔧 生成{schema.table_name}的特征...")
105
+ result = manager.generate_features(
106
+ schema=schema,
107
+ config=config,
108
+ feature_date=feature_date
109
+ )
110
+
111
+ feature_count = manager.count_features(schema, config)
112
+ print(f" ✅ {table_type}表特征生成完成: {feature_count}个特征")
113
+ return result
114
+
115
+ def _export_features(self, feature_date: str, output_path: str) -> Dict:
116
+ """导出所有特征表到指定路径"""
117
+ file_prefixes = {
118
+ 'behavior': 'aum_behavior_features',
119
+ 'asset_avg': 'aum_asset_avg_features',
120
+ 'asset_config': 'aum_asset_config_features',
121
+ 'monthly_stat': 'monthly_stat_features'
122
+ }
123
+
124
+ results = {}
125
+ for table_type, file_prefix in file_prefixes.items():
126
+ print(f" 💾 导出{table_type}表...")
127
+
128
+ # 构建特征表名
129
+ table_name = f"{self.schemas[table_type].table_name}_{feature_date}_f001"
130
+
131
+ result = self.engine.download_table_data(
132
+ table_name=f"{self.database}.{table_name}",
133
+ output_path=f"{output_path}/{file_prefix}_{feature_date}.parquet",
134
+ mode="cluster"
135
+ )
136
+
137
+ results[table_type] = result
138
+ print(f" ✅ 导出 {table_type}: {result.get('status', 'unknown')}")
139
+
140
+ return results
141
+
142
+ def get_summary(self) -> Dict:
143
+ """获取示例摘要信息"""
144
+ summary = {
145
+ 'database': self.database,
146
+ 'tables': {},
147
+ 'total_features': 0
148
+ }
149
+
150
+ for table_type, schema in self.schemas.items():
151
+ try:
152
+ manager = FeatureManager(self.engine, self.database)
153
+
154
+ if table_type == 'behavior':
155
+ # A表只有原始拷贝和聚合特征
156
+ config = FeatureConfig()
157
+ config.enable_feature(FeatureType.RAW_COPY)
158
+ config.enable_feature(FeatureType.AGGREGATION)
159
+ config.disable_feature(FeatureType.MOM)
160
+ config.disable_feature(FeatureType.YOY)
161
+ else:
162
+ # B、C、D表包含完整特征:聚合+5个月MoM+1年YoY
163
+ config = FeatureConfig()
164
+ config.enable_feature(FeatureType.AGGREGATION, mom_windows=[5], yoy_windows=[12])
165
+
166
+ feature_count = manager.count_features(schema, config)
167
+ summary['tables'][table_type] = {
168
+ 'table_name': schema.table_name,
169
+ 'field_count': len(schema.fields),
170
+ 'feature_count': feature_count,
171
+ 'features': {
172
+ 'total': feature_count,
173
+ 'aggregation': len(schema.fields), # 估算
174
+ 'mom': len(schema.fields) * 5 if table_type != 'behavior' else 0,
175
+ 'yoy': len(schema.fields) * 1 if table_type != 'behavior' else 0
176
+ }
177
+ }
178
+ summary['total_features'] += feature_count
179
+ except Exception as e:
180
+ # 在模拟模式下返回预估数量
181
+ base_fields = len(schema.fields)
182
+ if table_type == 'behavior':
183
+ estimated_features = base_fields * 2 # 原始拷贝 + 聚合
184
+ agg_count = base_fields
185
+ mom_count = 0
186
+ yoy_count = 0
187
+ else:
188
+ estimated_features = base_fields * 8 # 聚合 + MoM + YoY 组合
189
+ agg_count = base_fields
190
+ mom_count = base_fields * 5
191
+ yoy_count = base_fields * 1
192
+
193
+ summary['tables'][table_type] = {
194
+ 'table_name': schema.table_name,
195
+ 'field_count': base_fields,
196
+ 'feature_count': estimated_features,
197
+ 'mode': 'estimated',
198
+ 'features': {
199
+ 'total': estimated_features,
200
+ 'aggregation': agg_count,
201
+ 'mom': mom_count,
202
+ 'yoy': yoy_count
203
+ }
204
+ }
205
+ summary['total_features'] += estimated_features
206
+
207
+ return summary
208
+
209
+
210
+ # 简化的API函数
211
+ def create_aum_example(database: str = "dwegdata03000") -> AUMLongtailExample:
212
+ """
213
+ 一键创建AUM长尾模型示例
214
+
215
+ Args:
216
+ database: 数据库名称,默认为dwegdata03000
217
+
218
+ Returns:
219
+ AUMLongtailExample实例
220
+ """
221
+ return AUMLongtailExample(database)
222
+
223
+
224
+ def run_aum_example(feature_date: Optional[str] = None,
225
+ database: str = "dwegdata03000",
226
+ output_path: str = "file:///nfsHome/aum_longtail") -> Dict:
227
+ """
228
+ 一键运行AUM长尾模型特征工程
229
+
230
+ Args:
231
+ feature_date: 特征日期,格式为YYYYMM,默认为当前月
232
+ database: 数据库名称,默认为dwegdata03000
233
+ output_path: 输出路径,默认为file:///nfsHome/aum_longtail
234
+
235
+ Returns:
236
+ 包含所有结果的字典
237
+
238
+ Example:
239
+ >>> results = run_aum_example('202507')
240
+ >>> print(f"生成特征数: {len(results)}")
241
+ """
242
+ example = create_aum_example(database)
243
+ return example.run(feature_date, output_path)
244
+
245
+
246
+ __all__ = [
247
+ 'AUMLongtailExample',
248
+ 'create_aum_example',
249
+ 'run_aum_example'
250
+ ]