以下のようにそれぞれの変数の重要度を算出しました。
importance = pd.DataFrame({ '変数' :df_X.columns, '重要度' :gbm.feature_importances_}) importance = importance.sort_values('重要度', ascending=False) # 降順ソート importance
変数 重要度 3 β-EEG 1806 2 α-EEG 1719 1 θ-EEG 1582 0 δ-EEG 1531
これを以下のように、重要度の合計=1にして、それぞれの変数の値を%で表示するにはどうすればよいでしょうか?
変数 重要度 3 β-EEG 0.2653 2 α-EEG 0.2558 1 θ-EEG 0.2490 0 δ-EEG 0.2299
以下のように試しましたが、エラーになってしまいます。
お詳しい方、ご指導お願いいたします。
importance = pd.DataFrame({ '変数' :df_X.columns, '重要度' :gbm.feature_importances_}) importance = importance.sort_values('重要度', ascending=False) # 降順ソート importance = importance / np.sum(importance) importance
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in na_op(x, y) 1504 try: -> 1505 result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs) 1506 except TypeError: C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs) 207 if use_numexpr: --> 208 return _evaluate(op, op_str, a, b, **eval_kwargs) 209 return _evaluate_standard(op, op_str, a, b) C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b, truediv, reversed, **eval_kwargs) 122 if result is None: --> 123 result = _evaluate_standard(op, op_str, a, b) 124 C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b, **eval_kwargs) 67 with np.errstate(all='ignore'): ---> 68 return op(a, b) 69 TypeError: unsupported operand type(s) for /: 'str' and 'str' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in safe_na_op(lvalues, rvalues) 1528 with np.errstate(all='ignore'): -> 1529 return na_op(lvalues, rvalues) 1530 except Exception: C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in na_op(x, y) 1506 except TypeError: -> 1507 result = masked_arith_op(x, y, op) 1508 C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in masked_arith_op(x, y, op) 1025 with np.errstate(all='ignore'): -> 1026 result[mask] = op(xrav[mask], y) 1027 TypeError: unsupported operand type(s) for /: 'str' and 'str' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-195-b8e79d3fe232> in <module> 1 importance = pd.DataFrame({ '変数' :df_X.columns, '重要度' :gbm.feature_importances_}) 2 importance = importance.sort_values('重要度', ascending=False) # 降順ソート ----> 3 importance = importance / np.sum(importance) 4 importance C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in f(self, other, axis, level, fill_value) 2028 return _combine_series_frame(self, other, pass_op, 2029 fill_value=fill_value, axis=axis, -> 2030 level=level) 2031 else: 2032 if fill_value is not None: C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in _combine_series_frame(self, other, func, fill_value, axis, level) 1928 1929 # default axis is columns -> 1930 return self._combine_match_columns(other, func, level=level) 1931 1932 C:\python\anaconda\pgm\lib\site-packages\pandas\core\frame.py in _combine_match_columns(self, other, func, level) 5114 copy=False) 5115 assert left.columns.equals(right.index) -> 5116 return ops.dispatch_to_series(left, right, func, axis="columns") 5117 5118 def _combine_const(self, other, func): C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in dispatch_to_series(left, right, func, str_rep, axis) 1155 raise NotImplementedError(right) 1156 -> 1157 new_data = expressions.evaluate(column_op, str_rep, left, right) 1158 1159 result = left._constructor(new_data, index=left.index, copy=False) C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs) 206 use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) 207 if use_numexpr: --> 208 return _evaluate(op, op_str, a, b, **eval_kwargs) 209 return _evaluate_standard(op, op_str, a, b) 210 C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b, truediv, reversed, **eval_kwargs) 121 122 if result is None: --> 123 result = _evaluate_standard(op, op_str, a, b) 124 125 return result C:\python\anaconda\pgm\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b, **eval_kwargs) 66 _store_test_result(False) 67 with np.errstate(all='ignore'): ---> 68 return op(a, b) 69 70 C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in column_op(a, b) 1142 def column_op(a, b): 1143 return {i: func(a.iloc[:, i], b.iloc[i]) -> 1144 for i in range(len(a.columns))} 1145 1146 elif isinstance(right, ABCSeries): C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in <dictcomp>(.0) 1142 def column_op(a, b): 1143 return {i: func(a.iloc[:, i], b.iloc[i]) -> 1144 for i in range(len(a.columns))} 1145 1146 elif isinstance(right, ABCSeries): C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in wrapper(left, right) 1581 rvalues = rvalues.values 1582 -> 1583 result = safe_na_op(lvalues, rvalues) 1584 return construct_result(left, result, 1585 index=left.index, name=res_name, dtype=None) C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in safe_na_op(lvalues, rvalues) 1531 if is_object_dtype(lvalues): 1532 return libalgos.arrmap_object(lvalues, -> 1533 lambda x: op(x, rvalues)) 1534 raise 1535 pandas/_libs/algos.pyx in pandas._libs.algos.arrmap() C:\python\anaconda\pgm\lib\site-packages\pandas\core\ops.py in <lambda>(x) 1531 if is_object_dtype(lvalues): 1532 return libalgos.arrmap_object(lvalues, -> 1533 lambda x: op(x, rvalues)) 1534 raise 1535 TypeError: unsupported operand type(s) for /: 'str' and 'str'
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/11 06:17
2020/08/11 21:03 編集
2020/08/11 23:01