[英]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.