我正在尝试从python脚本导入一个函数,该脚本使用我的活动Jupyter笔记本中存在的变量。当我直接将功能复制到笔记本中并运行它们时,它们可以工作,但是当我导入它们时,它们无法识别我的变量。
这是我导入的脚本scripting_bayes.py:
import xgboost as xgb
import numpy as np
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
def optimize(X_train, y_train, X_val, y_val):
def hyperopt_train_test(params, X_train, y_train, X_val, y_val):
xg = xgb.XGBClassifier(**params,
nthread=10,
objective='binary:logistic')
xg.fit(X_train_os, y_train_os)
y_pred = xg.predict(X_val)
return recall_score(y_val, y_pred)
def f(params):
recall = hyperopt_train_test(params, X_train, y_train, X_val, y_val)
return {'loss': -recall, 'status': STATUS_OK}
trials = Trials()
space = {
'min_child_weight': hp.quniform('min_child_weight', 1, 10, 1),
'gamma': hp.uniform('gamma', 0.5, 5),
'subsample': hp.uniform('subsample', 0.05, 0.2),
'colsample_bytree': hp.uniform('colsample_bytree', 0.1, 0.6),
'max_depth': hp.choice('max_depth', np.arange(3, 6, dtype=int))
}
best_params = fmin(f, space, algo=tpe.suggest, max_evals=100, trials=trials)
return best_params
然后我在Jupter笔记本中将X_train_os,Y_train_os,X_val和y_val作为活动的numpy数组。当我运行导入并调用该函数时,出现一个错误,它无法识别我对X_train_os的第一个变量引用。
from modeling_bayes import optimize
best_parameters = optimize(X_train_os, y_train_os, X_val, y_val)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-aafa5107a6e9> in <module>()
----> 1 best_parameters = optimize(X_train_os, y_train_os, X_val, y_val)
D:\main-project\sandbox\pipeline\V2\modeling_bayes.py in optimize(X_train, y_train, X_val, y_val)
48 }
49
---> 50 best_params = fmin(f, space, algo=tpe.suggest, max_evals=100, trials=trials)
51 return best_params
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len)
365 verbose=verbose,
366 catch_eval_exceptions=catch_eval_exceptions,
--> 367 return_argmin=return_argmin,
368 )
369
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\base.py in fmin(self, fn, space, algo, max_evals, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin)
633 pass_expr_memo_ctrl=pass_expr_memo_ctrl,
634 catch_eval_exceptions=catch_eval_exceptions,
--> 635 return_argmin=return_argmin)
636
637
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len)
383 max_queue_len=max_queue_len)
384 rval.catch_eval_exceptions = catch_eval_exceptions
--> 385 rval.exhaust()
386 if return_argmin:
387 return trials.argmin
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\fmin.py in exhaust(self)
242 def exhaust(self):
243 n_done = len(self.trials)
--> 244 self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
245 self.trials.refresh()
246 return self
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\fmin.py in run(self, N, block_until_done)
216 else:
217 # -- loop over trials and do the jobs directly
--> 218 self.serial_evaluate()
219
220 if stopped:
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\fmin.py in serial_evaluate(self, N)
135 ctrl = base.Ctrl(self.trials, current_trial=trial)
136 try:
--> 137 result = self.domain.evaluate(spec, ctrl)
138 except Exception as e:
139 logger.info('job exception: %s' % str(e))
C:\ProgramData\Anaconda3\lib\site-packages\hyperopt\base.py in evaluate(self, config, ctrl, attach_attachments)
838 memo=memo,
839 print_node_on_error=self.rec_eval_print_node_on_error)
--> 840 rval = self.fn(pyll_rval)
841
842 if isinstance(rval, (float, int, np.number)):
D:\main-project\sandbox\pipeline\V2\modeling_bayes.py in f(params)
18
19 def f(params):
---> 20 recall = hyperopt_train_test(params, X_train, y_train, X_val, y_val)
21 return {'loss': -recall, 'status': STATUS_OK}
22
D:\main-project\sandbox\pipeline\V2\modeling_bayes.py in hyperopt_train_test(params, X_train, y_train, X_val, y_val)
11 objective='binary:logistic')
12
---> 13 xg.fit(X_train_os, y_train_os)
14 y_pred = xg.predict(X_val)
15 return recall_score(y_val, y_pred)
NameError: name 'X_train_os' is not defined
答案 0 :(得分:0)
内部hyperopt_train_test
接受参数X_train, y_train
,而内部则使用X_train_os, y_train_os
。这是错字吗?