pd.MultiIndex()
报错 TypeError: new() got an unexpected keyword argument ‘labels’
coefficients = list()
coeff_labels = ['lr', 'l1', 'l2']
coeff_models = [lr, lr_l1, lr_l2]
for lab,mod in zip(coeff_labels, coeff_models):
coeffs = mod.coef_
coeff_label = pd.MultiIndex(levels=[[lab], [0,1,2,3,4,5]],
labels=[[0,0,0,0,0,0], [0,1,2,3,4,5]])
coefficients.append(pd.DataFrame(coeffs.T, columns=coeff_label))
coefficients = pd.concat(coefficients, axis=1)
coefficients.sample(10)
报错信息
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-a2d41abfe2ce> in <module>()
8 coeffs = mod.coef_
9 coeff_label = pd.MultiIndex(levels=[[lab], [0,1,2,3,4,5]],
---> 10 labels=[[0,0,0,0,0,0], [0,1,2,3,4,5]])
11 coefficients.append(pd.DataFrame(coeffs.T, columns=coeff_label))
12
TypeError: __new__() got an unexpected keyword argument 'labels'
解决方法
使用pd.MultiIndex.from_product() 替换 pd.MultiIndex()
coeff_label = pd.MultiIndex.from_product([[lab], [0,1,2,3,4,5]], names=['coeff_labels', 'coefficient'])