Added "activity_change_count" feature

replace/f4512039013fb58bf47a7a337744e8455c2004a0
Echhit Joshi 2019-11-12 14:48:19 -05:00
parent 1d242abbe1
commit 336ce12270
1 changed files with 38 additions and 26 deletions

View File

@ -4,37 +4,49 @@ import scipy.stats as stats
#Read csv into a pandas dataframe #Read csv into a pandas dataframe
data = pd.read_csv(snakemake.input[0]) data = pd.read_csv(snakemake.input[0])
column = ['local_date_time','count','most_common_activity','number_unique_activities','activity_change_count']
finalDataset = pd.DataFrame(columns=column)
finalDataset.set_index('local_date_time',inplace=True)
if data.empty:
finalDataset.to_csv(snakemake.output[0])
#Resampling each of the required features as a pandas series else:
data.local_date_time = pd.to_datetime(data.local_date_time) #Resampling each of the required features as a pandas series
resampledData = data.set_index(data.local_date_time) data.local_date_time = pd.to_datetime(data.local_date_time)
resampledData = data.set_index(data.local_date_time)
resampledData = resampledData[~resampledData.index.duplicated()]
resampledData.rename_axis('time',axis='columns',inplace=True)
resampledData.drop(columns=['local_date_time'],inplace=True)
resampledData = resampledData[~resampledData.index.duplicated()] #Finding count grouped by day
resampledData.rename_axis('time',axis='columns',inplace=True) count = pd.DataFrame()
count = resampledData['activity_type'].resample('D').count()
count = count.rename(columns={"activity_type":"count"})
resampledData.drop(columns=['local_date_time'],inplace=True) #Finding most common activity of the day
mostCommonActivity = pd.DataFrame()
mostCommonActivity = resampledData['activity_type'].resample('D').apply(lambda x:stats.mode(x)[0])
mostCommonActivity = mostCommonActivity.rename(columns={'activity_type':'most_common_activity'})
#Finding count grouped by day #finding different number of activities during a day
count = pd.DataFrame() uniqueActivities = pd.DataFrame()
count = resampledData['activity_type'].resample('D').count() # countChanges = resampledData.to_period('D').groupby(resampledData.index)['activity_type'].value_counts()
count = count.rename(columns={"activity_type":"count"}) uniqueActivities = resampledData['activity_type'].resample('D').nunique()
#Finding most common activity of the day #finding Number of times activity changed
mostCommonActivity = pd.DataFrame() resampledData['activity_type_shift'] = resampledData['activity_type'].shift()
mostCommonActivity = resampledData['activity_type'].resample('D').apply(lambda x:stats.mode(x)[0]) resampledData['activity_type_shift'].fillna(resampledData['activity_type'].head(1),inplace=True)
mostCommonActivity = mostCommonActivity.rename(columns={'activity_type':'most_common_activity'}) #resampledData['different_activity'] = resampledData['activity_type'].apply(lambda x: 0 if resampledData['activity_type'] == resampledData['activity_type_shift'] else 1, axis=1)
resampledData['different_activity']=np.where(resampledData['activity_type']!=resampledData['activity_type_shift'],1,0)
countChanges = pd.DataFrame()
countChanges = resampledData['different_activity'].resample('D').sum()
#finding different number of activities during a day #Concatenating all the processed data only, no other sensor data is added here for simplicity
countChanges = pd.DataFrame() finalDataset = pd.DataFrame()
# countChanges = resampledData.to_period('D').groupby(resampledData.index)['activity_type'].value_counts() finalDataset = pd.concat([count,mostCommonActivity,uniqueActivities,countChanges],axis=1)
countChanges = resampledData['activity_type'].resample('D').nunique() finalDataset.rename(columns={0:"count",1:'most_common_activity','activity_type':'number_unique_activities','different_activity':'activity_change_count'},inplace = True)
#Concatenating all the processed data only, no other sensor data is added here for simplicity #Export final dataframe with extracted features to respective PID
finalDataset = pd.DataFrame() finalDataset.to_csv(snakemake.output[0])
finalDataset = pd.concat([count,mostCommonActivity,countChanges],axis=1)
finalDataset.rename(columns={0:"count",1:'most_common_activity','activity_type':'activity_changes_count'},inplace = True)
#Export final dataframe with extracted features to respective PID
finalDataset.to_csv(snakemake.output[0])