gpt4 book ai didi

python - 从 patsy 中的 DesignMatrix 获取名称

转载 作者:行者123 更新时间:2023-12-02 17:38:32 28 4
gpt4 key购买 nike

from patsy import *
from pandas import *
dta = DataFrame([["lo", 1],["hi", 2.4],["lo", 1.2],["lo", 1.4],["very_high",1.8]], columns=["carbs", "score"])
dmatrix("carbs + score", dta)
DesignMatrix with shape (5, 4)
Intercept carbs[T.lo] carbs[T.very_high] score
1 1 0 1.0
1 0 0 2.4
1 1 0 1.2
1 1 0 1.4
1 0 1 1.8
Terms:
'Intercept' (column 0), 'carbs' (columns 1:3), 'score' (column 3)

问题: 不是使用 Designinfo 指定列的“名称”(这基本上降低了我的代码的可重用性),我能否读取此 DesignMatrix 给出的名称以便我可以提供稍后将其放入 DataFrame,无需预先知道“引用水平/控制组”水平是多少?

即。当我做dmatrix("C(carbs, Treatment(reference='lo')) + score", dta)

"""
# How can I get something like this with dmatrix's output without hardcoding ?
names = obtained from dmatrix's output above
This should give names = ['Intercept' ,'carbs[T.lo]', 'carbs[T.very_high]', 'score']
"""
g=DataFrame(dmatrix("carbs + score", dta),columns=names)

Intercept carbs[T.lo] carbs[T.very_high] score
0 1 2 3
0 1 1 0 1.0
1 1 0 0 2.4
2 1 1 0 1.2
3 1 1 0 1.4
4 1 0 1 1.8

type(g)=<class 'pandas.core.frame.DataFrame'>

所以 g 将是转换后的数据框,我可以在其上进行逻辑建模,而无需记录(或对其进行硬编码)列名及其引用水平。

最佳答案

我认为您要查找的信息在 design_info.column_names 中:

>>> dm = dmatrix("carbs + score", dta)
>>> dm.design_info
DesignInfo(['Intercept', 'carbs[T.lo]', 'carbs[T.very_high]', 'score'],
term_slices=OrderedDict([(Term([]), slice(0, 1, None)), (Term([EvalFactor('carbs')]), slice(1, 3, None)), (Term([EvalFactor('score')]), slice(3, 4, None))]),
builder=<patsy.build.DesignMatrixBuilder at 0xb03f8cc>)
>>> dm.design_info.column_names
['Intercept', 'carbs[T.lo]', 'carbs[T.very_high]', 'score']

等等

>>> DataFrame(dm, columns=dm.design_info.column_names)
Intercept carbs[T.lo] carbs[T.very_high] score
0 1 1 0 1.0
1 1 0 0 2.4
2 1 1 0 1.2
3 1 1 0 1.4
4 1 0 1 1.8

[5 rows x 4 columns]

关于python - 从 patsy 中的 DesignMatrix 获取名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560104/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com