无法在 numpy 中拆分数据框

问题描述 投票:0回答:1

无法使用 numpy split 函数将数据帧的子集分配给

cols =["fLength","fWidth","fSize","fConc","fConcl","fAsym","fM3Long","fAlpha","fDist","class"]
df = pd.read_csv("magic04.data",names = cols)
df['class'] = (df['class']=='g').astype(int)

train, valid, test = np.split(df.sample(frac=1), [int(0.6*len(df)) , int(0.8*len(df)), ])

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.9/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3628             try:
-> 3629                 return self._engine.get_loc(casted_key)
   3630             except KeyError as err:

17 frames
KeyError: 0

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.9/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3629                 return self._engine.get_loc(casted_key)
   3630             except KeyError as err:
-> 3631                 raise KeyError(key) from err
   3632             except TypeError:
   3633                 # If we have a listlike key, _check_indexing_error will raise

尝试阅读文档但没有发现任何有用的东西。

python dataframe numpy machine-learning keyerror
1个回答
0
投票

您的代码中的错误是您正在尝试将 numpy 例程与 pandas 数据框一起使用。解决这个问题的最好方法是将您的

df.sample
转换为 numpy 数组,然后使用
np.split()
.

试试这个:

npsample=np.array(df.sample(frac=1))
train, valid, test = np.split(npsample, [int(0.6*len(npdata)) , int(0.8*len(npdata)), ])

试试这个,因为它在我的 VSCode 上运行得很好。

© www.soinside.com 2019 - 2024. All rights reserved.