<jupyter_start><jupyter_text>This notebook has been designed for the nurse care activity recognition challenge competition with the the aim of providing the basic knowledge of Human Activity Recognition by accelerometer.It has been made by <NAME>.  Exploring the data<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline<jupyter_output><empty_output><jupyter_text>First, we have to load the data and label file. In this tutorial, we only use the train data of 1 user to reduce the time of the whole process.<jupyter_code>#Load data
df=pd.read_csv('acc_user2.csv')
df_label=pd.read_csv('label/label_train.csv')<jupyter_output><empty_output><jupyter_text>Let's check what information the data contains.<jupyter_code>df.head(5)<jupyter_output><empty_output><jupyter_text>We can see that the data file contains 5 columns: subject_id, datetime, and 3 coordinates of the accelerometer data.<jupyter_code>df_label.head(5)<jupyter_output><empty_output><jupyter_text>For label file, we have 8 columns: id (label id), user_id, activity_type_id, activity_type (name), target_id (patients), activity2user_id, start and finish timestamp of the activity. Visualization the data<jupyter_code>df.plot()<jupyter_output><empty_output><jupyter_text>Check the bar plot of labels<jupyter_code>activity_map = {
1: "Vital", 
2: "Meal / medication", 
3: "Oral care", 
4: "Excretion", 
5: "Bathing / wiping",
6: "Treatment", 
7: "Morning gathering / exercises", 
8: "Rehabilitation / recreation", 
9: "Morning care", 
10: "Daytime user response", 
11: "Night care", 
12: "Nighttime user response", 
13: "Family / guest response", 
14: "Outing response", 
19: "Get up assistance", 
20: "Change dressing assistance", 
21: "Washing assistance", 
27: "Emergency response such as acciden",
15: "Linen exchange", 
16: "Cleaning",
23: "Preparation and checking of goods", 
24: "Organization of medications",
17: "Handwriting recording", 
18: "Delegating / meeting", 
22: "Doctor visit correspondence", 
25: "Family / doctor contact",
26: "Break", 
28: "Special remarks / notes"
}

df_label["activity_type_e"]=df_label["activity_type_id"].map(activity_map)
df_label['activity_type_e'].value_counts().plot.bar()<jupyter_output><empty_output><jupyter_text>Pre-processing the data The missing value and duplicated value rows are dropped. But we can also use several methods to handle missing value, it depends on your pipelines.All the values are sorted by datetime<jupyter_code>df = df.dropna()
df = df.sort_values('datetime')
df = df.drop_duplicates()<jupyter_output><empty_output><jupyter_text>In the label file, nan and duplicated value rows are also dropped.<jupyter_code>df_label = df_label.drop_duplicates('id')
df_label = df_label.dropna()<jupyter_output><empty_output><jupyter_text>Now, we only get the label of user we utilize<jupyter_code>df_label_user=df_label[df_label['user_id']==df['subject_id'].sample(1).values[0]]<jupyter_output><empty_output><jupyter_text>We change all the timestamp data into the same data type<jupyter_code>df['datetime']= pd.to_datetime(df['datetime'], format='%Y-%m-%dT%H:%M:%S.%f%z')
df_label_user['start'] = pd.to_datetime(df_label_user['start'], format='%Y-%m-%d %H:%M:%S %z')
df_label_user['finish'] = pd.to_datetime(df_label_user['finish'], format='%Y-%m-%d %H:%M:%S %z')<jupyter_output><empty_output><jupyter_text>We can check how the data change after pre-processing<jupyter_code>df.head(5)
df_label_user.head(5)<jupyter_output><empty_output><jupyter_text>Segmentation We try to reset the index value<jupyter_code>df_label_user=df_label_user.reset_index()
df_label_user=df_label_user.drop(columns='index')
df_label_user.head(5)<jupyter_output><empty_output><jupyter_text>Segment the data by the timestamp given by label file.Every segment windows are extracted by the start and finish time of the activity in label file.<jupyter_code>seg_list = []
seg_label_list = []

for i in range(len(df_label_user)):
    
    seg = df[(df["datetime"] >=df_label_user['start'][i]) & (df["datetime"] <= df_label_user['finish'][i])]
    seg_label = df_label_user["activity_type_id"][i]
    if (len(seg)!=0):
        seg_list.append(seg)
        seg_label_list.append(seg_label)<jupyter_output><empty_output><jupyter_text>Features Extraction In this tutorial, we extract 4 main features: STD, Average, Max, Min of 3 coordinates.<jupyter_code>def get_features(x_data):
    #Set features list
    features = []
    #Set columns name list
    DFclist=list(x_data.columns)

    #Calculate features (STD, Average, Max, Min) for each data columns X Y Z 
    for k in DFclist:
        # std
        features.append(x_data[k].std(ddof=0))
        # avg
        features.append(np.average(x_data[k]))
        # max
        features.append(np.max(x_data[k]))
        # min
        features.append(np.min(x_data[k]))
    return features
features_list = []
for i in seg_list:
  #Drop 2 columns 'subject_id' and 'datetime', we only use 3 columns x, y, z
  i = i.drop(columns=['subject_id','datetime'])
  features_list.append(get_features(i))<jupyter_output><empty_output><jupyter_text>Training The Random Forest Model is utilized in this tutorial<jupyter_code>from sklearn.ensemble import RandomForestClassifier         
model_ml = RandomForestClassifier(n_estimators=500,n_jobs=-1)<jupyter_output><empty_output><jupyter_text>Divide data into train and test file to evaluate the results<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features_list, seg_label_list, test_size=0.3, random_state=42)<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>model_ml.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Check the results<jupyter_code>from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

y_predict = model_ml.predict(X_test)
print(classification_report(y_test,y_predict))
confusion_matrix(y_test, y_predict)<jupyter_output>precision    recall  f1-score   support

           1       0.00      0.00      0.00         0
           4       0.60      0.33      0.43         9
           7       1.00      0.50      0.67         2
          10       0.42      0.71      0.53         7
          13       0.00      0.00      0.00         1

    accuracy                           0.47        19
   macro avg       0.40      0.31      0.32        19
weighted avg       0.54      0.47      0.47        19
StarcoderdataJupyterStructuredCleanDedup