import streamlit as st
|
import plotly.express as px
|
import plotly.graph_objects as go
|
import pandas as pd
|
import numpy as np
|
import joblib
|
import os
|
from datetime import datetime, timedelta
|
from app.services.extruder_service import ExtruderService
|
from app.services.main_process_service import MainProcessService
|
from sklearn.linear_model import LinearRegression
|
from sklearn.model_selection import train_test_split
|
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
|
|
|
# 导入稳态识别功能
|
class SteadyStateDetector:
|
def __init__(self):
|
pass
|
|
def detect_steady_state(self, df, weight_col='米重', window_size=20, std_threshold=0.5, duration_threshold=60):
|
"""
|
稳态识别逻辑:标记米重数据中的稳态段
|
:param df: 包含米重数据的数据框
|
:param weight_col: 米重列名
|
:param window_size: 滑动窗口大小(秒)
|
:param std_threshold: 标准差阈值
|
:param duration_threshold: 稳态持续时间阈值(秒)
|
:return: 包含稳态标记的数据框和稳态信息
|
"""
|
if df is None or df.empty:
|
return df, []
|
|
# 确保时间列是datetime类型
|
df['time'] = pd.to_datetime(df['time'])
|
|
# 计算滚动统计量
|
df['rolling_std'] = df[weight_col].rolling(window=window_size, min_periods=5).std()
|
df['rolling_mean'] = df[weight_col].rolling(window=window_size, min_periods=5).mean()
|
|
# 计算波动范围
|
df['fluctuation_range'] = (df['rolling_std'] / df['rolling_mean']) * 100
|
df['fluctuation_range'] = df['fluctuation_range'].fillna(0)
|
|
# 标记稳态点
|
df['is_steady'] = 0
|
steady_condition = (
|
(df['fluctuation_range'] < std_threshold) &
|
(df[weight_col] >= 0.1)
|
)
|
df.loc[steady_condition, 'is_steady'] = 1
|
|
# 识别连续稳态段
|
steady_segments = []
|
current_segment = {}
|
|
for i, row in df.iterrows():
|
if row['is_steady'] == 1:
|
if not current_segment:
|
current_segment = {
|
'start_time': row['time'],
|
'start_idx': i,
|
'weights': [row[weight_col]]
|
}
|
else:
|
current_segment['weights'].append(row[weight_col])
|
else:
|
if current_segment:
|
current_segment['end_time'] = df.loc[i-1, 'time'] if i > 0 else df.loc[i, 'time']
|
current_segment['end_idx'] = i-1
|
duration = (current_segment['end_time'] - current_segment['start_time']).total_seconds()
|
|
if duration >= duration_threshold:
|
weights_array = np.array(current_segment['weights'])
|
current_segment['duration'] = duration
|
current_segment['mean_weight'] = np.mean(weights_array)
|
current_segment['std_weight'] = np.std(weights_array)
|
current_segment['min_weight'] = np.min(weights_array)
|
current_segment['max_weight'] = np.max(weights_array)
|
current_segment['fluctuation_range'] = (current_segment['std_weight'] / current_segment['mean_weight']) * 100
|
|
# 计算置信度
|
confidence = 100 - (current_segment['fluctuation_range'] / std_threshold) * 50
|
confidence = max(50, min(100, confidence))
|
current_segment['confidence'] = confidence
|
|
steady_segments.append(current_segment)
|
|
current_segment = {}
|
|
# 处理最后一个稳态段
|
if current_segment:
|
current_segment['end_time'] = df['time'].iloc[-1]
|
current_segment['end_idx'] = len(df) - 1
|
duration = (current_segment['end_time'] - current_segment['start_time']).total_seconds()
|
|
if duration >= duration_threshold:
|
weights_array = np.array(current_segment['weights'])
|
current_segment['duration'] = duration
|
current_segment['mean_weight'] = np.mean(weights_array)
|
current_segment['std_weight'] = np.std(weights_array)
|
current_segment['min_weight'] = np.min(weights_array)
|
current_segment['max_weight'] = np.max(weights_array)
|
current_segment['fluctuation_range'] = (current_segment['std_weight'] / current_segment['mean_weight']) * 100
|
|
confidence = 100 - (current_segment['fluctuation_range'] / std_threshold) * 50
|
confidence = max(50, min(100, confidence))
|
current_segment['confidence'] = confidence
|
|
steady_segments.append(current_segment)
|
|
# 在数据框中标记完整的稳态段
|
for segment in steady_segments:
|
df.loc[segment['start_idx']:segment['end_idx'], 'is_steady'] = 1
|
|
return df, steady_segments
|
|
|
def show_metered_weight_regression():
|
# 初始化服务
|
extruder_service = ExtruderService()
|
main_process_service = MainProcessService()
|
|
# 页面标题
|
st.title("米重多元线性回归分析")
|
|
# 初始化会话状态用于日期同步
|
if 'mr_start_date' not in st.session_state:
|
st.session_state['mr_start_date'] = datetime.now().date() - timedelta(days=7)
|
if 'mr_end_date' not in st.session_state:
|
st.session_state['mr_end_date'] = datetime.now().date()
|
if 'mr_quick_select' not in st.session_state:
|
st.session_state['mr_quick_select'] = "最近7天"
|
if 'mr_time_offset' not in st.session_state:
|
st.session_state['mr_time_offset'] = 0.0
|
if 'mr_selected_features' not in st.session_state:
|
st.session_state['mr_selected_features'] = [
|
'螺杆转速', '机头压力', '流程主速', '螺杆温度',
|
'后机筒温度', '前机筒温度', '机头温度'
|
]
|
if 'mr_use_steady_data' not in st.session_state:
|
st.session_state['mr_use_steady_data'] = True
|
if 'mr_steady_window' not in st.session_state:
|
st.session_state['mr_steady_window'] = 20
|
if 'mr_steady_threshold' not in st.session_state:
|
st.session_state['mr_steady_threshold'] = 0.5
|
|
# 定义回调函数
|
def update_dates(qs):
|
st.session_state['mr_quick_select'] = qs
|
today = datetime.now().date()
|
if qs == "今天":
|
st.session_state['mr_start_date'] = today
|
st.session_state['mr_end_date'] = today
|
elif qs == "最近3天":
|
st.session_state['mr_start_date'] = today - timedelta(days=3)
|
st.session_state['mr_end_date'] = today
|
elif qs == "最近7天":
|
st.session_state['mr_start_date'] = today - timedelta(days=7)
|
st.session_state['mr_end_date'] = today
|
elif qs == "最近30天":
|
st.session_state['mr_start_date'] = today - timedelta(days=30)
|
st.session_state['mr_end_date'] = today
|
|
def on_date_change():
|
st.session_state['mr_quick_select'] = "自定义"
|
|
# 查询条件区域
|
with st.expander("🔍 查询配置", expanded=True):
|
# 添加自定义 CSS 实现响应式换行
|
st.markdown("""
|
<style>
|
/* 强制列容器换行 */
|
[data-testid="stExpander"] [data-testid="column"] {
|
flex: 1 1 120px !important;
|
min-width: 120px !important;
|
}
|
/* 针对日期输入框列稍微加宽一点 */
|
@media (min-width: 768px) {
|
[data-testid="stExpander"] [data-testid="column"]:nth-child(6),
|
[data-testid="stExpander"] [data-testid="column"]:nth-child(7) {
|
flex: 2 1 180px !important;
|
min-width: 180px !important;
|
}
|
}
|
</style>
|
""", unsafe_allow_html=True)
|
|
# 创建布局
|
cols = st.columns([1, 1, 1, 1, 1, 1.5, 1.5, 1])
|
|
options = ["今天", "最近3天", "最近7天", "最近30天", "自定义"]
|
for i, option in enumerate(options):
|
with cols[i]:
|
# 根据当前选择状态决定按钮类型
|
button_type = "primary" if st.session_state['mr_quick_select'] == option else "secondary"
|
if st.button(option, key=f"btn_mr_{option}", width='stretch', type=button_type):
|
update_dates(option)
|
st.rerun()
|
|
with cols[5]:
|
start_date = st.date_input(
|
"开始日期",
|
label_visibility="collapsed",
|
key="mr_start_date",
|
on_change=on_date_change
|
)
|
|
with cols[6]:
|
end_date = st.date_input(
|
"结束日期",
|
label_visibility="collapsed",
|
key="mr_end_date",
|
on_change=on_date_change
|
)
|
|
with cols[7]:
|
query_button = st.button("🚀 开始分析", key="mr_query", width='stretch')
|
|
# 数据对齐调整
|
st.markdown("---")
|
offset_cols = st.columns([2, 4, 2])
|
with offset_cols[0]:
|
st.write("⏱️ **数据对齐调整**")
|
with offset_cols[1]:
|
time_offset = st.slider(
|
"时间偏移 (分钟)",
|
min_value=0.0,
|
max_value=5.0,
|
value=st.session_state['mr_time_offset'],
|
step=0.1,
|
help="调整主流程和温度数据的时间偏移,使其与挤出机米重数据对齐。"
|
)
|
st.session_state['mr_time_offset'] = time_offset
|
with offset_cols[2]:
|
st.write(f"当前偏移: {time_offset} 分钟")
|
|
# 稳态识别配置
|
st.markdown("---")
|
steady_cols = st.columns(3)
|
with steady_cols[0]:
|
st.write("⚖️ **稳态识别配置**")
|
st.checkbox(
|
"仅使用稳态数据进行训练",
|
value=st.session_state['mr_use_steady_data'],
|
key="mr_use_steady_data",
|
help="启用后,只使用米重稳态时段的数据进行模型训练"
|
)
|
|
with steady_cols[1]:
|
st.write("📏 **稳态参数**")
|
st.slider(
|
"滑动窗口大小 (秒)",
|
min_value=5,
|
max_value=60,
|
value=st.session_state['mr_steady_window'],
|
step=5,
|
key="mr_steady_window",
|
help="用于稳态识别的滑动窗口大小"
|
)
|
|
with steady_cols[2]:
|
st.write("📊 **稳态阈值**")
|
st.slider(
|
"波动阈值 (%)",
|
min_value=0.1,
|
max_value=2.0,
|
value=st.session_state['mr_steady_threshold'],
|
step=0.1,
|
key="mr_steady_threshold",
|
help="稳态识别的波动范围阈值"
|
)
|
|
# 特征选择
|
st.markdown("---")
|
st.write("📋 **特征选择**")
|
feature_cols = st.columns(2)
|
all_features = [
|
'螺杆转速', '机头压力', '流程主速', '螺杆温度',
|
'后机筒温度', '前机筒温度', '机头温度'
|
]
|
for i, feature in enumerate(all_features):
|
with feature_cols[i % 2]:
|
st.session_state['mr_selected_features'] = [
|
f for f in st.session_state['mr_selected_features'] if f in all_features
|
]
|
if st.checkbox(
|
feature,
|
key=f"feat_{feature}",
|
value=feature in st.session_state['mr_selected_features']
|
):
|
if feature not in st.session_state['mr_selected_features']:
|
st.session_state['mr_selected_features'].append(feature)
|
else:
|
if feature in st.session_state['mr_selected_features']:
|
st.session_state['mr_selected_features'].remove(feature)
|
|
if not st.session_state['mr_selected_features']:
|
st.warning("至少需要选择一个特征变量")
|
|
# 转换为datetime对象
|
start_dt = datetime.combine(start_date, datetime.min.time())
|
end_dt = datetime.combine(end_date, datetime.max.time())
|
|
# 查询处理 - 仅获取数据并缓存到会话状态
|
if query_button:
|
with st.spinner("正在获取数据..."):
|
# 1. 获取完整的挤出机数据(包含所有时间点的螺杆转速和机头压力)
|
df_extruder_full = extruder_service.get_extruder_data(start_dt, end_dt)
|
|
# 2. 获取主流程控制数据
|
df_main_speed = main_process_service.get_cutting_setting_data(start_dt, end_dt)
|
|
df_temp = main_process_service.get_temperature_control_data(start_dt, end_dt)
|
|
# 检查是否有数据
|
has_data = any([
|
df_extruder_full is not None and not df_extruder_full.empty,
|
df_main_speed is not None and not df_main_speed.empty,
|
df_temp is not None and not df_temp.empty
|
])
|
|
if not has_data:
|
st.warning("所选时间段内未找到任何数据,请尝试调整查询条件。")
|
# 清除缓存数据
|
for key in ['cached_extruder_full', 'cached_main_speed', 'cached_temp', 'last_query_start', 'last_query_end']:
|
if key in st.session_state:
|
del st.session_state[key]
|
return
|
|
# 缓存数据到会话状态
|
st.session_state['cached_extruder_full'] = df_extruder_full
|
st.session_state['cached_main_speed'] = df_main_speed
|
st.session_state['cached_temp'] = df_temp
|
st.session_state['last_query_start'] = start_dt
|
st.session_state['last_query_end'] = end_dt
|
|
# 数据处理和图表渲染 - 每次应用重新运行时执行(包括调整时间偏移时)
|
if all(key in st.session_state for key in ['cached_extruder_full', 'cached_main_speed', 'cached_temp']):
|
with st.spinner("正在分析数据..."):
|
# 获取缓存数据
|
df_extruder_full = st.session_state['cached_extruder_full']
|
df_main_speed = st.session_state['cached_main_speed']
|
df_temp = st.session_state['cached_temp']
|
|
# 获取当前时间偏移量
|
offset_delta = timedelta(minutes=st.session_state['mr_time_offset'])
|
|
# 处理数据
|
if df_extruder_full is not None and not df_extruder_full.empty:
|
# 过滤机头压力大于2的值
|
df_extruder_filtered = df_extruder_full[df_extruder_full['head_pressure'] <= 2]
|
|
# 为米重数据创建偏移后的时间列(只对米重数据进行时间偏移)
|
df_extruder_filtered['weight_time'] = df_extruder_filtered['time'] - offset_delta
|
else:
|
df_extruder_filtered = None
|
|
# 检查是否有数据
|
has_data = any([
|
df_extruder_filtered is not None and not df_extruder_filtered.empty,
|
df_main_speed is not None and not df_main_speed.empty,
|
df_temp is not None and not df_temp.empty
|
])
|
|
if not has_data:
|
st.warning("所选时间段内未找到任何数据,请尝试调整查询条件。")
|
return
|
|
# 数据整合与预处理
|
def integrate_data(df_extruder_filtered, df_main_speed, df_temp):
|
# 确保挤出机数据存在
|
if df_extruder_filtered is None or df_extruder_filtered.empty:
|
return None
|
|
# 创建只包含米重和偏移时间的主数据集
|
df_weight = df_extruder_filtered[['weight_time', 'metered_weight']].copy()
|
df_weight.rename(columns={'weight_time': 'time'}, inplace=True) # 将weight_time重命名为time作为基准时间
|
|
# 创建包含螺杆转速和原始时间的完整数据集
|
df_screw = df_extruder_filtered[['time', 'screw_speed_actual']].copy()
|
|
# 创建包含机头压力和原始时间的完整数据集
|
df_pressure = df_extruder_filtered[['time', 'head_pressure']].copy()
|
|
# 使用偏移后的米重时间整合螺杆转速数据
|
df_merged = pd.merge_asof(
|
df_weight.sort_values('time'),
|
df_screw.sort_values('time'),
|
on='time',
|
direction='nearest',
|
tolerance=pd.Timedelta('1min')
|
)
|
|
# 使用偏移后的米重时间整合机头压力数据
|
df_merged = pd.merge_asof(
|
df_merged.sort_values('time'),
|
df_pressure.sort_values('time'),
|
on='time',
|
direction='nearest',
|
tolerance=pd.Timedelta('1min')
|
)
|
|
# 整合主流程数据
|
if df_main_speed is not None and not df_main_speed.empty:
|
df_main_speed = df_main_speed[['time', 'process_main_speed']]
|
df_merged = pd.merge_asof(
|
df_merged.sort_values('time'),
|
df_main_speed.sort_values('time'),
|
on='time',
|
direction='nearest',
|
tolerance=pd.Timedelta('1min')
|
)
|
|
# 整合温度数据
|
if df_temp is not None and not df_temp.empty:
|
temp_cols = ['time', 'nakata_extruder_screw_display_temp',
|
'nakata_extruder_rear_barrel_display_temp',
|
'nakata_extruder_front_barrel_display_temp',
|
'nakata_extruder_head_display_temp']
|
df_temp_subset = df_temp[temp_cols].copy()
|
df_merged = pd.merge_asof(
|
df_merged.sort_values('time'),
|
df_temp_subset.sort_values('time'),
|
on='time',
|
direction='nearest',
|
tolerance=pd.Timedelta('1min')
|
)
|
|
# 重命名列以提高可读性
|
df_merged.rename(columns={
|
'screw_speed_actual': '螺杆转速',
|
'head_pressure': '机头压力',
|
'process_main_speed': '流程主速',
|
'nakata_extruder_screw_display_temp': '螺杆温度',
|
'nakata_extruder_rear_barrel_display_temp': '后机筒温度',
|
'nakata_extruder_front_barrel_display_temp': '前机筒温度',
|
'nakata_extruder_head_display_temp': '机头温度'
|
}, inplace=True)
|
|
# 清理数据
|
df_merged.dropna(subset=['metered_weight'], inplace=True)
|
|
return df_merged
|
|
# 执行数据整合
|
df_analysis = integrate_data(df_extruder_filtered, df_main_speed, df_temp)
|
|
if df_analysis is None or df_analysis.empty:
|
st.warning("数据整合失败,请检查数据质量或调整时间范围。")
|
return
|
|
# 重命名米重列
|
df_analysis.rename(columns={'metered_weight': '米重'}, inplace=True)
|
|
# 稳态识别
|
steady_detector = SteadyStateDetector()
|
|
# 获取稳态识别参数
|
use_steady_data = st.session_state.get('mr_use_steady_data', True)
|
steady_window = st.session_state.get('mr_steady_window', 20)
|
steady_threshold = st.session_state.get('mr_steady_threshold', 0.5)
|
|
# 执行稳态识别
|
df_analysis_with_steady, steady_segments = steady_detector.detect_steady_state(
|
df_analysis,
|
weight_col='米重',
|
window_size=steady_window,
|
std_threshold=steady_threshold
|
)
|
|
# 更新df_analysis为包含稳态标记的数据
|
df_analysis = df_analysis_with_steady
|
|
# 稳态数据可视化
|
st.subheader("📈 稳态数据分布")
|
|
# 创建稳态数据可视化图表
|
fig_steady = go.Figure()
|
|
# 添加原始米重曲线
|
fig_steady.add_trace(go.Scatter(
|
x=df_analysis['time'],
|
y=df_analysis['米重'],
|
name='原始米重',
|
mode='lines',
|
line=dict(color='lightgray', width=1)
|
))
|
|
# 添加稳态数据点
|
steady_data_points = df_analysis[df_analysis['is_steady'] == 1]
|
fig_steady.add_trace(go.Scatter(
|
x=steady_data_points['time'],
|
y=steady_data_points['米重'],
|
name='稳态米重',
|
mode='markers',
|
marker=dict(color='green', size=3, opacity=0.6)
|
))
|
|
# 添加非稳态数据点
|
non_steady_data_points = df_analysis[df_analysis['is_steady'] == 0]
|
fig_steady.add_trace(go.Scatter(
|
x=non_steady_data_points['time'],
|
y=non_steady_data_points['米重'],
|
name='非稳态米重',
|
mode='markers',
|
marker=dict(color='red', size=3, opacity=0.6)
|
))
|
|
# 配置图表布局
|
fig_steady.update_layout(
|
title="米重数据稳态分布",
|
xaxis=dict(title="时间"),
|
yaxis=dict(title="米重 (Kg/m)"),
|
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
height=500
|
)
|
|
# 显示图表
|
st.plotly_chart(fig_steady, use_container_width=True)
|
|
# 显示稳态统计
|
total_data = len(df_analysis)
|
steady_data = len(df_analysis[df_analysis['is_steady'] == 1])
|
steady_ratio = (steady_data / total_data * 100) if total_data > 0 else 0
|
|
stats_cols = st.columns(3)
|
stats_cols[0].metric("总数据量", total_data)
|
stats_cols[1].metric("稳态数据量", steady_data)
|
stats_cols[2].metric("稳态数据比例", f"{steady_ratio:.1f}%")
|
|
# --- 原始数据趋势图 ---
|
st.subheader("📈 原始数据趋势图")
|
|
# 创建趋势图
|
fig_trend = go.Figure()
|
|
# 添加米重数据(使用偏移后的时间)
|
if df_extruder_filtered is not None and not df_extruder_filtered.empty:
|
fig_trend.add_trace(go.Scatter(
|
x=df_extruder_filtered['weight_time'], # 使用偏移后的时间
|
y=df_extruder_filtered['metered_weight'],
|
name='米重 (Kg/m) [已偏移]',
|
mode='lines',
|
line=dict(color='blue', width=2)
|
))
|
|
# 添加螺杆转速(使用原始时间)
|
fig_trend.add_trace(go.Scatter(
|
x=df_extruder_filtered['time'], # 使用原始时间
|
y=df_extruder_filtered['screw_speed_actual'],
|
name='螺杆转速 (RPM)',
|
mode='lines',
|
line=dict(color='green', width=1.5),
|
yaxis='y2'
|
))
|
|
# 添加机头压力(使用原始时间,已过滤大于2的值)
|
fig_trend.add_trace(go.Scatter(
|
x=df_extruder_filtered['time'], # 使用原始时间
|
y=df_extruder_filtered['head_pressure'],
|
name='机头压力 (≤2)',
|
mode='lines',
|
line=dict(color='orange', width=1.5),
|
yaxis='y3'
|
))
|
|
# 添加流程主速
|
if df_main_speed is not None and not df_main_speed.empty:
|
fig_trend.add_trace(go.Scatter(
|
x=df_main_speed['time'],
|
y=df_main_speed['process_main_speed'],
|
name='流程主速 (M/Min)',
|
mode='lines',
|
line=dict(color='red', width=1.5),
|
yaxis='y4'
|
))
|
|
# 添加温度数据
|
if df_temp is not None and not df_temp.empty:
|
# 螺杆温度
|
fig_trend.add_trace(go.Scatter(
|
x=df_temp['time'],
|
y=df_temp['nakata_extruder_screw_display_temp'],
|
name='螺杆温度 (°C)',
|
mode='lines',
|
line=dict(color='purple', width=1),
|
yaxis='y5'
|
))
|
|
# 配置趋势图布局
|
fig_trend.update_layout(
|
title=f'原始数据趋势 (米重向前偏移 {st.session_state["mr_time_offset"]} 分钟)',
|
xaxis=dict(
|
title='时间',
|
rangeslider=dict(visible=True),
|
type='date'
|
),
|
yaxis=dict(
|
title='米重 (Kg/m)',
|
title_font=dict(color='blue'),
|
tickfont=dict(color='blue')
|
),
|
yaxis2=dict(
|
title='螺杆转速 (RPM)',
|
title_font=dict(color='green'),
|
tickfont=dict(color='green'),
|
overlaying='y',
|
side='right'
|
),
|
yaxis3=dict(
|
title='机头压力',
|
title_font=dict(color='orange'),
|
tickfont=dict(color='orange'),
|
overlaying='y',
|
side='right',
|
anchor='free',
|
position=0.85
|
),
|
yaxis4=dict(
|
title='流程主速 (M/Min)',
|
title_font=dict(color='red'),
|
tickfont=dict(color='red'),
|
overlaying='y',
|
side='right',
|
anchor='free',
|
position=0.75
|
),
|
yaxis5=dict(
|
title='温度 (°C)',
|
title_font=dict(color='purple'),
|
tickfont=dict(color='purple'),
|
overlaying='y',
|
side='left',
|
anchor='free',
|
position=0.15
|
),
|
legend=dict(
|
orientation="h",
|
yanchor="bottom",
|
y=1.02,
|
xanchor="right",
|
x=1
|
),
|
height=600,
|
margin=dict(l=100, r=200, t=100, b=100),
|
hovermode='x unified'
|
)
|
|
# 显示趋势图
|
st.plotly_chart(fig_trend, width='stretch', config={'scrollZoom': True})
|
|
# --- 多元线性回归分析 ---
|
st.subheader("📊 多元线性回归分析")
|
|
# 检查是否选择了特征
|
if not st.session_state['mr_selected_features']:
|
st.warning("请至少选择一个特征变量进行回归分析")
|
else:
|
# 检查所有选择的特征是否在数据中
|
missing_features = [f for f in st.session_state['mr_selected_features'] if f not in df_analysis.columns]
|
if missing_features:
|
st.warning(f"数据中缺少以下特征: {', '.join(missing_features)}")
|
else:
|
# 准备数据
|
# 根据配置决定是否只使用稳态数据
|
use_steady_data = st.session_state.get('mr_use_steady_data', True)
|
if use_steady_data:
|
df_filtered = df_analysis[df_analysis['is_steady'] == 1]
|
st.info(f"已过滤非稳态数据,使用 {len(df_filtered)} 条稳态数据进行训练")
|
else:
|
df_filtered = df_analysis.copy()
|
|
X = df_filtered[st.session_state['mr_selected_features']]
|
y = df_filtered['米重']
|
|
# 清理数据中的NaN值
|
combined = pd.concat([X, y], axis=1)
|
combined_clean = combined.dropna()
|
|
# 检查清理后的数据量
|
if len(combined_clean) < 10:
|
st.warning("数据量不足或包含过多NaN值,无法进行有效的回归分析")
|
else:
|
# 重新分离X和y
|
X_clean = combined_clean[st.session_state['mr_selected_features']]
|
y_clean = combined_clean['米重']
|
|
# 分割训练集和测试集
|
X_train, X_test, y_train, y_test = train_test_split(X_clean, y_clean, test_size=0.2, random_state=42)
|
|
# 训练模型
|
model = LinearRegression()
|
model.fit(X_train, y_train)
|
|
# 预测
|
y_pred = model.predict(X_test)
|
y_train_pred = model.predict(X_train)
|
|
# 计算评估指标
|
r2 = r2_score(y_test, y_pred)
|
mse = mean_squared_error(y_test, y_pred)
|
mae = mean_absolute_error(y_test, y_pred)
|
rmse = np.sqrt(mse)
|
|
# 显示模型性能
|
metrics_cols = st.columns(2)
|
with metrics_cols[0]:
|
st.metric("R² 得分", f"{r2:.4f}")
|
st.metric("均方误差 (MSE)", f"{mse:.6f}")
|
with metrics_cols[1]:
|
st.metric("平均绝对误差 (MAE)", f"{mae:.6f}")
|
st.metric("均方根误差 (RMSE)", f"{rmse:.6f}")
|
|
# --- 实际值与预测值对比 ---
|
st.subheader("🔄 实际值与预测值对比")
|
|
# 创建对比数据
|
compare_df = pd.DataFrame({
|
'实际值': y_test,
|
'预测值': y_pred
|
})
|
compare_df = compare_df.sort_index()
|
|
# 创建对比图
|
fig_compare = go.Figure()
|
fig_compare.add_trace(go.Scatter(
|
x=compare_df.index,
|
y=compare_df['实际值'],
|
name='实际值',
|
mode='lines+markers',
|
line=dict(color='blue', width=2)
|
))
|
fig_compare.add_trace(go.Scatter(
|
x=compare_df.index,
|
y=compare_df['预测值'],
|
name='预测值',
|
mode='lines+markers',
|
line=dict(color='red', width=2, dash='dash')
|
))
|
fig_compare.update_layout(
|
title='测试集: 实际米重 vs 预测米重',
|
xaxis=dict(title='样本索引'),
|
yaxis=dict(title='米重 (Kg/m)'),
|
legend=dict(orientation='h', yanchor='bottom', y=1.02, xanchor='right', x=1),
|
height=400
|
)
|
st.plotly_chart(fig_compare, width='stretch')
|
|
# --- 残差分析 ---
|
st.subheader("📉 残差分析")
|
|
# 计算残差
|
residuals = y_test - y_pred
|
|
# 创建残差图
|
fig_residual = go.Figure()
|
fig_residual.add_trace(go.Scatter(
|
x=y_pred,
|
y=residuals,
|
mode='markers',
|
marker=dict(color='green', size=8, opacity=0.6)
|
))
|
fig_residual.add_shape(
|
type="line",
|
x0=y_pred.min(),
|
y0=0,
|
x1=y_pred.max(),
|
y1=0,
|
line=dict(color="red", width=2, dash="dash")
|
)
|
fig_residual.update_layout(
|
title='残差图',
|
xaxis=dict(title='预测值'),
|
yaxis=dict(title='残差'),
|
height=400
|
)
|
st.plotly_chart(fig_residual, width='stretch')
|
|
# --- 特征重要性 ---
|
st.subheader("⚖️ 特征重要性分析")
|
|
# 计算特征重要性(基于系数绝对值)
|
feature_importance = pd.DataFrame({
|
'特征': st.session_state['mr_selected_features'],
|
'系数': model.coef_,
|
'重要性': np.abs(model.coef_)
|
})
|
feature_importance = feature_importance.sort_values('重要性', ascending=False)
|
|
# 创建特征重要性图
|
fig_importance = px.bar(
|
feature_importance,
|
x='特征',
|
y='重要性',
|
title='特征重要性(基于系数绝对值)',
|
color='重要性',
|
color_continuous_scale='viridis'
|
)
|
fig_importance.update_layout(
|
xaxis=dict(tickangle=-45),
|
height=400
|
)
|
st.plotly_chart(fig_importance, width='stretch')
|
|
# 显示系数表
|
st.write("### 模型系数")
|
coef_df = pd.DataFrame({
|
'特征': ['截距'] + st.session_state['mr_selected_features'],
|
'系数': [model.intercept_] + list(model.coef_)
|
})
|
st.dataframe(coef_df, use_container_width=True)
|
|
# --- 模型保存功能 ---
|
st.subheader("💾 模型保存")
|
|
# 创建模型保存表单
|
st.write("保存训练好的模型权重:")
|
model_name = st.text_input(
|
"模型名称",
|
value=f"linear_regression_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
help="请输入模型名称,模型将保存为该名称的.joblib文件"
|
)
|
|
if st.button("保存模型"):
|
# 确保模型目录存在
|
model_dir = "saved_models"
|
os.makedirs(model_dir, exist_ok=True)
|
|
# 保存模型
|
model_path = os.path.join(model_dir, f"{model_name}.joblib")
|
try:
|
# 保存模型权重和相关信息
|
model_info = {
|
'model': model,
|
'features': st.session_state['mr_selected_features'],
|
'scaler': None, # 线性回归不需要标化器
|
'model_type': 'linear_regression',
|
'created_at': datetime.now(),
|
'r2_score': r2,
|
'mse': mse,
|
'mae': mae,
|
'rmse': rmse,
|
'use_steady_data': use_steady_data
|
}
|
joblib.dump(model_info, model_path)
|
st.success(f"模型已成功保存到: {model_path}")
|
except Exception as e:
|
st.error(f"模型保存失败: {e}")
|
|
# --- 数据预览 ---
|
st.subheader("🔍 数据预览")
|
st.dataframe(df_analysis.head(20), use_container_width=True)
|
else:
|
# 提示用户点击开始分析按钮
|
st.info("请选择时间范围并点击'开始分析'按钮获取数据。")
|