我正在使用 numpy 中的 100x3 字符串?dāng)?shù)據(jù)框,但這個(gè)問(wèn)題涉及一列,因此是一個(gè) 100x1 的 pandas 系列。我使用此函數(shù)將其轉(zhuǎn)換為 100x8x8x1 的棋盤(pán)陣列:def boardToNPArray(x): x = chess.Board(x) x=x.__str__() x = x.split("\n") for n in range(len(x)): x[n] = np.array(x[n].split()).reshape(8,1) return np.array(x)asdf['FEN'] = asdf['FEN'].apply(lambda x : boardToNPArray(x))這應(yīng)該使它成為一個(gè)長(zhǎng)度為 100 的數(shù)據(jù)幀,其中包含 8x8x1 的棋盤(pán) numpy 數(shù)組。然后我執(zhí)行 asdf['FEN'].values 將數(shù)據(jù)幀轉(zhuǎn)換為 numpy 數(shù)組。asdf['FEN'].values# Which returnsarray([array([[['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.']], [['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.']], [['.'], ['.'], ['.'], ['.'], ['.'], ['k'], ['.'], ['.']], [['R'], ['.'], ['.'], ['.'], ['.'], ['.'], ['p'], ['.']], [['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['P'], ['.']], [['.'], ['.'], ['K'], ['.'], ['.'], ['.'], ['.'], ['.']], [['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['.']], [['.'], ['.'], ['.'], ['.'], ['.'], ['.'], ['r'], ['.']]], dtype='<U1'),# This is one 8x8x1 entry in the 理論上,這應(yīng)該可以達(dá)到我的目標(biāo)——一個(gè) 100x8x8x1 的 numpy 數(shù)組。然而,在跑步時(shí)asdf['FEN'].shape它返回(100,)而在跑步的時(shí)候asdf['FEN'][0].shape它返回(8,8,1)兩者的 type() 都是 numpy.ndarray 為什么這不是 100x8x8x1 數(shù)組?
1 回答
繁星淼淼
TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
嘗試應(yīng)用于numpy.stack以下結(jié)果Series.values:
s = pd.Series(map(np.array,map(list,['asdf','qwer']))) np.stack(s.values).shape # (2,4)
添加回答
舉報(bào)
0/150
提交
取消
