将函数应用于字典值不起作用

Applying function to dictionary values not working

我正在尝试使用这段代码将 gower 包中的 gower_matrix 函数应用于字典的值:

import gower
import pandas as pd
from itertools import chain, combinations
from pydataset import data
from toolz.dicttoolz import valmap

cars = data('mtcars')
vnames=cars.columns
def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(1,len(s)+1))

combos=list(powerset(vnames))
combos=list(map(list, list(powerset(vnames))))
combo_dicts = {}
keys = range(len(combos))

for i in keys:
        combo_dicts[i] = cars[combos[i]]
        
gower_dicts = valmap(gower.gower_matrix, combo_dicts)

但我收到以下错误

TypeError: ufunc 'true_divide' output (typecode 'd') 
could not be coerced to provided output parameter (typecode 'q') 
according to the casting rule ''same_kind''

将它应用到特定的字典项上有效

gower.gower_matrix(combo_dicts[100])


array([[0.        , 0.02173357, 0.19395797, ..., 0.12646227, 0.35655078,
        0.11454861],
       [0.02173357, 0.        , 0.21569154, ..., 0.12262693, 0.3348172 ,
        0.10900868],
       [0.19395797, 0.21569154, 0.        , ..., 0.32042024, 0.55050874,
        0.10668287],
       ...,
       [0.12646227, 0.12262693, 0.32042024, ..., 0.        , 0.23008852,
        0.21544196],
       [0.35655078, 0.3348172 , 0.55050874, ..., 0.23008852, 0.        ,
        0.44382587],
       [0.11454861, 0.10900868, 0.10668287, ..., 0.21544196, 0.44382587,
        0.        ]], dtype=float32)

对这个问题有什么想法吗?

根据对 ufunc 'true_divide' output 的网络搜索,appears 在尝试划分整数值数组时出现错误(不是 Numpy 错误,而是几年前发生的行为)通过 floating-point 值。您传入 floating-point 值似乎是 gower 包的未指定要求。所以先转换cars数据。我的猜测是您有一些包含 floating-point 值的列和一些包含整数的列; combo_dicts 的测试元素工作正常,因为它恰好仅从 floating-point 列生成。