Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有子组操作(如行列式)的pandas groupby?_Python_Regression_Pandas Groupby - Fatal编程技术网

Python 具有子组操作(如行列式)的pandas groupby?

Python 具有子组操作(如行列式)的pandas groupby?,python,regression,pandas-groupby,Python,Regression,Pandas Groupby,我最近在做fat回归,所以我从R迁移到python,一切正常,但我经常遇到“奇异矩阵”(由于多重共线性或完美可预测性)错误。这在R或Stata中不是问题,因为它们会自动删除这样的观察值,所以我实际上有两个问题: python中是否有内置的方法来处理我刚才提到的两个源代码引起的“奇异矩阵”错误 如果没有,我想自己挑选这些观察结果,但在尝试时遇到了问题 基本思想是,对于每个子组(我使用lots fixed effect),计算相关性的行列式,去掉那些值足够低的子组 我的数据: df = pd.Dat

我最近在做fat回归,所以我从R迁移到python,一切正常,但我经常遇到“奇异矩阵”(由于多重共线性或完美可预测性)错误。这在R或Stata中不是问题,因为它们会自动删除这样的观察值,所以我实际上有两个问题:

  • python中是否有内置的方法来处理我刚才提到的两个源代码引起的“奇异矩阵”错误

  • 如果没有,我想自己挑选这些观察结果,但在尝试时遇到了问题

  • 基本思想是,对于每个子组(我使用lots fixed effect),计算相关性的行列式,去掉那些值足够低的子组

    我的数据:

    df = pd.DataFrame({'col_1': [1,1,1,1,1,2,2,2,2,2],
                       'col_2': [1,2,3,4,5,6,7,8,9,10],
                       'col_3':['A','A','A','A','A','B','B','B','B','B'],
                       'col_4':0*10})
    
    col_4 ~ col_1 + col_2 + C(col_3)
    
    我的回归方程:

    df = pd.DataFrame({'col_1': [1,1,1,1,1,2,2,2,2,2],
                       'col_2': [1,2,3,4,5,6,7,8,9,10],
                       'col_3':['A','A','A','A','A','B','B','B','B','B'],
                       'col_4':0*10})
    
    col_4 ~ col_1 + col_2 + C(col_3)
    
    每组的决定因素:

    np.linalg.det(np.corrcoef(df.loc[df['col_3'].isin(['A'])][['col_1','col_2','col_4']]))
    2.068910295301473e-48
    np.linalg.det(np.corrcoef(df.loc[df['col_3'].isin(['B'])][['col_1','col_2','col_4']]))
    8.197321048400957e-50
    
    我想要什么

     col_1  col_2 col_3  col_4       col_det
    0      1      1     A      0  2.068910e-48
    1      1      2     A      0  2.068910e-48
    2      1      3     A      0  2.068910e-48
    3      1      4     A      0  2.068910e-48
    4      1      5     A      0  2.068910e-48
    5      2      6     B      0  8.197321e-50
    6      2      7     B      0  8.197321e-50
    7      2      8     B      0  8.197321e-50
    8      2      9     B      0  8.197321e-50
    9      2     10     B      0  8.197321e-50