# ---
# 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
# ---

# !pip install plotly
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
import matplotlib.pyplot as plt
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KDTree



df_april_19 = pd.read_csv('../data/SpotifyAudioFeaturesApril2019.csv')
df_nov_18 = pd.read_csv('../data/SpotifyAudioFeaturesNov2018.csv')

df = pd.concat([df_april_19, df_nov_18], ignore_index=True)
print(df.shape)
assert df.shape[0] == (df_april_19.shape[0] + df_nov_18.shape[0])

df = df.drop_duplicates(subset = 'track_id', keep='first')
print(df.shape)

df.head(20)

# ## Running the model
#

# +
number_of_songs = 25
    
df_numerics =  df.drop(columns=['track_id', 'track_name', 'artist_name'])
    
df_scaled = StandardScaler().fit_transform(df_numerics)
df_modeled = KDTree(df_scaled)
       
dist, ind = df_modeled.query(df_scaled, k=(number_of_songs+1))
    
dist_df = pd.DataFrame(dist)
    
scores = (1 - ((dist - dist.min()) / (dist.max() - dist.min()))) * 100
    
columns = ['Searched_Song']
for i in range(number_of_songs):
    columns.append(f'Nearest_Song{i}')

dist_score = pd.DataFrame(scores.tolist(), columns = columns)
    
ind[:(number_of_songs+1)]
    
song_ids = np.array(df.track_id)
    
def find_similars(song_ids, ind):
    similars = []
    for row in ind:
        ids = [song_ids[i] for i in row]
        similars.append(ids)

    return similars 
    
nearest_neighbors = find_similars(song_ids, ind)
    
nearest_neighbors_df = pd.DataFrame(nearest_neighbors, columns=columns)

nearest_neighbors_df.head()
    
# -

pd.set_option('display.max_columns', None)
nearest_neighbors_df.iloc[[69]]



# ## Random Sample 3d Scatter Plot

fig = px.scatter_3d(df.sample(n=10000, random_state = 69), x='valence', y='danceability', z='popularity', color='valence', size='popularity',
                    opacity=.7, hover_name='track_name', color_discrete_sequence=px.colors.sequential.Plasma[-2::-1],
                    template="plotly_dark") #text = 'artist_name')
plot(fig)

# ## Scatter Plot of 25 Nearest Songs

a = df.query("track_name == 'Prop - Original mix'").index.values


a

# +
# TO  display specific Row
id_numbers = nearest_neighbors_df.iloc[[a[0]]].to_string(header=False, index=False, index_names=False).split()
        
to_be_3d = df.query('track_id == @id_numbers')


fig = px.scatter_3d(to_be_3d, x='valence', y='danceability', z='popularity', color='valence', size='popularity',
                    opacity=.7, hover_name='track_name', color_discrete_sequence=px.colors.sequential.Plasma[-2::-1],
                    template="plotly_dark", text = 'artist_name')
plot(fig)

#import plotly.io as pio
#py.plot(fig, filename='name.html')
# -



StarcoderdataJupyterScriptsDedupFiltered