seyia92coding commited on
Commit
614e80b
1 Parent(s): e546a30

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """HS_Recomm_Metacritic_Gradio.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1cIAUS8Z2U2DXPEVmRdou9mqI0vwNT0_0
8
+ """
9
+
10
+ import pandas as pd
11
+ import numpy as np
12
+ import scipy as sp
13
+ from scipy import sparse
14
+ from sklearn.metrics.pairwise import cosine_similarity
15
+ !pip install fuzzywuzzy
16
+ from fuzzywuzzy import fuzz
17
+
18
+ meta_df = pd.read_csv("/content/Metacritic_Scores_File.csv", error_bad_lines=False, encoding='utf-8')
19
+ meta_df = meta_df[['game', 'reviewer_ID', 'score']]
20
+
21
+ df_game_names = pd.read_csv("/content/Game_Titles_IDs.csv", error_bad_lines=False, encoding='utf-8')
22
+
23
+ #We will create a pivot table of users as rows and games as columns.
24
+ #The pivot table will help us make the calcuations of similarity between the reviewers.
25
+ pivot = meta_df.pivot_table(index=['reviewer_ID'], columns=['game'], values='score')
26
+
27
+ #Applying lambda function to multiple rows using Dataframe.apply()
28
+ #(x-np.mean(x))/(np.max(x)-np.min(x)) = Formula
29
+ pivot_n = pivot.apply(lambda x: (x-np.mean(x))/(np.max(x)-np.min(x)), axis=1)
30
+
31
+ # step 2 - Fill NaNs with Zeros
32
+ pivot_n.fillna(0, inplace=True)
33
+
34
+ # step 3 - Transpose the pivot table
35
+ pivot_n = pivot_n.T
36
+
37
+ # step 4 - Locate the columns that are not zero (unrated)
38
+ pivot_n = pivot_n.loc[:, (pivot_n != 0).any(axis=0)]
39
+
40
+ # step 5 - Create a sparse matrix based on our pivot table
41
+ piv_sparse = sp.sparse.csr_matrix(pivot_n.values)
42
+
43
+ #Compute cosine similarity between samples in X and Y.
44
+ game_similarity = cosine_similarity(piv_sparse)
45
+
46
+ #Turn our similarity kernel matrix into a dataframe
47
+ game_sim_df = pd.DataFrame(game_similarity, index = pivot_n.index, columns = pivot_n.index)
48
+
49
+ # create a function to find the closest title
50
+ def matching_score(a,b):
51
+ #fuzz.ratio(a,b) calculates the Levenshtein Distance between a and b, and returns the score for the distance
52
+ return fuzz.ratio(a,b)
53
+ # exactly the same, the score becomes 100
54
+
55
+ # a function to convert index to title
56
+ def get_title_from_index(index):
57
+ return df_game_names.iloc[index]['game']
58
+
59
+ # a function to return the most similar title to the words a user type
60
+ def find_closest_title(title):
61
+ #matching_score(a,b) > a is the current row, b is the title we're trying to match
62
+ leven_scores = list(enumerate(df_game_names['game'].apply(matching_score, b=title)))
63
+ sorted_leven_scores = sorted(leven_scores, key=lambda x: x[1], reverse=True)
64
+ closest_title = get_title_from_index(sorted_leven_scores[0][0])
65
+ distance_score = sorted_leven_scores[0][1]
66
+ return closest_title, distance_score
67
+ # Bejeweled Twist, 100
68
+
69
+ def game_recommendation(game):
70
+ #Insert closest title here
71
+ game, distance_score = find_closest_title(game)
72
+ #Counter for Ranking
73
+ number = 1
74
+ print('Recommended because you played {}:\n'.format(game))
75
+
76
+ for n in game_sim_df.sort_values(by = game, ascending = False).index[1:6]:
77
+ print("#" + str(number) + ": " + n + ", " + str(round(game_sim_df[game][n]*100,2)) + "% " + "match")
78
+ number +=1
79
+
80
+ !pip install gradio
81
+
82
+ import gradio as gr
83
+
84
+ recommender_interface = gr.Interface(game_recommendation, ["text"],
85
+ ["text"], title="Top 5 Game Recommendations", description="This is a Recommendation Engine based on how Metacritic professional reviewers have scored games up to 2019 (apologies for the out of date data). Simply input a game you have enjoyed playing and it should return 5 games that have been rated similarily")
86
+
87
+ recommender_interface.launch(debug=True)