<jupyter_start><jupyter_text>Modeling K-Nearest Neighbors<jupyter_code># Variables to cluster

cluster = ['popularity', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'tempo']

distortion = list()
for k in range(1, 31): #started with range [1, 51] and narrowed down to this range 
    kmeans = KMeans(n_clusters = k)
    kmeans.fit(scaled.loc[:, cluster])
    distortion.append(kmeans.inertia_) # append distortion value to list
knn = NearestNeighbors(n_neighbors=15)
features = ['popularity', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'tempo']
X = scaled[features].values

knn.fit(X)
# test on 1 song
test_song = X[20]

distance, neighbors = knn.kneighbors(np.array([test_song]))
distance, neighbors
scaled.iloc[20]<jupyter_output><empty_output><jupyter_text>New playlist based off user input<jupyter_code>song_list = []
for item in neighbors[0][1:]: # this way excludes itself
    row = scaled.iloc[item]
    song_list.append((row.track_name, row.artist_name))
    
names = ['song', 'artist']

new_playlist = pd.DataFrame(song_list, columns=names)
print(new_playlist.shape)
new_playlist<jupyter_output>(14, 2)<jupyter_text>Pickle the Model<jupyter_code>import joblib
joblib.dump(knn, 'model.pkl')
model = joblib.load('model.pkl')
model.kneighbors(X[11].reshape(1,-1))[1][0][1:]<jupyter_output><empty_output><jupyter_text>Sentiment Analysis (Pre-trained Neural Network)<jupyter_code>!pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# Using a pretrained neural network to encode title to numbers
# Adding numbers to column as sentiments
sentiments =[] 
analyzer = SentimentIntensityAnalyzer()
for sentence in scaled['track_name']:
  vs = analyzer.polarity_scores(sentence)
  sentiments.append(vs['compound'])
scaled['sentiments'] = sentiments
scaled.sort_values(by=['sentiments'],ascending=False).head(5)
scaled.sort_values(by=['sentiments'],ascending=False).tail(5)<jupyter_output><empty_output><jupyter_text>Visualization 1<jupyter_code>import plotly.graph_objects as go

categories = ['popularity', 'danceability', 'energy', 'instrumentalness', 'tempo']

fig = go.Figure()


fig.add_trace(go.Scatterpolar(
      r=[0.75, -0.34, 1.59, -0.39, -0.10],
      theta=categories,
      fill='toself',
      name='New Rules - <NAME>'
))
fig.add_trace(go.Scatterpolar(
      r=[0.63, -0.57, 1.38, 0.02, 0.27],
      theta=categories,
      fill='toself',
      name='Apocalypse Dreams - Tame Impala'
))
fig.add_trace(go.Scatterpolar(
      r=[0.52, -0.11, 1.26, -0.39, 0.17],
      theta=categories,
      fill='toself',
      name='On The Line - <NAME>'
))

fig.update_layout(
  polar=dict(
    radialaxis=dict(
      visible=True,
      range=[-2, 2]
    )),
  showlegend=False
)

fig.show()<jupyter_output><empty_output><jupyter_text>Visualization 2<jupyter_code>import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/Build-Week-Spotify-Song-Suggester-5/Data-Science/master/app/most_popular_spotify_songs.csv')

df.head()
# Compare/contrast genres
groupby_genre = df.groupby(['genre'])
genre_musical_attributes = groupby_genre.mean()

genre_musical_attributes.sort_values(by=['popularity'],ascending=False)
# Popularity By Genre

import plotly.graph_objects as go
import plotly.express as px

fig = go.Figure()
fig.add_trace(go.Bar(
    y= ['Pop', 'Rap', 'Rock', 'Hip-Hop', 'Dance', 'Indie', 'Childrens Music', 'Alternative', 'Folk', 'Raggaeton', 'R&B', 'Movie',
        'Country', 'Raggae', 'Soul', 'Electronic', 'Jazz'],
    
    x= [67, 59, 59, 58, 58, 53, 53, 51, 50, 50, 49, 49, 48, 48, 47, 47, 47],
    name='Genre',
    orientation='h',
    marker=dict(
        color='rgba(2, 181, 8, 0.6)',
        line=dict(color='rgba(105, 105, 105, 1.0)', width=1)      
    ) 
))
fig.update_layout(barmode='stack')
fig.update_layout(
  title_text="Popularity By Genre"),

# Source
annotations = []
annotations.append(dict(xref='paper', yref='paper',
                        x=0, y=-0.15,
                        text='Source: https://www.kaggle.com/yamaerenay/spotify-dataset-19212020-160k-tracks',
                        font=dict(family='Arial', size=13, color='rgb(150,150,150)'),
                        showarrow=False))

fig.update_layout(annotations=annotations)
fig.show()<jupyter_output><empty_output><jupyter_text>Hard Code Input Dictionary<jupyter_code>def model_maker():

  # Variables to cluster
  cluster = ['popularity', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'tempo']

  distortion = list()
  for k in range(1, 31): #started with range [1, 51] and narrowed down to this range 
      kmeans = KMeans(n_clusters = k)
      kmeans.fit(scaled.loc[:, cluster])
      distortion.append(kmeans.inertia_) # append distortion value to list
  
  # Modeling K-Nearest Neighbors
  knn = NearestNeighbors(n_neighbors=15)
  features = ['popularity', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'tempo']
  X = scaled[features].values

  knn.fit(X)



# test on 1 song
test_song = X[user_input]

distance, neighbors = knn.kneighbors(np.array([test_song]))

knn = model_maker()

def predict(user_input):

  # test on 1 song
  test_song = X[user_input]

  distance, neighbors = knn.kneighbors(np.array([test_song]))

  song_list = []
  for item in neighbors[0][1:]: # this way excludes itself
      row = scaled.iloc[item]
      song_list.append((row.track_name, row.artist_name))
    
  names = ['song', 'artist']

  new_playlist = pd.DataFrame(song_list, columns=names)
predict(user_input)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15175375805297064, 'fraction_numerical': 0.030064423765211165, 'mean_word_length': 4.703061224489796, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '352964', 'license': 'MIT', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2008, 'n_words': 395, 'path': 'notebooks/presentation_notebook.ipynb', 'repo_name': 'trackteam-spotify/data-science', 'size': '5598'}