在 pandas 数据框中映射虚拟变量
Mapping dummy variables in pandas data frame
我有一个包含 11 列的大型 DataFrame。我需要将分类变量转换为二进制值,所以我使用了 Patsy
:
attributes = "admit ~ C(gender) + age + C(ethnicity) + C(state) + gpa + sci_gpa + mcat + C(major) + C(tier) + C(same_ins)"
y, X = dmatrices(attributes, df, return_type="dataframe")
这很好用。但是,我想使用以原始数据框格式存储的数据来测试新样本
例如:
gender age ethnicity state gpa sci_gpa gre major tier same_ins
male 21 Asian NV 3.4 3.2 .99 Physics 1 1
有没有简单的方法可以将其转换为与 X 相同的格式??
我明白了。 Patsy 存储 dmatrix 的元数据。可以通过
调用
build_design_matrices()
我有一个包含 11 列的大型 DataFrame。我需要将分类变量转换为二进制值,所以我使用了 Patsy
:
attributes = "admit ~ C(gender) + age + C(ethnicity) + C(state) + gpa + sci_gpa + mcat + C(major) + C(tier) + C(same_ins)"
y, X = dmatrices(attributes, df, return_type="dataframe")
这很好用。但是,我想使用以原始数据框格式存储的数据来测试新样本 例如:
gender age ethnicity state gpa sci_gpa gre major tier same_ins
male 21 Asian NV 3.4 3.2 .99 Physics 1 1
有没有简单的方法可以将其转换为与 X 相同的格式??
我明白了。 Patsy 存储 dmatrix 的元数据。可以通过
调用build_design_matrices()