简体   繁体   中英

ValueError: could not convert string to float -- how to convert list of numpy array to float?

I am using protein-ligand affinity value file as input that return its values to list. Code is

def sep_input_file(input_file):

lines = preprocessing(read_file(open(input_file,"r")))

if lines.shape[1] == 2:
    return list(lines[:,0]), list(lines[:,1]), list(np.ones(len(lines)))
elif lines.shape[1] == 3:
    return list(lines[:,0]), list(lines[:,1]), list(np.array(lines[:,2], dtype = np.float32))

Input file is:

protein ligand affinity
./data/complexes/2hyyProtein.mol2 ./data/complexes/2hyyLigand.mol2 10.2

Please suggest me how to resolve it?

Getting the error

error:

return list(lines[:,0]), list(lines[:,1]), list(np.array(lines[:,2], dtype = np.float32))
ValueError: could not convert string to float:

There are most likely strings that can't be parsed into floats in the affinity column. Try replacing

list(np.array(lines[:,2], dtype = np.float32))

with

result = []
for data in lines[:,2]:
    try:
        value = float(data.replace(',', '.').strip())
    except (ValueError, TypeError):
        value = None
    result.append(value)

There will be None values in the result where the parsing fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM