成功解决TypeError: only size-1 arrays can be converted to Python scalars

Posted 一个处女座的程序猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了成功解决TypeError: only size-1 arrays can be converted to Python scalars相关的知识,希望对你有一定的参考价值。

成功解决TypeError: only size-1 arrays can be converted to Python scalars

目录

解决问题

解决思路

解决方法

1、分析问题

2、测试bug

3、寻找函数参数正确定义

4、最终解决办法


解决问题

TypeError: only size-1 arrays can be converted to Python scalars

解决思路

类型错误:只有size-1的数组可以转换为 Python 标量

解决方法

1、分析问题

在执行shap.summary_plot(shap_i_values, X_data, max_display=7)函数的时候,遇到了TypeError: only size-1 arrays can be converted to Python scalars问题,

explainer = shap.TreeExplainer(model_C)  # 初始化解释器
shap_i_values = explainer.shap_interaction_values(X_data)   # 返回 估计一组样本的 SHAP交互值
print(model_name,type(shap_i_values),shap_i_values.shape,'\\n',shap_i_values)
shap.summary_plot(shap_i_values, X_data, max_display=7)

2、测试bug

经过测试,发现model_C带来的shap_i_values存在问题,输出shap_i_values

model_C: RandomForestClassifier()
RFC <class 'list'> 2 
 [array([[[ 1.00892288e-01, -1.68401619e-02, -5.94680958e-03, ...,
         -1.32470081e-02,  3.55464100e-02,  5.80109769e-03],
        [-1.68401619e-02, -5.12440004e-02, -3.74259978e-02, ...,
         -2.28063353e-02,  4.59434195e-02, -3.04559773e-03],
        [-5.94680958e-03, -3.74259978e-02, -1.85461859e-02, ...,
         -6.63725042e-03,  1.92107791e-02, -2.68634910e-03],
        ...,
        [-1.32470081e-02, -2.28063353e-02, -6.63725042e-03, ...,
         -1.38762590e-02,  4.56568837e-02, -7.74597253e-03],
        [ 3.55464100e-02,  4.59434195e-02,  1.92107791e-02, ...,
          4.56568837e-02, -3.10902626e-01,  1.16618553e-02],
        [ 5.80109769e-03, -3.04559773e-03, -2.68634910e-03, ...,
         -7.74597253e-03,  1.16618553e-02,  2.75534972e-02]],

       [[ 8.19124603e-02,  1.07770267e-02, -2.73940856e-03, ...,
         -5.42286117e-03, -1.43148441e-02, -9.38927405e-03],
        [ 1.07770267e-02,  1.34434798e-02,  8.07094323e-03, ...,
          4.91124351e-03,  5.50744405e-03,  6.47903095e-03],
        [-2.73940856e-03,  8.07094323e-03, -1.43633784e-02, ...,
          3.11791569e-03,  3.20759035e-03,  3.83808653e-03],
        ...,
        [-5.42286117e-03,  4.91124351e-03,  3.11791569e-03, ...,
          7.87596527e-02,  1.74101730e-02, -1.64075188e-02],
        [-1.43148441e-02,  5.50744405e-03,  3.20759035e-03, ...,
          1.74101730e-02,  1.33626522e-01,  2.42559627e-02],
        [-9.38927405e-03,  6.47903095e-03,  3.83808653e-03, ...,
         -1.64075188e-02,  2.42559627e-02, -5.97394271e-02]],

       [[-1.38735034e-01, -2.42364407e-02,  1.14693608e-02, ...,
         -6.99696322e-03,  1.72904490e-02, -7.51722121e-03],
        [-2.42364407e-02,  2.39629923e-02, -6.97181439e-03, ...,
         -2.22822148e-02, -1.36613812e-02, -3.50808091e-02],
        [ 1.14693608e-02, -6.97181439e-03, -3.59474202e-03, ...,
         -6.09146988e-03,  5.59217986e-04, -3.53721479e-03],
        ...,
        [-6.99696322e-03, -2.22822148e-02, -6.09146988e-03, ...,
         -1.87555406e-02, -5.20973244e-03,  8.26104068e-03],
        [ 1.72904490e-02, -1.36613812e-02,  5.59217986e-04, ...,
         -5.20973244e-03,  1.75241348e-01, -3.04973301e-03],
        [-7.51722121e-03, -3.50808091e-02, -3.53721479e-03, ...,
          8.26104068e-03, -3.04973301e-03, -1.46418256e-02]],

       ...,

       [[-3.56423927e-02,  5.54317507e-02,  3.91146242e-03, ...,
         -1.75050321e-02,  1.85085775e-02, -7.70937834e-03],
        [ 5.54317507e-02, -1.39482944e-02,  6.70113175e-03, ...,
          6.15454045e-02, -9.39428826e-03,  1.47123014e-02],
        [ 3.91146242e-03,  6.70113175e-03, -1.26818812e-02, ...,
          5.26829692e-03, -2.15772294e-03, -2.47514340e-04],
        ...,
        [-1.75050321e-02,  6.15454045e-02,  5.26829692e-03, ...,
          5.55692076e-04, -2.84333320e-02, -3.75207918e-03],
        [ 1.85085775e-02, -9.39428826e-03, -2.15772294e-03, ...,
         -2.84333320e-02,  1.87019548e-01, -8.06957153e-03],
        [-7.70937834e-03,  1.47123014e-02, -2.47514340e-04, ...,
         -3.75207918e-03, -8.06957153e-03,  2.07337395e-02]],

       [[ 8.84126066e-02,  1.82736920e-02, -5.65054043e-03, ...,
          2.64967036e-02,  4.50916773e-02, -2.10122413e-02],
        [ 1.82736920e-02,  7.86941550e-02,  2.28788104e-03, ...,
         -2.58770389e-03,  4.72064392e-03, -1.03766811e-02],
        [-5.65054043e-03,  2.28788104e-03, -1.35146704e-02, ...,
         -4.56388283e-03, -8.82118001e-04,  7.42603680e-03],
        ...,
        [ 2.64967036e-02, -2.58770389e-03, -4.56388283e-03, ...,
         -2.79773016e-02,  3.87947909e-03, -9.58019047e-03],
        [ 4.50916773e-02,  4.72064392e-03, -8.82118001e-04, ...,
          3.87947909e-03, -2.77131303e-01, -1.79673439e-03],
        [-2.10122413e-02, -1.03766811e-02,  7.42603680e-03, ...,
         -9.58019047e-03, -1.79673439e-03, -2.83423819e-02]],

       [[-1.28237432e-01, -4.30365174e-02, -2.66452665e-02, ...,
          2.37250928e-02,  1.16385374e-02,  2.30255891e-03],
        [-4.30365174e-02, -7.58918083e-03,  1.13716355e-02, ...,
         -1.11100667e-02, -1.67378386e-02, -1.29576697e-02],
        [-2.66452665e-02,  1.13716355e-02,  5.76768628e-03, ...,
         -1.05677681e-02, -6.82289674e-03, -8.07668275e-03],
        ...,
        [ 2.37250928e-02, -1.11100667e-02, -1.05677681e-02, ...,
         -1.14539347e-01, -7.11114704e-03, -2.89130556e-02],
        [ 1.16385374e-02, -1.67378386e-02, -6.82289674e-03, ...,
         -7.11114704e-03,  1.88275364e-01, -1.17071760e-02],
        [ 2.30255891e-03, -1.29576697e-02, -8.07668275e-03, ...,
         -2.89130556e-02, -1.17071760e-02, -1.77655951e-02]]]), array([[[-1.00892288e-01,  1.68401619e-02,  5.94680958e-03, ...,
          1.32470081e-02, -3.55464100e-02, -5.80109769e-03],
        [ 1.68401619e-02,  5.12440004e-02,  3.74259978e-02, ...,
          2.28063353e-02, -4.59434195e-02,  3.04559773e-03],
        [ 5.94680958e-03,  3.74259978e-02,  1.85461859e-02, ...,
          6.63725042e-03, -1.92107791e-02,  2.68634910e-03],
        ...,
        [ 1.32470081e-02,  2.28063353e-02,  6.63725042e-03, ...,
          1.38762590e-02, -4.56568837e-02,  7.74597253e-03],
        [-3.55464100e-02, -4.59434195e-02, -1.92107791e-02, ...,
         -4.56568837e-02,  3.10902626e-01, -1.16618553e-02],
        [-5.80109769e-03,  3.04559773e-03,  2.68634910e-03, ...,
          7.74597253e-03, -1.16618553e-02, -2.75534972e-02]],

       [[-8.19124603e-02, -1.07770267e-02,  2.73940856e-03, ...,
          5.42286117e-03,  1.43148441e-02,  9.38927405e-03],
        [-1.07770267e-02, -1.34434798e-02, -8.07094323e-03, ...,
         -4.91124351e-03, -5.50744405e-03, -6.47903095e-03],
        [ 2.73940856e-03, -8.07094323e-03,  1.43633784e-02, ...,
         -3.11791569e-03, -3.20759035e-03, -3.83808653e-03],
        ...,
        [ 5.42286117e-03, -4.91124351e-03, -3.11791569e-03, ...,
         -7.87596527e-02, -1.74101730e-02,  1.64075188e-02],
        [ 1.43148441e-02, -5.50744405e-03, -3.20759035e-03, ...,
         -1.74101730e-02, -1.33626522e-01, -2.42559627e-02],
        [ 9.38927405e-03, -6.47903095e-03, -3.83808653e-03, ...,
          1.64075188e-02, -2.42559627e-02,  5.97394271e-02]],

       [[ 1.38735034e-01,  2.42364407e-02, -1.14693608e-02, ...,
          6.99696322e-03, -1.72904490e-02,  7.51722121e-03],
        [ 2.42364407e-02, -2.39629923e-02,  6.97181439e-03, ...,
          2.22822148e-02,  1.36613812e-02,  3.50808091e-02],
        [-1.14693608e-02,  6.97181439e-03,  3.59474202e-03, ...,
          6.09146988e-03, -5.59217986e-04,  3.53721479e-03],
        ...,
        [ 6.99696322e-03,  2.22822148e-02,  6.09146988e-03, ...,
          1.87555406e-02,  5.20973244e-03, -8.26104068e-03],
        [-1.72904490e-02,  1.36613812e-02, -5.59217986e-04, ...,
          5.20973244e-03, -1.75241348e-01,  3.04973301e-03],
        [ 7.51722121e-03,  3.50808091e-02,  3.53721479e-03, ...,
         -8.26104068e-03,  3.04973301e-03,  1.46418256e-02]],

       ...,

       [[ 3.56423927e-02, -5.54317507e-02, -3.91146242e-03, ...,
          1.75050321e-02, -1.85085775e-02,  7.70937834e-03],
        [-5.54317507e-02,  1.39482944e-02, -6.70113175e-03, ...,
         -6.15454045e-02,  9.39428826e-03, -1.47123014e-02],
        [-3.91146242e-03, -6.70113175e-03,  1.26818812e-02, ...,
         -5.26829692e-03,  2.15772294e-03,  2.47514340e-04],
        ...,
        [ 1.75050321e-02, -6.15454045e-02, -5.26829692e-03, ...,
         -5.55692076e-04,  2.84333320e-02,  3.75207918e-03],
        [-1.85085775e-02,  9.39428826e-03,  2.15772294e-03, ...,
          2.84333320e-02, -1.87019548e-01,  8.06957153e-03],
        [ 7.70937834e-03, -1.47123014e-02,  2.47514340e-04, ...,
          3.75207918e-03,  8.06957153e-03, -2.07337395e-02]],

       [[-8.84126066e-02, -1.82736920e-02,  5.65054043e-03, ...,
         -2.64967036e-02, -4.50916773e-02,  2.10122413e-02],
        [-1.82736920e-02, -7.86941550e-02, -2.28788104e-03, ...,
          2.58770389e-03, -4.72064392e-03,  1.03766811e-02],
        [ 5.65054043e-03, -2.28788104e-03,  1.35146704e-02, ...,
          4.56388283e-03,  8.82118001e-04, -7.42603680e-03],
        ...,
        [-2.64967036e-02,  2.58770389e-03,  4.56388283e-03, ...,
          2.79773016e-02, -3.87947909e-03,  9.58019047e-03],
        [-4.50916773e-02, -4.72064392e-03,  8.82118001e-04, ...,
         -3.87947909e-03,  2.77131303e-01,  1.79673439e-03],
        [ 2.10122413e-02,  1.03766811e-02, -7.42603680e-03, ...,
          9.58019047e-03,  1.79673439e-03,  2.83423819e-02]],

       [[ 1.28237432e-01,  4.30365174e-02,  2.66452665e-02, ...,
         -2.37250928e-02, -1.16385374e-02, -2.30255891e-03],
        [ 4.30365174e-02,  7.58918083e-03, -1.13716355e-02, ...,
          1.11100667e-02,  1.67378386e-02,  1.29576697e-02],
        [ 2.66452665e-02, -1.13716355e-02, -5.76768628e-03, ...,
          1.05677681e-02,  6.82289674e-03,  8.07668275e-03],
        ...,
        [-2.37250928e-02,  1.11100667e-02,  1.05677681e-02, ...,
          1.14539347e-01,  7.11114704e-03,  2.89130556e-02],
        [-1.16385374e-02,  1.67378386e-02,  6.82289674e-03, ...,
          7.11114704e-03, -1.88275364e-01,  1.17071760e-02],
        [-2.30255891e-03,  1.29576697e-02,  8.07668275e-03, ...,
          2.89130556e-02,  1.17071760e-02,  1.77655951e-02]]])]

3、寻找函数参数正确定义

因为shap_values: numpy.array
对于单个输出解释,这是一个SHAP值的矩阵(# samples x # features)。
对于多输出解释,这是一个SHAP值矩阵的列表。

 可知,当前list数据并非size-1的数组,所以,无法转为scalars类型的数据

4、最终解决办法

RandomForestClassifier()改为<lightgbm.basic.Booster object at 0x00000210CCAAF898>,经过测试,输出以下,运行成功!

model_C: <lightgbm.basic.Booster object at 0x00000269F30A5860>
LGBMC <class 'numpy.ndarray'> (295, 7, 7) 
 [[[-2.03212277e-01  8.38473368e-02  9.27494035e-03 ...  3.76043663e-02
   -1.12195878e-01 -7.51591770e-03]
  [ 8.38473368e-02  8.67422668e-04  2.27826062e-02 ...  1.51550190e-02
   -1.28015053e-01  1.73530157e-04]
  [ 9.27494035e-03  2.27826062e-02  9.83802075e-02 ...  0.00000000e+00
   -1.22171674e-01  0.00000000e+00]
  ...
  [ 3.76043663e-02  1.51550190e-02  0.00000000e+00 ... -7.48850883e-02
   -6.75088971e-04 -7.39881971e-04]
  [-1.12195878e-01 -1.28015053e-01 -1.22171674e-01 ... -6.75088971e-04
    8.18119865e-01 -7.07531214e-03]
  [-7.51591770e-03  1.73530157e-04  0.00000000e+00 ... -7.39881971e-04
   -7.07531214e-03 -5.30181847e-03]]

 [[-2.03905009e-01  1.76970856e-02  3.60372896e-03 ...  6.59590565e-02
    3.25192369e-02  2.85055286e-03]
  [ 1.76970856e-02  7.42271902e-03  9.11656209e-03 ... -2.63576934e-02
   -1.41990904e-02 -4.81719902e-03]
  [ 3.60372896e-03  9.11656209e-03  4.88673814e-02 ...  0.00000000e+00
    6.82874328e-03  0.00000000e+00]
  ...
  [ 6.59590565e-02 -2.63576934e-02  0.00000000e+00 ... -8.03527385e-02
   -2.39716159e-02 -1.06731256e-03]
  [ 3.25192369e-02 -1.41990904e-02  6.82874328e-03 ... -2.39716159e-02
   -3.32003213e-01 -8.00174419e-03]
  [ 2.85055286e-03 -4.81719902e-03  0.00000000e+00 ... -1.06731256e-03
   -8.00174419e-03  2.47351615e-02]]

 [[ 3.65524524e-01  1.23282110e-01 -6.94995088e-03 ...  1.08536810e-01
   -1.80451184e-02 -5.55932748e-03]
  [ 1.23282110e-01 -5.94745798e-02 -6.94995088e-03 ...  3.15618035e-02
    3.78288199e-02 -1.76019586e-03]
  [-6.94995088e-03 -6.94995088e-03  2.85466167e-02 ...  0.00000000e+00
    2.48014752e-03  0.00000000e+00]
  ...
  [ 1.08536810e-01  3.15618035e-02  0.00000000e+00 ... -1.29775694e-01
    4.39073931e-02  1.56505599e-03]
  [-1.80451184e-02  3.78288199e-02  2.48014752e-03 ...  4.39073931e-02
   -3.65634236e-01 -7.31780875e-04]
  [-5.55932748e-03 -1.76019586e-03  0.00000000e+00 ...  1.56505599e-03
   -7.31780875e-04  2.39037391e-02]]

 ...

 [[ 4.63957818e-02 -1.58713899e-02  1.81106212e-03 ...  2.55113122e-02
   -5.77921525e-02  3.08940168e-04]
  [-1.58713899e-02 -1.35632524e-02 -1.18515307e-02 ... -3.50505266e-02
    1.52571779e-02 -3.73236510e-04]
  [ 1.81106212e-03 -1.18515307e-02  3.32397672e-02 ...  0.00000000e+00
    3.52229420e-03  0.00000000e+00]
  ...
  [ 2.55113122e-02 -3.50505266e-02  0.00000000e+00 ... -9.64323297e-02
    3.35029039e-02  1.61635537e-03]
  [-5.77921525e-02  1.52571779e-02  3.52229420e-03 ...  3.35029039e-02
   -3.98187413e-01 -3.02662989e-04]
  [ 3.08940168e-04 -3.73236510e-04  0.00000000e+00 ...  1.61635537e-03
   -3.02662989e-04 -9.08739780e-03]]

 [[-1.71996993e-01 -1.47872120e-02  3.85899956e-03 ... -1.50209005e-01
   -2.18026430e-01  1.39766423e-02]
  [-1.47872120e-02 -3.28965975e-01 -2.96173881e-02 ...  1.32839906e-01
    7.62613499e-02  1.98508032e-02]
  [ 3.85899956e-03 -2.96173881e-02  5.05960370e-02 ...  0.00000000e+00
   -2.96173881e-02  0.00000000e+00]
  ...
  [-1.50209005e-01  1.32839906e-01  0.00000000e+00 ... -1.54809945e-01
   -1.33767766e-01  1.49019359e-02]
  [-2.18026430e-01  7.62613499e-02 -2.96173881e-02 ... -1.33767766e-01
    6.61193249e-01 -3.50387426e-02]
  [ 1.39766423e-02  1.98508032e-02  0.00000000e+00 ...  1.49019359e-02
   -3.50387426e-02  3.15684010e-02]]

 [[ 3.71525973e-01  1.15451908e-01 -6.94995088e-03 ...  9.02379792e-02
   -2.25047248e-02 -5.55932748e-03]
  [ 1.15451908e-01 -5.05320365e-02 -6.94995088e-03 ...  1.02980057e-02
    3.28772676e-02 -1.76019586e-03]
  [-6.94995088e-03 -6.94995088e-03  2.85466167e-02 ...  0.00000000e+00
    2.48014752e-03  0.00000000e+00]
  ...
  [ 9.02379792e-02  1.02980057e-02  0.00000000e+00 ... -1.94072290e-01
    1.63458574e-02  1.56505599e-03]
  [-2.25047248e-02  3.28772676e-02  2.48014752e-03 ...  1.63458574e-02
   -3.58855532e-01 -7.31780875e-04]
  [-5.55932748e-03 -1.76019586e-03  0.00000000e+00 ...  1.56505599e-03
   -7.31780875e-04  2.39037391e-02]]]

Vue报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object 的解决方法(代码片段

问题是这样的:如下》》 

技术图片

解决办法:

1“:我的webpack 最高版本,造成 混用import和module.exports ;所以降低webpack版本 安装指定版本:npm install webpack@ -g 例如:npm install webpack@3.6.0 -g

删除 编译目录文件:

技术图片

 

2重新生成 然后输入启动命令:

 技术图片

 

成功解决!效果:

 

 

方法二:

 

 技术图片

 

 注释掉后一个

技术图片

 

 重新生成,启动项目 OK!

以上是关于成功解决TypeError: only size-1 arrays can be converted to Python scalars的主要内容,如果未能解决你的问题,请参考以下文章

请解决这个 TypeError: can only concatenate str (not "int") to str

TypeError: can only concatenate str (not “int“) to str

ORA-00257:archiver error. connect internal only,until freed 问题解决

ORA-00257:archiver error. connect internal only,until freed 问题解决

在 Electron 中,使用“typeof”会导致 TypeError: Cannot assign to read only property

TypeError: only integer scalar arrays can be converted to a scalar index