-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMRS_main.py
360 lines (223 loc) · 7.84 KB
/
MRS_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
# coding: utf-8
# # CollabMP3
# ### Importing Libraries
# In[81]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
# ### Data Preprocessing
# In[82]:
data = pd.read_csv("SpotifyFeatures.csv")
data.head()
# In[83]:
def visualize(data):
corr = data.corr(method="pearson")
plt.figure(figsize=(14,6))
heatmap = sns.heatmap(corr, annot=True,vmin=-1, vmax=1, center=0, cmap="inferno", linewidths=1, linecolor="Black")
heatmap.set_title("Correlation")
sample = data.sample(int(0.001*len(data)))
print("Number of samples taken: ",len(sample))
plt.figure(figsize=(10,6))
sns.regplot(data=sample, y="acousticness", x="energy").set(title="Acousticness vs Energy")
sns.set_style(style="darkgrid")
plt.title("Duration of Songs")
sns.color_palette("rocket", as_cmap = True)
sns.barplot(y="genre", x="duration_ms", data = data)
sns.set_style(style = "darkgrid")
plt.figure(figsize=(10,5))
famous = data.sort_values("popularity", ascending=False)
sns.barplot(y="genre", x="popularity", data = famous).set(title="Top Genres by Popularity")
# In[84]:
visualize(data)
# In[85]:
def plot1(data):
print("Mean value of acousticness:", data['acousticness'].mean())
sns.histplot(x='acousticness', data=data, kde=True)
plt.xlabel('Acoustincess', fontsize=14)
plt.ylabel('Count', fontsize=14)
plt.tight_layout()
def plot2(data):
# mean value and histplot for for energy feature
print("Mean value of energy:", data['energy'].mean())
sns.histplot(x='energy', data=data, kde=True)
plt.xlabel('Energy', fontsize=14)
plt.ylabel('Count', fontsize=14)
plt.tight_layout()
# In[86]:
plot1(data)
# In[87]:
plot2(data)
# In[88]:
def plot3(data):
sample = data.sample(int(0.001*len(data)))
print("Number of samples taken: ",len(sample))
plt.figure(figsize=(10,6))
sns.regplot(data=sample, y="acousticness", x="energy").set(title="Acousticness vs Energy")
# In[89]:
plot3(data)
# ### Cluster creation
# In[90]:
def plot_clus(X, Y, kmeans):
plt.figure(figsize=(10,6))
plt.scatter(X[Y==0,0], X[Y==0,1], s=5, c='red', label="Cluster 1")
plt.scatter(X[Y==1,0], X[Y==1,1], s=5, c='green', label="Cluster 2")
plt.scatter(X[Y==2,0], X[Y==2,1], s=5, c='blue', label="Cluster 3")
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=20, c="black", label='Centroids')
plt.title("Clusters")
def cluster(data):
X = data.iloc[:, [5,8]].values
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters=i, init='k-means++', random_state=30)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
sns.set()
plt.plot(range(1,11), wcss)
plt.title("Elbow graph")
plt.show()
kmeans = KMeans(n_clusters=3, init='k-means++', random_state=0)
Y = kmeans.fit_predict(X)
plot_clus(X, Y, kmeans)
# In[91]:
cluster(data)
# Most Popular Songs
# In[92]:
def most_popular(data):
df2 = data.copy()
df2.drop_duplicates(subset = "track_name", inplace = True) #dropping duplicate songs
df2.head()
rslt_df = df2.sort_values(by = 'popularity', ascending = False)
rslt_df = rslt_df[['genre', 'artist_name', 'track_name']]
print("Top 10 most popular songs:\n")
for i in range(10):
row_list = rslt_df.loc[i, :].values.flatten().tolist()
print(row_list[1], "-", row_list[2])
# In[93]:
most_popular(data)
# ### Code for single track that is being playing in real time by the user
# In[94]:
import spotipy
from spotipy.oauth2 import SpotifyOAuth
client_ID = "7f43ef2510664eb1bc313596b46800ca"
client_SECRET = "57cfb3ffb5d84ef7b922cc8c4a9181aa"
redirect_url= "http://localhost:9000"
sp= spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_ID,
client_secret=client_SECRET,
redirect_uri=redirect_url,
scope="user-read-playback-state",
open_browser=True))
# client_ID='279311a4db264aeb86a9848fa6cb5056'
# client_SECRET= 'f3cbc5b8cf5e4252a5a7add41e213641'
# In[95]:
def read_current_play(sp):
try:
results= sp.current_playback()
except:
sp= spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_ID,
client_secret=client_SECRET,
redirect_uri=redirect_url,
scope="user-read-playback-state",
open_browser=True)).current_playback()
results= sp.current_playback()
return currently_playing(results)
# In[96]:
def get_features(play):
df= pd.DataFrame(sp.audio_features(play.id))
return df.drop(columns=['type', 'uri', 'track_href', 'analysis_url'])
# In[97]:
class currently_playing:
def __init__(self, results):
self.results= results
self.id= results['item']['id']
self.time= results['item']['duration_ms']
self.popularity= results['item']['popularity']
def info(self):
name= []
print(f"Track name: {self.results['item']['album']['name']}")
for artist in self.results["item"]["artists"]:
name.append(artist['name'])
print("Artists: ", end="")
print(", ".join(name))
print(f"Popularity: {self.popularity}")
print(f"Release Date: {self.results['item']['album']['release_date']}")
def artists(self):
artist_ids= []
for artist in self.results["item"]["artists"]:
artist_ids.append(artist['id'])
return artist_ids
def track_name(self):
return self.results['item']['album']['name']
# In[98]:
playing= read_current_play(sp)
# In[99]:
play_df = get_features(playing)
play_df.head()
# ### Plotting current song on the plot
# In[100]:
A = []
def song_features(data):
B = []
data = data.values.tolist()
# print(data[0][1])
# print(data[0][6])
B.append(data[0][1])
B.append(data[0][6])
A.append(B)
# print(A)
# In[101]:
song_features(play_df)
# In[102]:
from scipy.spatial import distance
def AddPoint(plot, x, y, color):
plot.scatter(x, y, c=color)
plot.figure(figsize=(10,6))
plot.show()
def cal_cluster(A):
dist=[]
X = data.iloc[:, [5,8]].values
kmeans = KMeans(n_clusters=3, init='k-means++', random_state=0)
Y = kmeans.fit_predict(X)
for i in range(len(kmeans.cluster_centers_)):
dist.append(distance.euclidean(kmeans.cluster_centers_[i], A))
# print(dist)
num = pd.Series(dist).idxmin()
if(num==0):
print("Song is in RED cluster")
elif(num==1):
print("Song is in GREEN cluster")
else:
print("Song is in BLUE cluster")
return num
# In[103]:
#determining which cluster the given song is in
num = cal_cluster(A)
X = data.iloc[:, [5,8]].values
kmeans = KMeans(n_clusters=3, init='k-means++', random_state=0)
Y = kmeans.fit_predict(X)
#plotting the song in the scatter plot
plot_clus(X, Y, kmeans)
AddPoint(plt, A[0][0], A[0][1], 'yellow')
# ### Euclidean Distance
# In[104]:
import math
def recommend(flag):
eDist=[]
for i in range(len(X[Y==flag])):
eDistance = math.dist(A[0], X[Y==flag][i])
eDist.append(eDistance)
eDist2 = []
eDist2.extend(eDist)
eDist2.sort()
eDist2
for i in range(5):
num = eDist.index(eDist2[i])
arr = data.loc[(data['energy'] == X[Y==flag][num][1]) & (data['acousticness'] == X[Y==flag][num][0])]
arr = arr.values.flatten().tolist()
print(arr[2] , 'by', arr[1])
# In[105]:
recommend(num)