# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

dat = pd.read_csv(r'F:\AppliedDataScience\data\dat.csv')

dat.head()

dat.columns

df = dat[['session_id','track_id', 'skip_1', 'skip_2', 'skip_3', 'not_skipped','context_switch']]

df.head(10)

# +
skip_1 = df['skip_1'].replace({False: 0,True: -1})
skip_1 = pd.DataFrame(skip_1)
skip_2 = df['skip_2'].replace({False: 0,True: -0.5})
skip_2 = pd.DataFrame(skip_2)
skip_3 = df['skip_3'].replace({False: 0,True: 0.2})
skip_3 = pd.DataFrame(skip_3)
not_skipped = df['not_skipped'].replace({False: 0,True: 1})
not_skipped = pd.DataFrame(not_skipped)

### replace columns###

df['skip_1']=skip_1
df['skip_2']=skip_2
df['skip_3']=skip_3
df['not_skipped']=not_skipped
# -

df.head(15)

len(df)

skip_score2 = (df['skip_1']/len(df) + df['skip_2']/len(df)+ df['skip_3']/len(df)+ df['not_skipped']/len(df))
skip_score2 = pd.DataFrame(skip_score2)

skip_score2.head(12)

skip_score = df['skip_1'] + df['skip_2']+ df['skip_3']+ df['not_skipped']
skip_score = pd.DataFrame(skip_score)

skip_score.head(10)

behavior = dat[['hist_user_behavior_reason_start','hist_user_behavior_reason_end','hist_user_behavior_is_shuffle']]

behavior.head(15)

behavior.hist_user_behavior_reason_start.unique()

behavior.hist_user_behavior_reason_end.unique()

# +
start = behavior['hist_user_behavior_reason_start'].replace({'trackdone': 1,'remote': 0,'backbtn':0.9,'clickrow':0.2,
                                                                'appload':0.2,'endplay':0.4,'playbtn':0.5,'trackerror':0,
                                                                'fwdbtn':0.3})
start = pd.DataFrame(start)



end = behavior['hist_user_behavior_reason_end'].replace({'trackdone': 1,'endplay':0.4,'fwdbtn':0.3,'backbtn':0.9,'remote':0,
                                                            'logout':0,'clickrow':0.2})
end = pd.DataFrame(end)


shuffle = behavior['hist_user_behavior_is_shuffle'].replace({True:1,False:0})
shuffle = pd.DataFrame(shuffle)

behavior['hist_user_behavior_reason_start'] = start
behavior['hist_user_behavior_reason_end'] = end
behavior['hist_user_behavior_is_shuffle'] = shuffle
# -

behavior.head(15)

len(behavior)

liking3 = ((behavior['hist_user_behavior_reason_start'] + behavior['hist_user_behavior_reason_end']) + 
          behavior['hist_user_behavior_is_shuffle'])/len(behavior)
liking3 = pd.DataFrame(liking3)
liking3.head(15)

sns.distplot(liking3,bins = 5)
plt.show()

# +
liking1 = ((behavior['hist_user_behavior_reason_start'] * behavior['hist_user_behavior_reason_end']) + 
          behavior['hist_user_behavior_is_shuffle'])
liking1 = pd.DataFrame(liking1)

liking2 = ((behavior['hist_user_behavior_reason_start'] + behavior['hist_user_behavior_reason_end']) + 
          behavior['hist_user_behavior_is_shuffle'])
liking2 = pd.DataFrame(liking2)
# -

liking2.head()

liking1.head(15)

sns.distplot(liking1,bins = 5)
plt.show()

sns.distplot(liking2,bins = 5)
plt.show()

skip_score['liking'] = liking2


sns.distplot(skip_score[0],bins = 5)
plt.show()

from sklearn import preprocessing
liking_array = liking2.values


liking_array.shape

normalized_X = preprocessing.normalize([liking_array])

liking_array.shape

liking_array

zoopy = np.reshape(liking_array, (2, -1))

zoopy

zoopy.shape

zoopy = zoopy.transpose()

zoopy.transpose()

zoopy

normalized_zoopy = preprocessing.normalize(zoopy)

normalized_zoopy

like = normalized_zoopy.reshape(-1,1)

like = pd.DataFrame(like)

like.head(10)

sns.distplot(like,bins = 5)
plt.show()

skip_score.head(10)

like.head()

skip_score['liking'] = liking2

skip_score['normalized_liking'] = like

mul_label = skip_score

mul_label.head()

mul_label = mul_label.rename(columns = {0: "skip_score"})

mul_label.head(10)

dat.columns

track_features = dat[['acousticness', 'beat_strength', 'bounciness', 'danceability','energy', 'flatness']]



frames = [df, behavior,track_features,mul_label]

result = pd.concat(frames,axis = 1)

result.head()

# +
import statsmodels.api as sm
X1 = result[['skip_1','skip_2','skip_3','context_switch','hist_user_behavior_reason_start','hist_user_behavior_reason_end',
            
            'hist_user_behavior_is_shuffle']]

X2 = result[['acousticness', 'beat_strength', 'bounciness', 'danceability','energy', 'flatness']]
y1 = result[['skip_score']]
y2 = result[['liking']]
y3 = result [['normalized_liking']]
# -

# # Predictind based on the track features

# ### skip_score

model_skip_score2 = sm.OLS(y1, X2).fit()
predictions_skip_score_tf = model_skip_score2.predict(X2)
predictions_skip_score_tf = pd.DataFrame(predictions_skip_score_tf)
predictions_skip_score_tf.head()

# ### liking

model_liking2 = sm.OLS(y2, X2).fit()
predictions_liking_tf = model_liking2.predict(X2)
predictions_liking_tf = pd.DataFrame(predictions_liking_tf)
predictions_liking_tf.head()

# ### normalized liking

model_normalized_liking2 = sm.OLS(y3, X2).fit()
predictions_normalized_liking_tf = model_normalized_liking2.predict(X2)
predictions_normalized_liking_tf = pd.DataFrame(predictions_normalized_liking_tf)
predictions_normalized_liking_tf.head()

pout2 = predictions_skip_score_tf
pout2['p_like_tf'] = predictions_liking_tf
pout2['p_normalized_like_tf'] = predictions_normalized_liking_tf

pout2.rename(columns = {0:'p_skip_tf'}, inplace = True) 

pout2.head()

# ## predicting  based on the behavior features

model_skip_score = sm.OLS(y1, X1).fit()

model_skip_score

predictions_skip_score_ubf = model_skip_score.predict(X1)

predictions_skip_score_ubf = pd.DataFrame(predictions_skip_score)

predictions_skip_score_ubf.head()

# +
#### predict the liking based on user behavior features###
# -

model_liking = sm.OLS(y2, X1).fit()
predictions_liking_ubf = model_liking.predict(X1)
predictions_liking_ubf = pd.DataFrame(predictions_liking)
predictions_liking_ubf.head()

# +
#### predict the normalized liking based on user behavior features###
# -

model_normalized_liking = sm.OLS(y3, X1).fit()
predictions_normalized_liking_ubf = model_normalized_liking.predict(X1)
predictions_normalized_liking_ubf = pd.DataFrame(predictions_normalized_liking_ubf)
predictions_normalized_liking_ubf.head()





pout = predictions_skip_score

pout['p_like_ubf'] = predictions_liking_ubf
pout['p_normalized_like_ubf'] = predictions_normalized_liking_ubf

pout.head()

pout.rename(columns = {0:'p_skip_ubf'}, inplace = True) 

pout.head()

result_tf = result
result_tf['p_skip_tf'] = pout2['p_skip_tf']
result_tf['p_like_tf'] = pout2['p_like_tf']
result_tf['p_normalized_tf'] = pout2['p_normalized_like_tf']

result_tf.head()





result['p_skip_ubf'] = pout['p_skip_ubf']

result['p_like_ubf'] = pout['p_like_ubf']

result['p_normalized_like_ubf'] = pout['p_normalized_like_ubf']

result.head()

sns.distplot(result['p_skip'],bins = 5)
plt.show()

sns.distplot(result['p_like'],bins = 5)
plt.show()

# +

sns.distplot(result['p_normalized_like'],bins = 5)
plt.show()
# -



result.to_csv(r'F:\AppliedDataScience\data\multivariate_result_total.csv')
result.to_csv(r'F:\AppliedDataScience\data\multivariate_result_only_tf.csv')



# +
#### EDA OF resultant Data Frame####
# -

result.head()

plt.figure(figsize=(10, 7))
result.boxplot(column=['skip_1','skip_2','skip_3','not_skipped','context_switch'],patch_artist=True,vert=True,showmeans=True)
plt.show()

gh1 = dat.hist_user_behavior_reason_start.unique()



plt.figure(figsize=(10, 7))
result.boxplot(column=['hist_user_behavior_reason_start',
                       'hist_user_behavior_reason_end',
                        'hist_user_behavior_is_shuffle'],
                        patch_artist='True',vert=True,showmeans=True)
plt.show()

# +

plt.figure(figsize=(10, 7))
result.boxplot(column=['skip_score',
                       'liking',
                        'normalized_liking'],
               patch_artist='True',vert=True,showmeans=True)
plt.show()
plt.figure(figsize=(10, 7))
result.boxplot(column=['p_skip_tf',
                       'p_like_tf',
                       'p_normalized_tf'],
               patch_artist='True',vert=True,showmeans=True)
plt.show()


plt.figure(figsize=(10, 7))
result.boxplot(column=['p_skip_ubf',
                       'p_like_ubf',
                       'p_normalized_like_ubf'],
               patch_artist='True',vert=True,showmeans=True)
plt.show()
# -





len(pd.DataFrame(dat['session_id']))

len(pd.DataFrame(dat['session_id'].unique()))



StarcoderdataJupyterScriptsDedupFiltered