pythonpandastensorflowactivity-recognition

key error KeyError: ('accelerometerx', 'lx','mx','gx') i want to add the data of 4 hardware but when i try the error


please help i want the accelerometer data with gyro(gx) magnetometer(mx) but when ever i add this the KeyError: ('accelerometerx', 'lx', 'mx','gx') but when ever i enter just accelerometerx the code run well

df['Activity'].value_counts().plot(kind='bar', title='Training examples by activity type');

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

Solution

  • You cant select multiple columns like this. You have to create a list for the columns you want to select.

    Change this first line in the for loop to:

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