Skip to content

Instantly share code, notes, and snippets.

View JLFDataScience's full-sized avatar

Jose Luis Fernández Nuevo JLFDataScience

  • FGCSIC
View GitHub Profile
@JLFDataScience
JLFDataScience / Distribution_data.py
Last active January 24, 2020 14:05
distribution of the number of times I wake up, inefficiency of sleep, distribution of bedtime and wake-up time
#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])
@JLFDataScience
JLFDataScience / bedtime_analyze.py
Last active January 24, 2020 14:05
Observing bedtime, we can analyze how much time and sleep efficiency per week
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])
@JLFDataScience
JLFDataScience / Exploring_sleep_perweek.py
Last active January 24, 2020 14:05
Explore variations based on days of the week
#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?
@JLFDataScience
JLFDataScience / evolution_sleep_hour.py
Last active January 24, 2020 14:06
Evolution of sleep hours per month
#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')
@JLFDataScience
JLFDataScience / sleep_wake_up_fitbit.py
Last active January 24, 2020 14:06
Visualize time of sleeping and waking up.
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')
@JLFDataScience
JLFDataScience / temporal_feature_fitbit.py
Last active January 24, 2020 14:06
New temporary features
#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
@JLFDataScience
JLFDataScience / Inconsistencies_df_fitbit.py
Last active January 24, 2020 14:07
Homogenize the inconsistencies in df fitbit
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)
@JLFDataScience
JLFDataScience / datetime_type_conversion.py
Last active January 24, 2020 14:08
datetime type conversion
#Clean and format conversion
#Change 'Fecha' variable to 'datetime'
df_activity['date'] = pd.to_datetime(df_activity['date'], format="%d-%m-%Y")
@JLFDataScience
JLFDataScience / Request_api_fitbit.py
Last active January 24, 2020 14:03
Request api fitbit function
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()
@JLFDataScience
JLFDataScience / Knn.py
Last active January 24, 2020 14:07
k-Nearest Neighbor model
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)