2017-06-23 32 views
-1

我正在開發一個推薦引擎,以將商品推薦給本地零售商店連鎖店,並使用我在開發電影時學到的代碼使用電影鏡頭數據集的推薦系統和用於推薦電影的代碼在這裏似乎不起作用。本地變量'index_three'在使用Python開發推薦引擎時出現賦值錯誤之前引用了

一個函數來獲取項目之間的相關性在三級

def get_movie_similarity(level3Id): 
    index_three = list(index_three).index(level3Id) 
    return corr_matrixthree[index_three] 

功能通過安排類似的人的itmes得到類似由用戶最爲購買的itmes項目用戶按升序購買基於它們的皮爾森coreation得分

def get_movie_recommendations(merged): 
    movie_similarities = np.zeros(corr_matrixthree.shape[0]) 
    for level3Id in merged: 
     movie_similarities = movie_similarities + get_movie_similarity(level3Id) 
    similarities_df = pd.DataFrame({'level3Id': index_three,'sum_similarity': movie_similarities}) 
    similarities_df = similarities_df[~(similarities_df.level3Id.isin(merged))] 
    similarities_df = similarities_df.sort_values(by=['sum_similarity'], ascending=False) 
    return similarities_df` 

相似度矩陣I GE用戶和他們購買的物品之間是無差別的,價值就是你在每件物品上花費的金額。

sample_user = 42140122376 
merged[merged.cust_id==sample_user].sort_values(by=['amount_extended'], ascending=False) 


sample_user_movies = merged[merged.cust_id==sample_user].level3Id.tolist() 
recommendations = get_movie_recommendations(sample_user_movies) 

#We get the top 20 recommended movies 
recommendations.level3Id.head(20) 

,我得到的錯誤是:

local variable 'index_three' referenced before assignment 

Index_three是在數據集中 而所有項目的索引,corr_matrix三是相似的itmes之間的矩陣使用皮爾森的分數生成。 合併是我的數據集

你能幫我嗎?

我可以分享我在jupyter筆記本上的代碼!

+0

什麼'index_three'應該是??這是一個清單嗎?你能解釋一下嗎? – zaidfazil

回答

0

爲此,您需要了解變量作用域的工作原理。看看這個!

def my_func(): 
    index3 =5000 
    print(index3) 

index3=10; 
print(index3) 
my_func() 

輸出:

10 
5000 

注意:雖然有兩個index3你可能會認爲它們是相同的。但它們是不是

index3內的my_func是一個局部變量。而你的程序中的一個(不在函數中的那個)index3是不同的!所以會發生什麼在上面的代碼是第一print(index3)打印INDEX3在我的代碼(不是我的程序中任何functions..just)然後my_func()被調用和print(index3)my_func()打印的局部變量index3

乘坐看這個!

def my_func(): 
    print(index3) 

index3=10; 
print(index3) 
my_func() 

輸出:

10 
10 

現在看到這兩個倍index3這是相同的10這意味着它打印全局變量兩次。

現在到了您的問題!:

def my_func(): 
    index3 =index3+1 

index3=10; 
print(index3) 
my_func() 

輸出:

10 
Traceback (most recent call last): 
    File "/home/mr/func.py", line 6, in <module> 
    my_func() 
    File "/home/mr/func.py", line 2, in my_func 
    index3 =index3+1 
UnboundLocalError: local variable 'index3' referenced before assignment 

爲什麼?

因此index3 =index3+1因此,當它看到一個index3=它創建一個局部變量。所以index3=0表示將0賦給局部變量。

index3 =index3+1會混淆它!它認爲

等你要我分配本地變量 index3爲本地變量 index3 + 1?但你還沒有宣佈它!

def my_func(): 
    global index3 
    index3 =index3+1 
    print(index3) 

index3=10 
print(index3) 
my_func() 
print(index3) 

輸出:

10 
11 
11 

現在只需要在函數內部全球價值和它的變化。所以index3被這個函數改變了。

注意:使用全局變量是錯誤的編碼習慣。

def getIndex3(): 
    return index3 

def my_func(): 
    index3 = getIndex3() 
    index3 =index3+1 
    print(index3) 

index3=10 
print(index3) 
my_func() 
print(index3) 

現在輸出:

10 
11 
10 

你得到的區別吧?這就是爲什麼你的程序顯示錯誤。這正是這意味着什麼local variable 'index_three' referenced before assignment

0

在您定義的每個函數中,都使用了index_three變量。

在功能get_movie_similarity,您使用的是像 -

index_three = list(index_three).index(level3Id) 

爲了使上述表態的工作,index_tree應該有它一定的價值。 所以至少通過index_three來運作或使其成爲全球性的,如果可以的話。什麼我上面解釋

例子:

def get_str(): 
"""Give me new string with appending given string with word new""" 
    val = val + "_new" 
    return val 

print get_str() 

當我執行上面的程序,我會得到如下錯誤:

C:\Users\dinesh\Desktop>python multi.py 
Traceback (most recent call last): 
    File "multi.py", line 358, in <module> 
    get_str() 
    File "multi.py", line 355, in get_str 
    val = val + "_new" 
UnboundLocalError: local variable 'val' referenced before assignment 

同你得到。我解決了如下上述錯誤:

def get_str(val): 
    val = val + "_new" 
    return val 

print get_str("Dinesh") 

C:\Users\dinesh\Desktop>python multi.py 
Dinesh_new 

注:使全局變量不推薦。

相關問題