# kfoldLoss
交叉验证分区回归模型的损失
函数库: TyMachineLearning
# 语法
ls = kfoldLoss(y_true, y_pred)
# 说明
ls = kfoldLoss(y_true, y_pred)返回由交叉验证回归模型获得的损失ls(均方误差)。
# 示例
通过kfoldLoss_tree计算交叉验证分区回归模型的损失
加载数据集,tree_x中的三个变量特征分别记录着排放量、马力以及重量的信息,tree_y记录着每加仑的英里数MPG。
using CSV
using TyMachineLearning
using DataFrames
file1 = joinpath(pkgdir(TyMachineLearning), "data/Regression/tree_x.csv")
file2 = joinpath(pkgdir(TyMachineLearning), "data/Regression/tree_y.csv")
fgdata_x = CSV.read(file1, DataFrame; header=0)
fgdata_y = CSV.read(file2, DataFrame; header=0)
fgdata_x1 = Matrix(fgdata_x)
fgdata_y1 = Array(fgdata_y)
X = DataFrame([fgdata_x1 fgdata_y1], :auto)
rename!(X, [:x4 => :y])
X_pre, y_pre = dropna(X)
显示样本数据X的前5行数据。
X[1:5,:]
5×4 DataFrame
Row │ x1 x2 x3 y
│ Float64 Float64 Float64 Float64
─────┼────────────────────────────────────
1 │ 307.0 130.0 3504.0 18.0
2 │ 350.0 165.0 3693.0 15.0
3 │ 318.0 150.0 3436.0 18.0
4 │ 304.0 150.0 3433.0 16.0
5 │ 302.0 140.0 3449.0 17.0
对该数据集进行回归树fitrtree模型拟合。
mdf = fitrtree(X_pre, y_pre;max_depth=2)
PyObject DecisionTreeRegressor(max_depth=2, min_weight_fraction_leaf=0, random_state=1)
计算模型的损失值。
mdl = mdf
X_test = X_pre
tmm2 = TyMachineLearning.predict(mdl, X_test)
y_true = y_pre
y_pred = tmm2
ls = kfoldLoss(y_true, y_pred)
14.530186465267116
# 输入参数
y_true - 响应变量
数值向量
响应数据,指定为 n×1 数值向量。y 的行对应于不同观测值。y 的行数必须与 X 的行数相同。
数据类型: Vector
y_pred - 响应变量的预测值
数值向量
模型拟合后响应数据的预测值,指定为 n×1 数值向量。y 的行对应于不同观测值。y 的行数必须与 X 的行数相同。
数据类型: Vector
# 输出参数
ls - 模型的损失值
数值
交叉验证分区回归模型的损失,计算方式为均方误差(mse)。
数据类型: Float