fix: 修复工作目录与步骤名不对应、回归预测虚数报错、模型加载及预处理名称转换问题,重构可视化并修正勾选联动
This commit is contained in:
@ -11,16 +11,24 @@ def MMS(input_spectrum):
|
||||
output_spectrum = MinMaxScaler().fit_transform(input_spectrum)
|
||||
return output_spectrum
|
||||
|
||||
# 标准化
|
||||
# 标准化 (StandardScaler)
|
||||
def SS(input_spectrum, save_path=None):
|
||||
"""标准化预处理,使用 StandardScaler 拟合并可选保存模型参数。
|
||||
|
||||
Args:
|
||||
input_spectrum: 输入光谱数据 (numpy array or DataFrame)
|
||||
save_path: scaler模型保存路径。如果提供则保存到该路径(推荐保存到7_Supervised_Model_Training目录)
|
||||
"""
|
||||
# 初始化 StandardScaler 并拟合数据
|
||||
scaler = StandardScaler()
|
||||
output_spectrum = scaler.fit_transform(input_spectrum)
|
||||
|
||||
# 如果指定了保存路径,保存 scaler 对象
|
||||
# 如果指定了保存路径,保存 scaler 对象(用于后续预测时加载)
|
||||
if save_path:
|
||||
import os
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
joblib.dump(scaler, save_path)
|
||||
print(f"Scaler parameters saved to {save_path}")
|
||||
print(f"SS Scaler parameters saved to: {save_path}")
|
||||
|
||||
return output_spectrum
|
||||
|
||||
@ -124,7 +132,14 @@ def wave(input_spectrum):
|
||||
return output_spectrum
|
||||
|
||||
# 通用预处理函数
|
||||
def Preprocessing(method, input_spectrum):
|
||||
def Preprocessing(method, input_spectrum, save_path=None):
|
||||
"""通用预处理函数
|
||||
|
||||
Args:
|
||||
method: 预处理方法名称
|
||||
input_spectrum: 输入光谱数据
|
||||
save_path: 可选的模型保存路径(仅SS方法使用,保存到7_Supervised_Model_Training目录)
|
||||
"""
|
||||
if isinstance(input_spectrum, np.ndarray):
|
||||
input_spectrum = pd.DataFrame(input_spectrum)
|
||||
if method == "None":
|
||||
@ -132,7 +147,11 @@ def Preprocessing(method, input_spectrum):
|
||||
elif method == 'MMS':
|
||||
output_spectrum = MMS(input_spectrum.values)
|
||||
elif method == 'SS':
|
||||
output_spectrum = SS(input_spectrum.values, r'E:\code\WQ\models/scaler_params.pkl')
|
||||
# SS预处理模型保存到工作目录的7_Supervised_Model_Training/scaler_params.pkl
|
||||
# 如果调用者没有提供save_path,则使用默认路径
|
||||
if not save_path:
|
||||
save_path = r'E:\code\WQ\models\scaler_params.pkl'
|
||||
output_spectrum = SS(input_spectrum.values, save_path)
|
||||
elif method == 'CT':
|
||||
output_spectrum = CT(input_spectrum.values)
|
||||
elif method == 'SNV':
|
||||
|
||||
Reference in New Issue
Block a user