Pandas: 熊猫 get_dummies() 和 n-1 分类编码选项以避免共线性?

创建于 2016-01-15  ·  3评论  ·  资料来源: pandas-dev/pandas

在进行线性回归和编码分类变量时,完美共线性可能是一个问题。 为了解决这个问题,建议的方法是使用 n-1 列。 如果pd.get_dummies()有一个布尔参数,该参数为每个被编码的分类列返回 n-1,那将会很有用。

例子:

>>> df
    Account  Network      Device
0  Account1   Search  Smartphone
1  Account1  Display      Tablet
2  Account2   Search  Smartphone
3  Account3  Display  Smartphone
4  Account2   Search      Tablet
5  Account3   Search  Smartphone
>>> pd.get_dummies(df)
   Account_Account1  Account_Account2  Account_Account3  Network_Display  \
0                 1                 0                 0                0   
1                 1                 0                 0                1   
2                 0                 1                 0                0   
3                 0                 0                 1                1   
4                 0                 1                 0                0   
5                 0                 0                 1                0   

   Network_Search  Device_Smartphone  Device_Tablet  
0               1                  1              0  
1               0                  0              1  
2               1                  1              0  
3               0                  1              0  
4               1                  0              1  
5               1                  1              0 

相反,我希望在get_dummies()有一些参数,例如drop_first=True get_dummies() ,它会执行以下操作:

>>> new_df = pd.DataFrame(index=df.index)
>>> for i in df:
    new_df = new_df.join(pd.get_dummies(df[i]).iloc[:, 1:])


>>> new_df
   Account2  Account3  Search  Tablet
0         0         0       1       0
1         0         0       0       1
2         1         0       1       0
3         0         1       0       0
4         1         0       1       1
5         0         1       1       0

来源
http://fastml.com/converting-categorical-data-into-numbers-with-pandas-and-scikit-learn/
http://stackoverflow.com/questions/31498390/how-to-get-pandas-get-dummies-to-emit-n-1-variables-to-avoid-co-lineraity
http://dss.princeton.edu/online_help/analysis/dummy_variables.htm

Reshaping

最有用的评论

允许删除特定值而不仅仅是“第一个”将是有利的。

省略的类别(参考组)影响系数的解释。

例如,一种最佳做法是省略“最大”值作为参考类别;

````

热 = df[['vol_k', '激活']]

cat_vars = list(df.select_dtypes(include=['category']).columns)
对于 cat_vars 中的 var:
新 = pd.get_dummies(df[var])
hot = hot.join(新)

#drop most frequent variable for ref category
drop_col = df.groupby([var]).size().idxmax()
hot.drop(drop_col, axis=1, inplace=True)

print(var + " dropping " + drop_col)
print(df.groupby([var]).size())`

``

所有3条评论

听起来不错,有兴趣提交拉取请求吗?

:+1:

允许删除特定值而不仅仅是“第一个”将是有利的。

省略的类别(参考组)影响系数的解释。

例如,一种最佳做法是省略“最大”值作为参考类别;

````

热 = df[['vol_k', '激活']]

cat_vars = list(df.select_dtypes(include=['category']).columns)
对于 cat_vars 中的 var:
新 = pd.get_dummies(df[var])
hot = hot.join(新)

#drop most frequent variable for ref category
drop_col = df.groupby([var]).size().idxmax()
hot.drop(drop_col, axis=1, inplace=True)

print(var + " dropping " + drop_col)
print(df.groupby([var]).size())`

``

此页面是否有帮助?
0 / 5 - 0 等级