【问题标题】:How to simulate multi-collinearity using Sklearn?如何使用 Sklearn 模拟多重共线性?
【发布时间】:2022-01-22 07:47:46
【问题描述】:

我想看看多重共线性对线性回归模型有什么影响,但我需要能够生成多重共线性数据,在这些数据中我可以改变特征的数量以及这些特征之间的共线性。

我查看了 Sklearn 的 make_regression 函数,它允许生成多个特征,但据我了解,这些特征都是不相关的,对吗?

如果是这样,有谁知道我如何改变这些特征之间的相关性或使用不同的方法来生成线性多共线数据集来训练 Sklearn 的线性回归模型?

【问题讨论】:

    标签: python scikit-learn linear-regression multicollinearity


    【解决方案1】:

    您可以模拟多元正态分布的特征,如下所示:

    import numpy as np
    from sklearn.linear_model import LinearRegression
    
    def make_regression(n_samples, n_uncorrelated, n_correlated, correlation, weights, bias, noise=1, seed=42):
    
        np.random.seed(seed)
    
        X_correlated = np.random.multivariate_normal(
            mean=np.zeros(n_correlated),
            cov=correlation * np.ones((n_correlated, n_correlated)) + (1 - correlation) * np.eye(n_correlated),
            size=n_samples
        )
    
        X_uncorrelated = np.random.multivariate_normal(
            mean=np.zeros(n_uncorrelated),
            cov=np.eye(n_uncorrelated),
            size=n_samples
        )
    
        X = np.hstack([X_correlated, X_uncorrelated])
        e = np.random.normal(loc=0, scale=noise, size=n_samples)
        y = bias + np.dot(X, weights) + e
    
        return X, y
    
    X, y = make_regression(
        n_samples=1000,
        n_uncorrelated=1,
        n_correlated=3,
        correlation=0.999,
        weights=[0.5, 0.5, 0.5, 0.5],
        bias=0,
    )
    
    print(np.round(np.corrcoef(X, rowvar=False), 1))
    # [[ 1.  1.  1. -0.]
    #  [ 1.  1.  1. -0.]
    #  [ 1.  1.  1. -0.]
    #  [-0. -0. -0.  1.]]
    
    reg = LinearRegression()
    reg.fit(X, y)
    
    print(reg.intercept_)
    # -0.0503434375710194
    
    print(reg.coef_)
    # [0.62245063 -0.43110213  1.31516103  0.52019845]
    

    【讨论】:

    • 嘿,这正是我要找的。谢谢!但是有一个问题,相关参数为所有特征设置了相同级别的相关性,我正确吗?如果我想控制特定特征之间的相关程度,它会如何工作?
    • 是的,这是正确的,为简单起见,假设每对相关特征之间的相关系数相同。如果要使用不同的相关系数,则需要手动定义协方差矩阵。
    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2016-10-05
    • 2020-03-26
    • 1970-01-01
    • 2019-01-30
    • 2020-04-09
    • 2021-08-16
    相关资源
    最近更新 更多