简体   繁体   中英

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

I am getting

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

example:

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

I tried debugging and I found, shape of ans array is (), which should not happen.

If anyone can give an idea about how to map this and solve the error, would be appreciated.

Thank you in advance.

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

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

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