简体   繁体   English

在 Google App Engine 中运行游戏推荐引擎时遇到问题

[英]Having trouble running Game Recommendation Engine in Google App Engine

So a couple buddies and I are building a game recommendation engine for our final project.所以我和几个朋友正在为我们的最终项目构建一个游戏推荐引擎。 We got the engine working but decided to host it using Google App Engine.我们让引擎工作,但决定使用 Google App Engine 来托管它。 We have the project up and running but whenever we try to run the code, get "IndexError: list index out of range"我们已经启动并运行了项目,但是每当我们尝试运行代码时,都会得到“IndexError:list index out of range”

Right now we are running a version of the code that already is set to recommend 10 games for counter strike (appid 10 on steam) just to see if it works.现在我们正在运行一个已经设置为推荐 10 款反击游戏的代码版本(steam 上的 appid 10)只是为了看看它是否有效。 We have a version that asks for user input that we will try later.我们有一个要求用户输入的版本,我们稍后会尝试。

I can see in the console that it is recommending games, but it is having issues, as mentioned above.我可以在控制台中看到它正在推荐游戏,但它有问题,如上所述。 On the site, it also displays the same error and trace back.在该站点上,它还显示相同的错误和回溯。

Console Log控制台日志

I also have the code posted below.我也有下面发布的代码。

Any help would be gratefully appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

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 app.yaml

runtime: python39

requirements.txt要求.txt

Flask==1.1.2 Pandas==1.2.4

I think the issue is from the line ds.loc[ds['appid'] == appid]['name'].tolist()[0].split(' - ')[0] .我认为问题出在ds.loc[ds['appid'] == appid]['name'].tolist()[0].split(' - ')[0]

You are doing a comparison ```==``` and not assigning a value ie you are comparing the value of ```ds['appid']``` and ```appid``` which means you are getting a boolean result (true or false).你正在做一个比较``==```而不是分配一个值,即你正在比较```ds['appid']```和``appid```的值,这意味着你得到boolean 结果(真或假)。 This means your code is essentially ```ds.loc[True]['name'].tolist()[0].split(' - ')[0]```这意味着您的代码本质上是```ds.loc[True]['name'].tolist()[0].split(' - ')[0]```


I'm deleting my answer because I found out from this link that panda dataframes can be based on boolean values ie ```ds.loc[True] is valid.我正在删除我的答案,因为我从这个链接中发现熊猫数据帧可以基于 boolean 值,即 ```ds.loc[True] 是有效的。 The same link also gives a reason why one might get indexing error but you'll have to figure it out from the data itself相同的链接还给出了一个可能会出现索引错误的原因,但您必须从数据本身中找出原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM