This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Distribution of the data set | |
| #Distribution of the number of times I wake up | |
| fig,axes = plt.subplots(figsize=(12, 4), nrows=1, ncols=2) | |
| plt.sca(axes[0]) | |
| d = df_final.boxplot(column='sleep_awakeningsCount', by='weekday', ax =axes[0]) | |
| d = plt.xticks(list(range(8)),['', 'Lun','Mar','Mir','Jue','Vie','Sab','Dom']) | |
| plt.title('Distribución de veces despertado') | |
| #Inefficiency of sleep | |
| plt.sca(axes[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fig,axes = plt.subplots(figsize=(12, 4), nrows=1, ncols=2) | |
| #Average the times I wake up during the week | |
| ct =0 | |
| plt.sca(axes[ct]) | |
| wak.plot(kind = 'bar',color = colrcode[0], alpha = 0.5) | |
| plt.ylabel('Nº de veces que despierto') | |
| plt.title('Número de veces que despierto') | |
| plt.xticks(list(range(7)),['Lun','Mar','Mir','Jue','Vie','Sab','Dom']) | |
| prepare_plot_area(axes[ct]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Calculate the average number of steps I take per day of the week | |
| weekday_stps = df_final['steps'].groupby(df_final['weekday']).median() | |
| #Calculate the hours I get on average per day of the week | |
| sleep_minutes_asleep_med = df_final['sleep_minutesAsleep'].groupby(df_final['weekday']).median()/60 | |
| #Try to calculate the efficiency of my sleep based on the days of the week | |
| sl_eff = (1-df_final['sleep_minutesAsleep']/df_final['sleep_timeInBed'])*100 | |
| sl = sl_eff.groupby(df_final['weekday']).median() | |
| #How many times do I wake up on average throughout the week? | |
| wak = df_final['sleep_awakeningsCount'].groupby(df_final['weekday']).median() | |
| #How much time on average do I stay in bed depending on the days of the week? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #List with labels for x-axis | |
| list_MA = sleep_evolution.index.tolist() | |
| fig, ax = plt.subplots(figsize=(15,7)) | |
| sleep_evolution.plot(ax=ax, kind = 'line',color = colrcode[2], alpha = 0.5,linewidth = 2, marker = 'o',markersize = 10) | |
| plt.title('Evolución de las horas de sueño/mes') | |
| plt.xticks(list(range(len(list_MA))),list_MA, rotation='vertical') | |
| plt.xlabel('Evolución mensual de más reciente a más antiguo') | |
| plt.ylabel('Horas de sueño al mes') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fig,axes = plt.subplots(figsize = (12,4),nrows = 1, ncols = 2) | |
| plt.sca(axes[0]) | |
| df_final['wake_hour'].plot(kind = 'hist',color = colrcode[0], alpha = 0.8, xlim=[0, 24]) | |
| plt.xlabel('Hora de despertarme por la mañana') | |
| plt.ylabel('Nº de días') | |
| plt.sca(axes[1]) | |
| df_final['sleep_start_hr'].plot(kind = 'hist',color = colrcode[0], alpha = 0.8, xlim= [0, 5]) | |
| xt = plt.xticks([11,0,1,2,3],['11pm','12pm','1am','2am','3am']) | |
| plt.xlabel('Hora de ir a dormir') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Extract new variables about the details of the week and month in the records. | |
| df_final['weekday'] = df_final['date'].map(lambda x: (datetime.datetime.strptime(str(x),"%Y-%m-%d")).weekday() , na_action = 'ignore') | |
| df_final['month'] = df_final['date'].map(lambda x: (datetime.datetime.strptime(str(x),"%Y-%m-%d")).month , na_action = 'ignore') | |
| df_final['year'] = df_final['date'].map(lambda x: (datetime.datetime.strptime(str(x),"%Y-%m-%d")).year , na_action = 'ignore') | |
| df_final['month_year'] = df_final['month'].map(lambda x: str(x)) + '-' +df_final['year'].map(lambda x: str(x)) | |
| #Extract % of bed time awake (related to sleep inefficiency) | |
| df_final['sleep_minutes_awake_per'] = round(df_final['sleep_minutesAwake']/df_final['sleep_timeInBed']*100, 2) | |
| #Calculate the time to wake up for each record/row |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df_final['sedant']=df_final['sedant'].replace(1440,np.nan) | |
| df_final[['active_light', 'active_fair', 'active_very', 'active_cals', 'sleep_awakeningsCount']] = df_final[['active_light', 'active_fair', 'active_very', 'active_cals', 'sleep_awakeningsCount']].replace(0,np.nan) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Clean and format conversion | |
| #Change 'Fecha' variable to 'datetime' | |
| df_activity['date'] = pd.to_datetime(df_activity['date'], format="%d-%m-%Y") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| import pandas as pd | |
| from time import sleep | |
| from datetime import datetime | |
| #Automated process with a function to access each of variables more quickly. | |
| def df_fitbit(activity, base_date, end_date, token): | |
| url = 'https://api.fitbit.com/1/user/-/' + activity + '/date/' + base_date + '/' + end_date + '.json' | |
| response = requests.get(url=url, headers={'Authorization':'Bearer ' + token}).json() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.neighbors import KNeighborsClassifier | |
| # First we use a value of 3 neighbors. | |
| knn = KNeighborsClassifier(n_neighbors = 4) | |
| knn.fit(x_train, y_train) | |
| y_pred_train = knn.predict(x_train) | |
| metrics(y_train, y_pred_train) |