重现此内容的最小示例是:
import numpy
numpy.loadtxt("data.txt", delimiter='\t')
data.txt
为:
A R N D C Q
A 5 -2 -1 -2 -1 -1
R -2 7 -1 -2 -4 1
N -1 -1 7 2 -2 0
运行代码时出现 ValueError:
[root@mycomp]$ python Needleman-Wunsch.py
Traceback (most recent call last):
File "Needleman-Wunsch.py", line 92, in <module>
(alignedSeq1, alignedSeq2) = computeFMatrix(seq1, seq2, -6)
File "Needleman-Wunsch.py", line 34, in computeFMatrix
similarityMatrixMap = readBLOSUM50("BLOSUM50.txt")
File "Needleman-Wunsch.py", line 16, in readBLOSUM50
similarityMatrix = np.loadtxt(fileName, delimiter='\t')
File "/usr/local/lib/python2.7/site-packages/numpy/lib/npyio.py", line 827, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: could not convert string to float: A R N D C Q E G H I L K M F P S T W Y V
你也可以有 the original BLOSUM50.txt
file完整代码来自the link above .
删除 BLOSUM50.txt
的第一行给出了同样的错误。
最佳答案
您可以将 loadtxt
替换为
numpy.genfromtxt("data.txt", delimiter='\t', skip_header=True)[:, 1:]
这会跳过标题,将列名转换为 nan
,然后将它们切掉。
关于python - numpy.loadtxt "could not convert string to float",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212579/