image_output_1.png
import datetime as dt

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

daily_activities = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/dailyActivity_merged.csv')
daily_calories = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/dailyCalories_merged.csv')
daily_intensities = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/dailyIntensities_merged.csv')
daily_steps = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/dailySteps_merged.csv')
hourly_steps = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/hourlySteps_merged.csv')
hourly_intensities = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/hourlyIntensities_merged.csv')
sleep_day = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/sleepDay_merged.csv')
weight_index = pd.read_csv('../input/fitbit/Fitabase Data 4.12.16-5.12.16/weightLogInfo_merged.csv')

len(weight_index.Id.unique())

len(daily_activities.Id.unique())

len(sleep_day.Id.unique())

sleep_day.columns = ['Id', 'Date', 'TotalSleepRecords', 'TotalMinutesAsleep', 'TotalTimeInBed']
daily_activities.columns = ['Id', 'Date', 'TotalSteps', 'TotalDistance', 'TrackerDistance', 'LoggedActivitiesDistance', 'VeryActiveDistance', 'ModeratelyActiveDistance', 'LightActiveDistance', 'SedentaryActiveDistance', 'VeryActiveMinutes', 'FairlyActiveMinutes', 'LightlyActiveMinutes', 'SedentaryMinutes', 'Calories']

sleep_day.Date = pd.to_datetime(sleep_day['Date'], format='%m/%d/%Y %I:%M:%S %p').dt.strftime('%m/%d/%Y')
daily_activities.Date = pd.to_datetime(daily_activities['Date'], format='%m/%d/%Y').dt.strftime('%m/%d/%Y')
weight_index.Date = pd.to_datetime(weight_index['Date'], format='%m/%d/%Y %I:%M:%S %p').dt.strftime('%m/%d/%Y')

#Change date into day of week
index_day=[]
week_day=[]
for i in range(len(daily_activities.Id)):
    day=dt.datetime.strptime(daily_activities.Date[i], "%m/%d/%Y")
    week_day.append(day.strftime("%A"))
    index_day.append(day.weekday())
    i=i+1
daily_activities["DayofWeek"]=week_day
daily_activities["IndexofWeek"]=index_day
gb_daily_activities=daily_activities.groupby(by=['DayofWeek']).mean().reset_index()
gb_daily_activities.sort_values("IndexofWeek")
sns.set_theme(style="darkgrid")
plt.figure(figsize=(10,8))
ax=sns.barplot(x="DayofWeek", y="TotalSteps",data=gb_daily_activities.sort_values("IndexofWeek"), palette="Accent_r")
ax.set_ylabel("Average Steps",fontsize=20)
ax.set_xlabel("Day of Week ",fontsize=20)
ax.set_title("Average Steps During Week ",fontsize=25)
plt.show()

hour=[]
for i in range(len(hourly_steps.Id)):
    day=dt.datetime.strptime(hourly_steps.ActivityHour[i], "%m/%d/%Y  %I:%M:%S %p")
    hour.append(day.strftime("%H"))
    i=i+1
hourly_steps["Hour"]=hour
gb_hourly_steps=hourly_steps.groupby('Hour').mean().reset_index()
sns.set_theme(style="darkgrid")
plt.figure(figsize=(10,8))
hourly_chart=sns.barplot(data=gb_hourly_steps,x="Hour",y="StepTotal")
hourly_chart.set_ylabel("Average Steps",fontsize=20)
hourly_chart.set_xlabel("Hour",fontsize=20)
hourly_chart.set_title("Hourly distribution of user steps",fontsize=25)
plt.show()

sleep_day['TimeTakeToSleep'] = sleep_day['TotalTimeInBed'] - sleep_day['TotalMinutesAsleep']
hourly_intensities.columns = ['Id', 'Date', 'TotalIntensity', 'AverageIntensity']
hourly_intensities.Date = pd.to_datetime(hourly_intensities['Date'], format='%m/%d/%Y %I:%M:%S %p').dt.strftime('%m/%d/%Y')
gb_hourly_intensities = hourly_intensities.groupby(['Date', 'Id']).sum().reset_index()
sleep_and_intensities = pd.merge(sleep_day, gb_hourly_intensities, on=['Date', 'Id'], how='inner')
f, axes = plt.subplots(1, 3, figsize=(12, 6))
k1 = sns.regplot(data=sleep_and_intensities, x='TimeTakeToSleep', y='TotalIntensity', ax=axes[1])
k2 = sns.regplot(data=sleep_and_intensities, x='TotalMinutesAsleep', y='TotalIntensity', ax=axes[0])
k2 = sns.regplot(data=sleep_and_intensities, x='TotalTimeInBed', y='TotalIntensity', ax=axes[2])