Osele1/sonic-clusters
0
1import pandas as pd2import numpy as np3 4print("Loading Kaggle Spotify Tracks Dataset...")5 6# Load the Kaggle dataset7try:8 df_kaggle = pd.read_csv('backend/data/spotify-tracks-dataset.csv')9 print(f"Loaded {len(df_kaggle):,} tracks!")10except Exception as e:11 print(f"Error loading dataset: {e}")12 exit(1)13 14# Let's drop duplicates based on track_id15df_kaggle = df_kaggle.drop_duplicates(subset=['track_id'])16 17# The dataset is huge (114k rows). Let's sample 2,000 random tracks for our ML models 18# (You can change this number or remove the sample() call to use the whole thing)19sample_size = min(2000, len(df_kaggle))20print(f"Randomly selecting {sample_size:,} tracks for the new dataset...")21df_sample = df_kaggle.sample(n=sample_size, random_state=42).copy()22 23print("Formatting columns to match the Million Song Dataset...")24 25# Map Kaggle columns to MSD columns26df_mapped = pd.DataFrame()27 28# Identifiers29df_mapped['file_path'] = 'spotify://' + df_sample['track_id']30df_mapped['song_id'] = df_sample['track_id']31df_mapped['track_id'] = df_sample['track_id']32 33# Metadata34# The artists column sometimes has multiple artists separated by ';'35df_mapped['artist_name'] = df_sample['artists'].astype(str).apply(lambda x: x.split(';')[0])36df_mapped['title'] = df_sample['track_name']37df_mapped['release'] = df_sample['album_name']38df_mapped['genre'] = df_sample['track_genre']39df_mapped['year'] = 2023 # This specific dataset was collected around 2022-202340 41# Popularity (Kaggle: 0-100, MSD: 0.0-1.0)42df_mapped['song_hotttnesss'] = df_sample['popularity'] / 100.043df_mapped['artist_hotttnesss'] = 0.5 # Default fallback44df_mapped['artist_familiarity'] = 0.5 # Default fallback45df_mapped['is_popular'] = df_sample['popularity'] >= 7046 47# Audio Features48df_mapped['duration'] = df_sample['duration_ms'] / 1000.049df_mapped['tempo'] = df_sample['tempo']50df_mapped['loudness'] = df_sample['loudness']51df_mapped['key'] = df_sample['key']52df_mapped['mode'] = df_sample['mode']53df_mapped['time_signature'] = df_sample['time_signature']54df_mapped['danceability'] = df_sample['danceability']55df_mapped['energy'] = df_sample['energy']56 57# Confidence Metrics (Echo Nest specific, missing from modern Spotify API)58df_mapped['key_confidence'] = 1.059df_mapped['mode_confidence'] = 1.060df_mapped['time_signature_confidence'] = 1.061 62# Ensure order matches MSD exactly63expected_cols = [64 'file_path', 'song_id', 'track_id', 'artist_name', 'title', 'release', 65 'genre', 'year', 'artist_hotttnesss', 'song_hotttnesss', 'artist_familiarity', 66 'duration', 'tempo', 'loudness', 'key', 'key_confidence', 'mode', 67 'mode_confidence', 'time_signature', 'time_signature_confidence', 68 'danceability', 'energy', 'is_popular'69]70 71df_final = df_mapped[expected_cols]72 73# Save to CSV74output_file = 'new_spotify_songs.csv'75df_final.to_csv(output_file, index=False)76 77print(f"\nSuccess! Extracted and formatted {len(df_final):,} tracks into '{output_file}'")78print("You can now open 'new_spotify_songs.csv', copy the rows, and paste them into your Phase 3 dataset!")79 