【问题标题】:Having trouble running Game Recommendation Engine in Google App Engine在 Google App Engine 中运行游戏推荐引擎时遇到问题
【发布时间】:2021-07-24 00:39:43
【问题描述】:

所以我和几个朋友正在为我们的最终项目构建一个游戏推荐引擎。我们让引擎工作,但决定使用 Google App Engine 来托管它。我们已经启动并运行了项目,但是每当我们尝试运行代码时,都会收到“IndexError: list index out of range”

目前,我们正在运行已设置为推荐 10 款反击游戏的代码版本(steam 上的 appid 10),只是为了看看它是否有效。我们有一个要求用户输入的版本,我们稍后会尝试。

我可以在控制台中看到它正在推荐游戏,但它存在问题,如上所述。在站点上,它也显示相同的错误和回溯。

Console Log

我也有下面的代码。

任何帮助将不胜感激。谢谢。

main.py

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

requirements.txt

Flask==1.1.2 Pandas==1.2.4

【问题讨论】:

  • 您是否尝试过记录ds.loc[ds['appid'] == appid]['name'] 以确保它实际上已被定义? IDK Python 很好,但那是一条看起来很复杂的行。

标签: python google-app-engine flask recommendation-engine google-app-engine-python


【解决方案1】:

我认为问题出在ds.loc[ds['appid'] == appid]['name'].tolist()[0].split(' - ')[0] 行。

你正在做一个比较``==```而不是分配一个值,即你正在比较```ds['appid']```和``appid``的值,这意味着你得到布尔结果(真或假)。这意味着您的代码本质上是```ds.loc[True]['name'].tolist()[0].split(' - ')[0]```


我正在删除我的答案,因为我从这个 link 发现熊猫数据帧可以基于布尔值,即 ``ds.loc[True] 是有效的。同一个链接还给出了一个可能会出现索引错误的原因,但您必须从数据本身中找出原因

【讨论】:

  • 您建议如何修复索引错误?
猜你喜欢
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多