关键错误KeyError :('accelerometerx','lx','mx','gx')我想添加4种硬件的数据,但是当我尝试错误时

时间:2019-11-28 10:23:09

标签: python pandas tensorflow activity-recognition

请帮助我获取带陀螺仪(gx)磁力计(mx)的加速度计数据,但是每当我添加此 KeyError :('accelerometerx','lx','mx','gx'),但是每当我输入accelerometerx时,代码都能很好地运行

df ['Activity']。value_counts()。plot(kind ='bar',title ='按活动类型培训示例');

def plot_activity(Activity, df):
    data = df[df['Activity'] == Activity][['gx', 'gy', 'gz','mx','my','mz','lx','ly','lz','accelerometerx','accelerometery','accelerometerz']][:200]
    axis = data.plot(subplots=True, figsize=(8, 6), 
                     title=Activity)
    for ax in axis:
        ax.legend(loc='lower left', bbox_to_anchor=(1.0, 0.5))
        df=df.astype(float)
plot_activity(11111, df)


N_TIME_STEPS = 200
N_FEATURES = 3
step = 20
segments = []
labels = []
for i in range(0, len(df) - N_TIME_STEPS, step):
    xs = df['accelerometerx', 'lx', 'mx','gx'].values[i: i + N_TIME_STEPS]
    ys = df['accelerometery'].values[i: i + N_TIME_STEPS]
    zs = df['accelerometerz'].values[i: i + N_TIME_STEPS]
    label = stats.mode(df['Activity'][i: i + N_TIME_STEPS])[0][0]
    segments.append([xs, ys, zs])
    labels.append(label)
    np.array(segments).shape
reshaped_segments = np.asarray(segments, dtype= np.float32).reshape(-1, N_TIME_STEPS, N_FEATURES)
labels = np.asarray(pd.get_dummies(labels), dtype = np.float32)

reshaped_segments.shape

1 个答案:

答案 0 :(得分:1)

您不能选择多个这样的列。您必须为要选择的列创建一个列表。

for循环中的第一行更改为:

xs = df[['accelerometerx', 'lx', 'mx','gx']].values[i: i + N_TIME_STEPS]