为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ibeautiful 2023-07-11 14:54:31
我试图使用这段代码将包gower_matrix中的函数应用gower到字典的值:import gowerimport pandas as pdfrom itertools import chain, combinationsfrom pydataset import datafrom toolz.dicttoolz import valmapcars = data('mtcars')vnames=cars.columnsdef 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)对这个问题有什么想法吗?
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

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



查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信