"""
产品组件：GDM (数据建模工作台)
工作空间：b
文件：机器学习模型1.gdmm
"""


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.io.matlab.tests.test_mio import a
from sklearn.metrics import mean_squared_error

from gdmBHTR.transformed_histogram import TransformedHistogramRegressor
from gdmBHTR.transformed_boost import TransformedHistogramBoost

nd_train_x = DF_train.select("data_x").toPandas()
train_X=np.array(nd_train_x) 

 
nd_train_y = DF_train.select("data_y").toPandas()
train_y1=np.array(nd_train_y) 
train_y=train_y1.flatten()

nd_test_x = DF_verify.select("data_x").toPandas()
test_X=np.array(nd_test_x) 

nd_test_y = DF_verify.select("data_y").toPandas()
test_y1 = np.array(nd_test_y) 
test_y=test_y1.flatten()
 
 
NumHistograms = 500 # 直方图变换迭代次数 
Lambda = 0.01 # rho：学习率 
LowerMargin, UpperMargin = -2, 0 # 直方图变换尺度参数 
Samplerate = 1 # 样本率 
 
## 使用TransformedHistogramBoost类调用BHTR的步骤 
reg_boost = TransformedHistogramBoost(NumHistograms,Lambda,LowerMargin,UpperMargin,Samplerate) # 初始化 
reg_boost.fit(train_X, train_y) # 用训练集数据拟合模型 
 
test_y_hat = reg_boost.predict(test_X) # 预测测试集数据 
mse_opt_j, ite_opt_j, mse_store = reg_boost.predict_mse(test_X,test_y) # 计算模型的均方误差 

print("""{
    "指标项": "均方误差",
    "指标值": """ + str(mse_store) + """
},
{
    "指标项": "最小均方误差",
    "指标值": """ + str(mse_opt_j) + """
},
{
    "指标项": "最小均方误差对应的直方图变换迭代次数 ",
    "指标值": """ + str(ite_opt_j) + """
},
{
    "指标项": "Y值均方误差 ",
    "指标值": """ + str(mean_squared_error(test_y, test_y_hat)) + """
}""")









