繁体   English   中英

IndexError:访问numpy ndarray形状时元组索引超出范围

[英]IndexError: tuple index out of range while accessing numpy ndarray shape

我正进入(状态

Index error: Tuple index out of range while doing one-hot encoding for smile string at line: 
    if ans.shape[1]<length:

例子:

smiles_string= ['C', 'C', '1', '=', 'C', 'C', '(', '=', 'C', 'N', '=', 'C', '1', 'C', '2', '=', 'C', 'C', '(', '=', 'N', 'C', '=', 'C', '2', ')', 'C', ')', 'C', 'C', '(', '=', 'O', ')', 'N', 'C', '3', '=', 'N', 'C', '=', 'C', '(', 'C', '=', 'C', '3', ')', 'C', '4', '=', 'N', 'C', '=', 'C', 'N', '=', 'C', '4']
char_list=['7', '2', '.', 'Br', 'Pt', '=', '[', 'F', '(', ')', 'O', '6', 'S', '5', '1', 'I', ']', '+', '8', '#', 'C', '3', 'B', '9', 'Cl', 'P', '-', '4', 'N']
def onehot_encode(char_list, smiles_string, length):
    encode_row = lambda char: map(int, [c == char for c in smiles_string])
    print(encode_row)
    ans = np.array(map(encode_row, char_list))
    if ans.shape[1] < length:
        residual = np.zeros((len(char_list), length - ans.shape[1]), dtype=np.int8)
        ans = np.concatenate((ans, residual), axis=1)
    return ans

我试过调试,发现ans数组的形状是(),这不应该发生。

如果有人可以提供有关如何映射并解决错误的想法,将不胜感激。

先感谢您。

你在没有形状的东西上调用.shape()

print(ans)
Out[1]: <map object at 0x000001E9352B8908>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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