简体   繁体   中英

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. We have the project up and running but whenever we try to run the code, get "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. 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

runtime: python39

requirements.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] .

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). This means your code is essentially ```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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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