<jupyter_start><jupyter_text>Import Package & Read in data<jupyter_code>import pandas as pd
df = pd.read_csv('data/SpotifyAudioFeatures2019.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Data Wrangling<jupyter_code>df = df.dropna()
df.isnull().sum().sum()
df_features= df.drop(columns=['artist_name', 'track_id', 'track_name'])
df_features.head()<jupyter_output><empty_output><jupyter_text>Create Prediction Model<jupyter_code>from sklearn.preprocessing import RobustScaler
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
scaler = RobustScaler()
X = scaler.fit_transform(df_features)
model = KDTree(X)<jupyter_output><empty_output><jupyter_text>Sample X For 1 Prediction Only<jupyter_code>import numpy as np
X_Prediction = np.array([X[0]])
X_Prediction
dist, ind = model.query(X_Prediction, k=6)
df_dist = pd.DataFrame(dist)
pd.DataFrame(ind).head()
pd.DataFrame(df_dist).head()<jupyter_output><empty_output><jupyter_text>Convert distance score back to song ID<jupyter_code>prediction_list = []
for row in ind:
    song_id = [df.track_id[i] for i in row]
    prediction_list.append(song_id)
# Turn list in dataframe and export to csv
columns = ['id', '1', '2', '3', '4', '5']
final_prediction = pd.DataFrame(prediction_list, columns=columns)
final_prediction = final_prediction.drop_duplicates(subset=['id'], keep='first')
final_prediction<jupyter_output><empty_output><jupyter_text>Test Query<jupyter_code># Test query
final_prediction[final_prediction.id == "2RM4jf1Xa9zPgMGRDiht8O"].values.tolist()[0][1:]<jupyter_output><empty_output><jupyter_text>Test with JSON from dataframe<jupyter_code>out=df.drop(columns=['artist_name', 'track_name']).head(1).to_json(orient='records')[1:-1].replace('},{', '} {')
print(out)
type(json.loads(out))
s = pd.Series(json.loads(out))
json_df = s.to_frame()
json_df.transpose()
print(df.drop(columns=['artist_name', 'track_name']).shape)
print(json_df.transpose().shape)
df2 = pd.concat([df.drop(columns=['artist_name', 'track_name']),json_df.transpose()], sort=False)
df2 = df2.reset_index(drop=True)
df2.tail()
dynamic_df = df2.drop(columns=['track_id'])
print(dynamic_df.shape)
dynamic_df.tail()<jupyter_output>(130664, 14)<jupyter_text>Make Prediction for test song from original dataframe<jupyter_code>from sklearn.preprocessing import RobustScaler
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
scaler = RobustScaler()
X = scaler.fit_transform(dynamic_df)
model = KDTree(X)
X_Prediction = np.array([X[-1]])
X_Prediction
dist, ind = model.query(X_Prediction, k=6)
prediction_list = []
for row in ind:
    song_id = [df2.track_id[i] for i in row]
    prediction_list.append(song_id)
columns = ['id', '1', '2', '3', '4', '5']
final_prediction = pd.DataFrame(prediction_list, columns=columns)
final_prediction = final_prediction.drop_duplicates(subset=['id'], keep='first')
final_prediction<jupyter_output><empty_output><jupyter_text>Test with custom defined dictionary<jupyter_code>dict1={"track_id":"any_song_id","acousticness":0.00583,"danceability":0.743,"duration_ms":238373,"energy":0.339,"instrumentalness":0.0,"key":1,"liveness":0.0812,"loudness":-7.678,"mode":1,"speechiness":0.409,"tempo":203.927,"time_signature":4,"valence":0.118,"popularity":15}
s = pd.Series(dict1)
json_df = s.to_frame()
json_df.transpose()
print(df.drop(columns=['artist_name', 'track_name']).shape)
print(json_df.transpose().shape)
df2 = pd.concat([df.drop(columns=['artist_name', 'track_name']),json_df.transpose()], sort=False)
df2 = df2.reset_index(drop=True)
df2.tail()
dynamic_df = df2.drop(columns=['track_id'])
print(dynamic_df.shape)
dynamic_df.tail()<jupyter_output>(130664, 14)<jupyter_text>Make Prediction for custom defined song<jupyter_code>from sklearn.preprocessing import RobustScaler
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
scaler = RobustScaler()
X = scaler.fit_transform(dynamic_df)
model = KDTree(X)
X_Prediction = np.array([X[-1]])
X_Prediction
dist, ind = model.query(X_Prediction, k=6)
prediction_list = []
for row in ind:
    song_id = [df2.track_id[i] for i in row]
    prediction_list.append(song_id)
columns = ['id', '1', '2', '3', '4', '5']
final_prediction = pd.DataFrame(prediction_list, columns=columns)
final_prediction = final_prediction.drop_duplicates(subset=['id'], keep='first')
final_prediction
final_prediction.values.tolist()[0][1:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15446789402784014, 'fraction_numerical': 0.026493039964077234, 'mean_word_length': 9.148063781321184, 'pattern_counts': {'":': 15, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '467443', 'license': 'MIT', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1684, 'n_words': 265, 'path': 'dynamic_spotify_prediction.ipynb', 'repo_name': 'Lambda-School-Labs/groovn-music-ds', 'size': '4454'}