如何在Python中将蒙特卡罗交叉验证应用于多元线性回归?

时间:2019-04-17 11:52:20

标签: python-3.x linear-regression cross-validation montecarlo

所有人。

因此,我是Python的新手,我试图根据10个不同的数字输入来预测一个数字变量。特别是,我尝试应用多元线性回归,但想在训练测试验证阶段添加Monte Carlo交叉验证。因此,我编写了如下代码:

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.BottomSheetDialogTheme)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)

    dialog.setOnShowListener {
        val bottomSheet = dialog.findViewById<View>(
            android.support.design.R.id.design_bottom_sheet) as? FrameLayout
        val behavior = BottomSheetBehavior.from(bottomSheet)
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
        behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                //showing the different states.
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> dismiss() //if you want the modal to be dismissed when user drags the bottomsheet down
                    BottomSheetBehavior.STATE_EXPANDED -> {
                    }
                    BottomSheetBehavior.STATE_COLLAPSED -> {
                    }
                    BottomSheetBehavior.STATE_DRAGGING -> {
                    }
                    BottomSheetBehavior.STATE_SETTLING -> {
                    }
                }
            }
        })
        dialog.setOnCancelListener {
            // Doesn't matter what you write here, the dialog will be closed.
        }
        dialog.setOnDismissListener {
            // Doesn't matter what you write here, the dialog will be closed.
        }
    }

    return dialog
}

override fun onCancel(dialog: DialogInterface?) {
    // Doesn't matter what you write here, the dialog will be closed.
    super.onCancel(dialog)
}

override fun onDismiss(dialog: DialogInterface?) {
    // Doesn't matter what you write here, the dialog will be closed.
    super.onDismiss(dialog)
}

我的问题是:这是应用蒙特卡洛交叉验证的正确方法吗?

此后,我应用了MLR,并且在每次运行代码时,R平方,MSE和其他值都会发生变化,因此我猜想蒙特卡洛会奏效。如果是这样,是否有任何方法可以使每次运行获得相同的结果,但同时使用MCCV?

此外,目标是还开发一个ANN模型(也使用Monte Carlo),并最终比较MLR和ANN,然后使用最佳开发的模型对未来进行预测。我读过某个地方,在进行预测时无法使用MCCV,对吗?

非常感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

要应用MCCV,您应该运行随机生成(无替换)训练集和测试集的过程。

因此,粗略地说,您需要在for循环中插入代码(生成训练/测试集,学习和预测)。

请注意:分区是针对每次运行独立生成的,因此,同一数据点可以在训练(测试)集中多次出现,实际上,这与k-折叠交叉验证。