代码1

需要将模型中对test数据的prediction值进行分类,分类标准是大于0.5的为1类,即相似,小于0.5的为0类,即不相似,并写入文件。

字符到底是什么

文件读入写出都是字符? 

字符大小比较,比较那个0.5错没有,accsi表中没有0.5,只有0到9,那怎么比较124和125的字符串呢?

错误1:

f = open('/home/xbwang/xingyu/predictions/results-dependency.1l.150d.epoch-1.0.31137.10005.pred','r')
for line in f:
    #print(line)
    if line>0.5 :
        line = 1
    elif line<0.5 :
        line = 0
    print(line)
    f1=open('/home/xbwang/Desktop/another-results-dependency.1l.150d.epoch-1.0.35192.10005.pred','a')
    f1.write(line+'
')

命令行报错误:Traceback (most recent call last):
        File "count.py",line 8,in <module>
          f1.write(line+' ')
       TypeError: unsupported operand type(s) for +: 'int' and 'str'

错误2:

f = open('/home/xbwang/xingyu/predictions/results-dependency.1l.150d.epoch-1.0.31137.10005.pred','r')
for line in f:
    #print(line)
#if line>0.5 :
# print(line) if line>0.5 : line = '1'
  
elif line<0.5 : line = '0' #print(line) f1=open('/home/xbwang/Desktop/another-results-dependency.1l.150d.epoch-1.0.35192.10005.pred','a') f1.write(line+' ')

错误:所要写入的文件里全是1

正确代码:

f = open('/home/xbwang/xingyu/predictions/results-dependency.1l.150d.epoch-1.0.31137.10005.pred','r')
for line in f:
    #print(line)
    if line>'0.5' :
        line = '1'
    elif line<'0.5' :
        line = '0'
    print(line)
    f1=open('/home/xbwang/Desktop/sb.txt','a')
    f1.write(line+'
')
原文地址:https://www.cnblogs.com/ymjyqsx/p/6238134.html