Download recomender_system_cluster_based.py from Golyass/Recommender_system: direct link, hf CLI and curl.
- Browser
- Download file 7.24 kB
-
https://huggingface.co/Golyass/Recommender_system/resolve/main/recomender_system_cluster_based.py
- Command line
-
hf download hf://Golyass/Recommender_system/recomender_system_cluster_based.py
-
curl -L -o recomender_system_cluster_based.py https://huggingface.co/Golyass/Recommender_system/resolve/main/recomender_system_cluster_based.py
7.24 kB
| # -*- 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() | |