forked from Ranit-Bandyopadhyay/Music-recommendation-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_recommendation.py
108 lines (89 loc) · 3.38 KB
/
music_recommendation.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
import songs as s
import platform
if(platform.system()=='Windows'):
f = open('C:\\Users\\user\\Desktop\\important\\machine learning files\\csv\\music\\songDb.tsv', 'r')
else:
f = open('/home/arnamaity/Hackathons/Music-recommendation-system/spotify-music-genre-list/songDb.tsv','r',encoding="ISO-8859-1")
y = f.read().splitlines()
count = []
for i in range(len(y)):
if (i % 2 == 0):
count.append(y[i].split('\t'))
d = []
e = 0
f = []
for i in range(len(count)):
d.append(count[i][-1])
i = 1
j = i + 1
'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
# LABEL ENCODING
try:
while (i != len(d)):
while (d[i] == d[j]):
e += 1
i += 1
j += 1
f.append(e)
i += 1
j += 1
e = 0
except IndexError:
pass
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
g = []
t = 0
for i, j in zip(f, range(len(f))):
t = i
while (t != 0):
g.append(j)
t -= 1
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------'
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
# MODEL
p = []
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=19,max_depth=2)
X_train = [count[i][1:11] for i in range(1, len(count))][:92106]
y_train = g[:92106]
X_test = s.function()
model.fit(X_train, y_train)
#print(model.score(X_train,y_train))
p.append(model.predict(X_test))
print(p)
p[0].sort()
#print('GETTING YOUR RECOMMENDATION....')
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
def fa(x):
for i in range(len(g)):
if (g[i] == x):
return g[i]
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
# DICTIONARY
dictionary = dict(zip(g, d))
#print(dictionary)
#print(dictionary[fa(p[0])])
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
#RETRIEVING THE NAMES BASED ON GENRE AND DURATION IN MS
i=0
seu=[]
try:
for j in f:
if(dictionary[fa(p[0])] == count[i][-1] ):
while(i!=j+1):
v=float(count[i][-3])
seu.append(v)
i+=1
break
else:
i+=j
except IndexError:
pass
seu.sort()
try:
for i in range(1,len(count)):
if(str(seu[-i])==count[i][-3]):
print(count[i][0])
except IndexError:
pass
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'