File size: 7,242 Bytes
afc1a2b
 
 
 
 
 
 
 
a2f4653
afc1a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2f4653
afc1a2b
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
# -*- coding: utf-8 -*-
"""recomender2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1WTIRumzmdqNSB2Z2dIv3r2LkSN5CdoU5
"""
!pip install transformers
from google.colab import drive
drive.mount('/content/drive')

import os
os.environ['KAGGLE_CONFIG_DIR'] = "/content/drive/MyDrive/kaggle"

# Commented out IPython magic to ensure Python compatibility.
# %ls

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import scale, StandardScaler, normalize
from yellowbrick.cluster import KElbowVisualizer
from sklearn.manifold import TSNE
import seaborn as sns
import random

df1=pd.read_csv('/content/drive/MyDrive/kaggle/ratings.csv')
df2=pd.read_csv('/content/drive/MyDrive/kaggle/movies_metadata.csv')
df3=pd.read_csv('/content/drive/MyDrive/kaggle/ratings_small.csv')

df2

df2.shape

df2.columns

df2.info()

df2.isna().sum()

data=df2[['revenue','runtime','vote_average','vote_count','title']]

data

data=data.fillna(0)

#scale the data

scaler = StandardScaler()
data_scaled = scaler.fit_transform(data.iloc[:, :-1])
data_scaled

inertia = []
silhouette_scores = []
k_values = range(2, 10)
for k in k_values:
    kmeans = KMeans(n_clusters=k, random_state=3,n_init=4)
    kmeans.fit(data_scaled)
    #center=kmeans.cluster_centers
    inertia.append(kmeans.inertia_)
    silhouette_scores.append(silhouette_score(data_scaled, kmeans.labels_))
    print(silhouette_scores)

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(k_values, inertia, marker='o')
plt.xlabel('Number of clusters (k)')
plt.ylabel('Inertia')
plt.title('Inertia vs. Number of Clusters')

plt.subplot(1, 2, 2)
plt.plot(k_values, silhouette_scores, marker='o')
plt.xlabel('Number of clusters (k)')
plt.ylabel('Silhouette Score')
plt.title('Silhouette Score vs. Number of Clusters')

plt.tight_layout()
plt.show()

model = KMeans(n_init=8)
visualiser = KElbowVisualizer(model, k=(2,12))
visualiser.fit(data_scaled)
visualiser.show()

kmeans_mod = KMeans(n_clusters=5, n_init=10)
kmeans_mod.fit(data_scaled)

labels =kmeans_mod.fit_predict(data_scaled)
data['cluster']=labels
data=data[['revenue','runtime','vote_average','vote_count','cluster','title']]
data

data['cluster'].value_counts()

tsne = TSNE(n_components=2, random_state=42)
X_tsne = tsne.fit_transform(data_scaled)

plt.figure(figsize=(8,6))
plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=kmeans.labels_, cmap='viridis')
plt.xlabel('t-SNE Dimension 1')
plt.ylabel('t-SNE Dimension 2')
plt.title('Clustering Results with K-Means (k=5)'.format(5))
plt.colorbar(label='Cluster')
plt.show()

cdata=data[['cluster','title']]
cdata

cluster0=[]
cluster1=[]
cluster2=[]
cluster3=[]
cluster4=[]

filmname=''
for i in cdata.iloc():
     if i[0]==0:
      filmname=''
      filmname=i[1]
      #print(filmname)
      cluster0.append(filmname)
     elif i[0]==1:
        filmname=''
        filmname=i[1]
        cluster1.append(filmname)
     elif i[0]==2:
        filmname=''
        filmname=i[1]
        cluster2.append(filmname)
     if i[0]==3:
        filmname=''
        filmname=i[1]
        cluster3.append(filmname)
     if i[0]==4:
        filmname=''
        filmname=i[1]
        cluster4.append(filmname)
print(len(cluster0))
print(len(cluster1))
print(len(cluster2))
print(len(cluster3))
print(len(cluster4))


print((cluster0))
print((cluster1))
print((cluster2))
print((cluster3))
print((cluster4))

data=data.iloc[:, :-1]
data

from sklearn.decomposition import PCA
pca_mod = PCA(n_components=3)
# fit the PCA model to the (scaled) features
pca_mod.fit(data)

# plot the cumulative variance explained across all the components
# start the explained variance at 0
exp_variance = np.insert(pca_mod.explained_variance_ratio_, 0, 0)

# plot the explained variance
figure = plt.figure(figsize=(8, 5))

plt.plot(np.cumsum(exp_variance))
plt.xticks(range(0,6))
plt.xlabel("Number of principal components (PCs)", size=13)
plt.ylabel("% cumulative variance explained", size=13)
plt.title("Cumulative variance explained", size=15)

plt.show()
np.cumsum(exp_variance)

pca_mod = PCA(n_components=3)

data_pca = pd.DataFrame(data=pca_mod.fit_transform(data_scaled),
                        columns=['PC1', 'PC2', 'PC3'])

# view data frame of PCs
data_pca.head()

# combine data for plotting
data.reset_index(drop=True, inplace=True)
data_pca.reset_index(drop=True, inplace=True)
plot_data = pd.concat([data_pca, data['cluster']], axis=1)

# visualise the clusters by plotting the PCAs in a scatter/pair plot,
# with points colored by cluster membership
sns.pairplot(data=plot_data,
             hue='cluster',
             palette='Set1',
             corner=True)

cluster=[cluster0,cluster1,cluster2,cluster3,cluster4]

def recomender(input1,input2,input3):

  encod_input1=[]
  encod_input2=[]
  encod_input3=[]


  encod_input1.append(cluster0.count(input1))
  encod_input1.append(cluster1.count(input1))
  encod_input1.append(cluster2.count(input1))
  encod_input1.append(cluster3.count(input1))
  encod_input1.append(cluster4.count(input1))



  encod_input2.append(cluster0.count(input2))
  encod_input2.append(cluster1.count(input2))
  encod_input2.append(cluster2.count(input2))
  encod_input2.append(cluster3.count(input2))
  encod_input2.append(cluster4.count(input2))



  encod_input3.append(cluster0.count(input3))
  encod_input3.append(cluster1.count(input3))
  encod_input3.append(cluster2.count(input3))
  encod_input3.append(cluster3.count(input3))
  encod_input3.append(cluster4.count(input3))


  return encod_input1,encod_input2,encod_input3


def recomender2(a):
  s1=0
  s2=0
  s3=0
  for i in range(0,5):
    if a[0][i]!=0:
        s1=i
        #print(f'The first film is in cluster {i+1}')
    if a[1][i]!=0:
       s2=i
       #print(f'The second film is in cluster {i+1}')
    if a[2][i]!=0:
       s3=i
       #print(f'The third film is in cluster {i+1}')
  return s1, s2, s3



def recom(b):
  if b[0]==b[1]==b[2]:
    return random.choice(cluster[b[1]])
  if b[0]==b[1]!=b[2]:
    return random.choice(cluster[b[1]])
  if b[0]!=b[1]==b[2]:
     return random.choice(cluster[b[1]])
  if b[0]==b[2]!=b[1]:
     return random.choice(cluster[b[2]])
  if  b[0]!=b[2]!=b[1]:
     return random.choice(cluster[b[0]])

def original_recom(input1,input2,input3):
  return recom(recomender2(recomender(input1,input2,input3)))

a=recomender('The Ballroom','Scooby-Doo! and the Reluctant Werewolf','Last Summer')
a

b=recomender2(a)
print(b)

recom(b)

cluster4.count("Trial")

!pip install gradio

import numpy as np
import gradio as gr


def original_recom(input1,input2,input3):
  return recom(recomender2(recomender(input1,input2,input3)))
with gr.Blocks() as demo:
    gr.Markdown("Flip text or image files using this demo.")

    input1 = gr.Textbox(placeholder='first film name...')

    input2 = gr.Textbox(placeholder='second film name...')

    input3 = gr.Textbox(placeholder='third film name...')

    text_output=gr.Textbox( )


    text_button = gr.Button("Submit")
    text_button.click(original_recom, inputs=[input1,input2,input3], outputs=text_output)
demo.launch()