早教吧 育儿知识 作业答案 考试题库 百科 知识分享

python程序gridregression.py运行出错:NameError:globalname'outfilename'isnotdefined错误如下:Traceback(mostrecentcalllast):File"D:\huigui\libsvm\Python24\gridregression.py",line281,in?resultfile=open(outfilename

题目详情
python程序gridregression.py运行出错: NameError: global name 'out_filename' is not defined 错误如下:
Traceback (most recent call last):
File "D:\huigui\libsvm\Python24\gridregression.py", line 281, in ?
result_file = open(out_filename,'w',0)
NameError: global name 'out_filename' is not defined
部分程序代码如下:
#global parameters and their default values
fold = 5
c_begin, c_end, c_step = -1, 6, 1
g_begin, g_end, g_step = 0, -8, -1
p_begin, p_end, p_step = -8, -1, 1
global dataset_pathname, dataset_title, pass_through_string
global out_filename, png_filename
# experimental
ssh_workers = []
# ssh_workers = ['linux1','linux1','linux2','linux2','linux3', 'linux4', 'linux6','linux7','linux8','linux8','linux9','linux10','linux11','linux12']
nr_local_worker = 1
# process command line options, set global parameters
def process_options(argv=sys.argv):
global fold
global c_begin, c_end, c_step
global g_begin, g_end, g_step
global p_begin, p_end, p_step
global dataset_pathname, dataset_title, pass_through_string
global svmtrain_exe, gnuplot_exe, gnuplot, out_filename, png_filename
usage = """\
Usage: grid.py [-log2c begin,end,step] [-log2g begin,end,step] [-log2p begin,end,step] [-v fold]
[-svmtrain pathname] [-gnuplot pathname] [-out pathname] [-png pathname]
[additional parameters for svm-train] dataset"""
def main():
# set parameters
process_options()
# gather results

done_jobs = {}
result_file = open(out_filename,'w',0)
db = []
best_mse = 100000000
for line in jobs:
for (c,g,p) in line:
while not done_jobs.has_key((c,g,p)):
(worker,c1,g1,p1,mse) = result_queue.get()
done_jobs[(c1,g1,p1)] = mse
result_file.write('%s %s %s %s\n' %(c1,g1,p1,mse))
result_file.flush()
print "[%s] %s %s %s %s" % (worker,c1,g1,p1,mse),
if mse < best_mse:
best_mse = mse
best_c = 2.0**c1
best_g = 2.0**g1
best_p = 2.0**p1
print " (best c=%s, g=%s, p=%s, mse=%s)" % \
(best_c, best_g, best_p, best_mse)
# db.append((c,g,r,done_jobs[(c,g,r)]))
job_queue.put((WorkerStopToken,None,None))
print "%s %s %s %s" % (best_c, best_g, best_p, best_mse)
main()
▼优质解答
答案和解析
你好:
这是全局变量的用法;
要想在一个函数里面用全局函数:
需要现在外面声明一下;
然后在函数里面使用:global声明