程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

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

編輯:Python

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

目錄

解決問題

解決思路

解決方法

1、分析問題

2、測試bug

3、Look for function parameters to be properly defined

4、最終解決辦法


解決問題

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

解決思路

類型錯誤:只有size-1An array of can be converted to 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) # 返回 Estimate a set of samples 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、Look for function parameters to be properly defined

因為shap_values: numpy.array
Explain for a single output,這是一個SHAP值的矩陣(# samples x # features).
For multiple output explanations,這是一個SHAPlist of value matrices.

 可知,當前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]]]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved