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

我得到了

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','=','(','N','2',')','O','3','4','4']
char_list=['7','.','Br','Pt','[','F','6','S','5','I',']','+','8','#','B','9','Cl','P','-','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数组的形状为(),这不应该发生。

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

谢谢。

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

您正在呼叫.shape(),因为它没有形状。

print(ans)
Out[1]: <map object at 0x000001E9352B8908>
本文链接:https://www.f2er.com/2997700.html

大家都在问