python
1# -*- coding: utf-8 -*- 2import matplotlib.figure as figure 3import matplotlib.pyplot as plt 4import numpy as np 5import pandas as pd 6import seaborn as sns 7import optuna 8import lightgbm as lgb 9from scipy.spatial.distance import cdist 10from sklearn import metrics 11from sklearn.model_selection import train_test_split 12 13 14#optuna 調整用の関数 15def tyousei(autoscaled_x_train, autoscaled_y_train, autoscaled_x_test, autoscaled_y_test): 16 def Gini(y_true, y_pred): 17 assert y_true.shape == y_pred.shape 18 n_samples = y_true.shape[0] 19 arr = np.array([y_true, y_pred]).transpose() 20 true_order = arr[arr[:, 0].argsort()][::-1, 0] 21 pred_order = arr[arr[:, 1].argsort()][::-1, 0] 22 L_true = np.cumsum(true_order) * 1. / np.sum(true_order) 23 L_pred = np.cumsum(pred_order) * 1. / np.sum(pred_order) 24 L_ones = np.linspace(1 / n_samples, 1, n_samples) 25 G_true = np.sum(L_ones - L_true) 26 G_pred = np.sum(L_ones - L_pred) 27 return G_pred * 1. / G_true 28 29 30 def objectives(trial): 31 params = { 32 'objective': 'regression', 33 'max_bin': trial.suggest_int('max_bin', 1, 200), 34 'learning_rate': trial.suggest_int('learning_rate', 0.05, 0.9), 35 'max_depth': -1, # 木の数 (負の値で無制限) 36 'num_leaves': trial.suggest_int('num_leaves', 2, 10), 37 'metric': ('mean_absolute_error', 'mean_squared_error', 'rmse'), 38 'min_child_samples':trial.suggest_int('min_child_samples', 1, 5), 39 } 40 41 42 lgb_train = lgb.Dataset(autoscaled_x_train, autoscaled_y_train) 43 lgb_eval = lgb.Dataset(autoscaled_x_test, autoscaled_y_test, reference=lgb_train) 44 45 model = lgb.train(params, lgb_train,valid_sets=[lgb_train, lgb_eval],verbose_eval=10,num_boost_round=1000,early_stopping_rounds=10) 46 47 y_pred_valid = model.predict(autoscaled_x_test, num_iteration=model.best_iteration)* y_train.std() + y_train.mean() 48 49 score=Gini(y_test, y_pred_valid) 50 return score 51 52 53 study = optuna.create_study(sampler=optuna.samplers.RandomSampler(seed=0)) 54 study.optimize(objectives, n_trials=200) 55 study.best_params 56 print("study.best_params",study.best_params) 57 print("study.best_value",study.best_value) 58 return study.best_value 59
python プログラムで、メインのプログラムと、関数のプログラムをファイルを別にして書いています。
メインのプログラム中で関数を書いて実行すると動作するのですが、動作確認済みの関数を関数をまとめているファイルに移して、関数をまとめているファイルをimportして実行すると下記エラーが発生してしまいます。
原因をご教示頂けたら幸いです。
動作させたいプログラムは、LightGBMをopentunaを用いてハイパーパラメータを調節するものです。
実行環境は、jupiternotebook 又は、 google Colaboratoryを使用しています。
宜しくお願い致します。
--- Logging error ---
ERROR:root:
UnicodeDecodeError while processing traceback.
ERROR:root:
UnicodeDecodeError while processing traceback.
ERROR:root:
UnicodeDecodeError while processing traceback.
During handling of the above exception, another exception occurred:
UnicodeDecodeError Traceback (most recent call last)
~\Anaconda3\lib\logging_init_.py in emit(self, record)
1033 try:
-> 1034 msg = self.format(record)
1035 stream = self.stream
~\Anaconda3\lib\logging_init_.py in format(self, record)
879 fmt = _defaultFormatter
--> 880 return fmt.format(record)
881
~\Anaconda3\lib\site-packages\colorlog\colorlog.py in format(self, record)
122 if sys.version_info > (2, 7):
--> 123 message = super(ColoredFormatter, self).format(record)
124 else:
~\Anaconda3\lib\logging_init_.py in format(self, record)
626 if not record.exc_text:
--> 627 record.exc_text = self.formatException(record.exc_info)
628 if record.exc_text:
~\Anaconda3\lib\logging_init_.py in formatException(self, ei)
576 # traceback.print_stack(tb.tb_frame.f_back, file=sio)
--> 577 traceback.print_exception(ei[0], ei[1], tb, None, sio)
578 s = sio.getvalue()
~\Anaconda3\lib\traceback.py in print_exception(etype, value, tb, limit, file, chain)
103 for line in TracebackException(
--> 104 type(value), value, tb, limit=limit).format(chain=chain):
105 print(line, file=file, end="")
~\Anaconda3\lib\traceback.py in init(self, exc_type, exc_value, exc_traceback, limit, lookup_lines, capture_locals, _seen)
507 walk_tb(exc_traceback), limit=limit, lookup_lines=lookup_lines,
--> 508 capture_locals=capture_locals)
509 self.exc_type = exc_type
~\Anaconda3\lib\traceback.py in extract(klass, frame_gen, limit, lookup_lines, capture_locals)
362 for f in result:
--> 363 f.line
364 return result
~\Anaconda3\lib\traceback.py in line(self)
284 if self._line is None:
--> 285 self._line = linecache.getline(self.filename, self.lineno).strip()
286 return self._line
~\Anaconda3\lib\linecache.py in getline(filename, lineno, module_globals)
15 def getline(filename, lineno, module_globals=None):
---> 16 lines = getlines(filename, module_globals)
17 if 1 <= lineno <= len(lines):
~\Anaconda3\lib\linecache.py in getlines(filename, module_globals)
46 try:
---> 47 return updatecache(filename, module_globals)
48 except MemoryError:
~\Anaconda3\lib\linecache.py in updatecache(filename, module_globals)
136 with tokenize.open(fullname) as fp:
--> 137 lines = fp.readlines()
138 except OSError:
~\Anaconda3\lib\codecs.py in decode(self, input, final)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 459: invalid start byte
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
2017 # in the engines. This should return a list of strings.
-> 2018 stb = value.render_traceback()
2019 except Exception:
AttributeError: 'UnicodeDecodeError' object has no attribute 'render_traceback'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_code(self, code_obj, result, async_)
3282 if result is not None:
3283 result.error_in_exec = sys.exc_info()[1]
-> 3284 self.showtraceback(running_compiled_code=True)
3285 else:
3286 outflag = False
~\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
2019 except Exception:
2020 stb = self.InteractiveTB.structured_traceback(etype,
-> 2021 value, tb, tb_offset=tb_offset)
2022
2023 self._showtraceback(etype, value, stb)
~\Anaconda3\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
1377 self.tb = tb
1378 return FormattedTB.structured_traceback(
-> 1379 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1380
1381
~\Anaconda3\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
1280 # Verbose modes need a full traceback
1281 return VerboseTB.structured_traceback(
-> 1282 self, etype, value, tb, tb_offset, number_of_lines_of_context
1283 )
1284 elif mode == 'Minimal':
~\Anaconda3\lib\site-packages\IPython\core\ultratb.py in structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
1142 exception = self.get_parts_of_chained_exception(evalue)
1143 if exception:
-> 1144 formatted_exceptions += self.prepare_chained_exception_message(evalue.cause)
1145 etype, evalue, etb = exception
1146 else:
TypeError: can only concatenate str (not "list") to str
あなたの回答
tips
プレビュー