[英]Having trouble running Game Recommendation Engine in Google App Engine
所以我和幾個朋友正在為我們的最終項目構建一個游戲推薦引擎。 我們讓引擎工作,但決定使用 Google App Engine 來托管它。 我們已經啟動並運行了項目,但是每當我們嘗試運行代碼時,都會得到“IndexError:list index out of range”
現在我們正在運行一個已經設置為推薦 10 款反擊游戲的代碼版本(steam 上的 appid 10)只是為了看看它是否有效。 我們有一個要求用戶輸入的版本,我們稍后會嘗試。
我可以在控制台中看到它正在推薦游戲,但它有問題,如上所述。 在該站點上,它還顯示相同的錯誤和回溯。
我也有下面發布的代碼。
任何幫助將不勝感激。 謝謝你。
主文件
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
app = Flask(__name__)
#@app.route("/")
#def index():
#return "Congratulations, it's a web app!"
@app.route("/")
def filter():
url = 'https://drive.google.com/file/d/1_skLvOKWQtq4c3x2aZtz1HlJeIxtQeon/view'
path = 'https://drive.google.com/uc?export=download&id=' + url.split('/')[-2]
ds = pd.read_csv(path)
tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 1), min_df=0, stop_words='english')
tfidf_matrix = tf.fit_transform(ds['genres'])
cosine_similarities = linear_kernel(tfidf_matrix, tfidf_matrix)
results = {}
for idx, row in ds.iterrows():
similar_indices = cosine_similarities[idx].argsort()[:-100:-1]
similar_items = [(cosine_similarities[idx][i], ds['appid'][i]) for i in similar_indices]
results[row['appid']] = similar_items[1:]
print('done!')
def item(appid):
return ds.loc[ds['appid'] == appid]['name'].tolist()[0].split(' - ')[0]
# Just reads the results out of the dictionary.
def recommend(item_id, num):
print("Recommending " + str(num) + " products similar to " + item(item_id) + "...")
print("-------")
recs = results[item_id][:num]
for rec in recs:
print("Recommended: " + item(rec[1]))
recommend(item_id=10, num=10)
return recommend
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)
app.yaml
runtime: python39
要求.txt
Flask==1.1.2 Pandas==1.2.4
我認為問題出在ds.loc[ds['appid'] == appid]['name'].tolist()[0].split(' - ')[0]
。
我正在刪除我的答案,因為我從這個鏈接中發現熊貓數據幀可以基於 boolean 值,即 ```ds.loc[True] 是有效的。 相同的鏈接還給出了一個可能會出現索引錯誤的原因,但您必須從數據本身中找出原因
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.