diff --git a/doc/接口文档code.md b/doc/接口文档code.md index bdc4920..d56cfae 100644 --- a/doc/接口文档code.md +++ b/doc/接口文档code.md @@ -334,29 +334,33 @@ GET /api/metrics Response: { "status": "success", - "metrics": { + "metric": { "classification": [ { "name": "accuracy", "description": "准确率", - "range": [0, 1] + "range": [0, 1], + "interpretation": "值越大越好" }, { "name": "f1", "description": "F1分数", - "range": [0, 1] + "range": [0, 1], + "interpretation": "值越大越好" } ], "regression": [ { "name": "mse", "description": "均方误差", - "range": [0, null] + "range": [0, null], + "interpretation": "值越小越好" }, { "name": "mae", "description": "平均绝对误差", - "range": [0, null] + "range": [0, null], + "interpretation": "值越小越好" } ] } diff --git a/example_method_reader_metric.py b/example_method_reader_metric.py new file mode 100644 index 0000000..148a994 --- /dev/null +++ b/example_method_reader_metric.py @@ -0,0 +1,7 @@ + +from function.method_reader_metric import MethodReader + + + +method_reader = MethodReader() +print(method_reader.get_metrics()) \ No newline at end of file diff --git a/function/__pycache__/method_reader_metric.cpython-39.pyc b/function/__pycache__/method_reader_metric.cpython-39.pyc new file mode 100644 index 0000000..b54480e Binary files /dev/null and b/function/__pycache__/method_reader_metric.cpython-39.pyc differ diff --git a/function/method_reader_metric.py b/function/method_reader_metric.py new file mode 100644 index 0000000..bf43fea --- /dev/null +++ b/function/method_reader_metric.py @@ -0,0 +1,79 @@ +import yaml +from typing import Dict, List +import os +import logging +from pathlib import Path + +class MethodReader: + """方法配置读取器""" + + def __init__(self): + """初始化方法读取器""" + self.logger = logging.getLogger(__name__) + self.method_config = self._load_metrics() + + + def _load_metrics(self) -> Dict: + """加载方法配置文件""" + try: + config_path = Path('model/metrics.yaml') + if not config_path.exists(): + raise FileNotFoundError(f"Method config file not found at {config_path}") + + with open(config_path, 'r', encoding='utf-8') as f: + config = yaml.safe_load(f) + + self.logger.info("Successfully loaded method config") + return config + + except Exception as e: + self.logger.error(f"Error loading method config: {str(e)}") + raise + + + + def get_metrics(self) -> Dict: + """获取预处理方法列表""" + try: + metrics = [] + + # 分类方法 + classification_metrics = self.method_config.get('classification', {}) + if classification_metrics: + metrics.append({ + "name": "classification_metrics", + "description": "分类方法评价指标", + "metric": classification_metrics + }) + + # 回归方法 + regression_metrics = self.method_config.get('regression', {}) + if regression_metrics: + metrics.append({ + "name": "regression_metrics", + "description": "回归方法评价指标", + "metric": regression_metrics + }) + + # 聚类方法 + clustering_metrics = self.method_config.get('clustering', {}) + if clustering_metrics: + metrics.append({ + "name": "clustering_metrics", + "description": "聚类方法评价指标", + "metric": clustering_metrics + }) + + return { + "status": "success", + "metric": metrics + } + + except Exception as e: + self.logger.error(f"Error getting preprocessing methods: {str(e)}") + return { + "status": "error", + "error": str(e) + } + + \ No newline at end of file diff --git a/model/metrics.yaml b/model/metrics.yaml new file mode 100644 index 0000000..38847e0 --- /dev/null +++ b/model/metrics.yaml @@ -0,0 +1,57 @@ +classification: + - name: "accuracy" + description: "分类正确的样本占总样本数的比例。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "precision" + description: "预测为正类的样本中,真正类的比例。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "recall" + description: "真正类样本中被正确预测的比例。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "f1-score" + description: "精确率和召回率的调和平均值。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "roc_auc" + description: "ROC 曲线下的面积,衡量模型区分正负样本的能力。" + range: [0, 1] + interpretation: "值越大效果越好" + +regression: + - name: "mean_absolute_error" + description: "平均绝对误差,表示预测值与真实值之差的绝对值的均值。" + range: [0, +∞] + interpretation: "值越小效果越好" + - name: "mean_squared_error" + description: "均方误差,表示预测值与真实值之差的平方的均值。" + range: [0, +∞] + interpretation: "值越小效果越好" + - name: "r2_score" + description: "决定系数,表示模型解释数据方差的能力,1 表示完美拟合。" + range: [-∞, 1] + interpretation: "值越大效果越好" + - name: "explained_variance_score" + description: "解释方差,衡量预测数据与真实数据的方差相似程度。" + range: [0, 1] + interpretation: "值越大效果越好" + +clustering: + - name: "adjusted_rand_score" + description: "调整兰德指数,衡量聚类结果与真实标签的相似度。" + range: [-1, 1] + interpretation: "值越大效果越好" + - name: "homogeneity_score" + description: "同质性得分,衡量聚类的纯度,即每个聚类是否只包含单一类别的样本。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "completeness_score" + description: "完整性得分,衡量所有同类别样本是否被正确聚类到同一组。" + range: [0, 1] + interpretation: "值越大效果越好" + - name: "silhouette_score" + description: "轮廓系数,衡量样本在其簇内的紧密度和与其他簇的分离度。" + range: [-1, 1] + interpretation: "值越大效果越好"