🎯 项目成果: - 准确率: 53.10% (2026年测试集) - 任务类型: 二分类 (预测涨/跌) - 特征: OHLCV + RSI + MACD (9个特征) - 训练数据: 2020-2025年BTC小时数据 (52,351序列) - 测试数据: 2026年1-29日 (484序列) 📊 性能指标: - 精确率: 51.79% - 召回率: 49.36% - F1分数: 0.5054 - 预测分布: 非常平衡 (偏差仅-11个) 🔬 实验历史: - 尝试了5种方案 (回归/分类, 不同数据划分) - 分类任务优于回归任务 (53.10% vs 49.5%) - 全量训练避免了过拟合问题 📦 主要模型文件: - models/checkpoints/dlinear_classification_2026/ - best_model.pt (最佳模型) - preprocessor.pkl (预处理器) - test_2026_results.json (测试结果) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
"""评估已训练的DLinear模型"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import torch
|
|
import json
|
|
from data.fetcher import BinanceDataFetcher
|
|
from data.preprocessor import CryptoPreprocessor, split_data
|
|
from data.dataset import create_dataloaders
|
|
from models.dlinear.model import DLinear
|
|
from training.evaluator import evaluate_model
|
|
|
|
def main():
|
|
print("="*80)
|
|
print("评估DLinear模型(真实数据)")
|
|
print("="*80)
|
|
|
|
# 加载数据
|
|
print("\n加载数据...")
|
|
fetcher = BinanceDataFetcher()
|
|
df = fetcher.fetch_ohlcv('BTC', '1h', use_cache=True)
|
|
|
|
preprocessor = CryptoPreprocessor(lookback=168, forecast_horizon=24)
|
|
X, y, _ = preprocessor.preprocess_pipeline(df, task='classification', fit=True)
|
|
|
|
train_data, val_data, test_data = split_data(X, y)
|
|
_, _, test_loader = create_dataloaders(
|
|
train_data, val_data, test_data,
|
|
task='classification', batch_size=64
|
|
)
|
|
|
|
# 加载模型
|
|
print("加载最佳模型...")
|
|
model_path = './models/checkpoints/dlinear_btc_1h_real/best_model.pt'
|
|
checkpoint = torch.load(model_path, map_location='cpu')
|
|
model = DLinear(checkpoint['config'])
|
|
model.load_state_dict(checkpoint['model_state_dict'])
|
|
|
|
# 评估
|
|
print("\n在测试集上评估...")
|
|
device = 'mps' if torch.backends.mps.is_available() else 'cpu'
|
|
metrics = evaluate_model(model, test_loader, 'classification', device)
|
|
|
|
# 保存结果
|
|
results_path = './models/checkpoints/dlinear_btc_1h_real/test_metrics.json'
|
|
with open(results_path, 'w') as f:
|
|
json.dump(metrics, f, indent=2)
|
|
|
|
# 打印结果
|
|
print("\n" + "="*80)
|
|
print("测试集评估结果")
|
|
print("="*80)
|
|
print(f"\n📊 分类指标:")
|
|
print(f" 准确率: {metrics.get('accuracy', 0):.4f}")
|
|
print(f" F1分数: {metrics.get('f1', 0):.4f}")
|
|
print(f" 精确率: {metrics.get('precision', 0):.4f}")
|
|
print(f" 召回率: {metrics.get('recall', 0):.4f}")
|
|
|
|
if 'true_positives' in metrics:
|
|
print(f"\n📈 混淆矩阵:")
|
|
print(f" 真阳性: {metrics['true_positives']}")
|
|
print(f" 真阴性: {metrics['true_negatives']}")
|
|
print(f" 假阳性: {metrics['false_positives']}")
|
|
print(f" 假阴性: {metrics['false_negatives']}")
|
|
|
|
print(f"\n💰 回测结果:")
|
|
print(f" 总收益率: {metrics.get('backtest_total_return', 0)*100:.2f}%")
|
|
print(f" 最终资金: ${metrics.get('backtest_final_capital', 0):,.2f}")
|
|
print(f" 夏普比率: {metrics.get('backtest_sharpe_ratio', 0):.4f}")
|
|
print(f" 最大回撤: {metrics.get('backtest_max_drawdown', 0)*100:.2f}%")
|
|
print(f" 胜率: {metrics.get('backtest_win_rate', 0)*100:.2f}%")
|
|
print(f" 交易次数: {metrics.get('backtest_num_trades', 0)}")
|
|
|
|
print("\n" + "="*80)
|
|
print(f"✓ 结果已保存至:{results_path}")
|
|
print("="*80)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|