局部变量';指数三';使用Python开发推荐引擎时发生分配前引用错误
我正在开发一个推荐引擎,向当地的零售连锁店推荐商品,我正在使用我在使用电影镜头数据集开发电影推荐系统时学到的代码,而用于推荐电影的代码现在似乎不起作用 获取三级项目之间相关性的函数局部变量';指数三';使用Python开发推荐引擎时发生分配前引用错误,python,recommendation-engine,Python,Recommendation Engine,我正在开发一个推荐引擎,向当地的零售连锁店推荐商品,我正在使用我在使用电影镜头数据集开发电影推荐系统时学到的代码,而用于推荐电影的代码现在似乎不起作用 获取三级项目之间相关性的函数 def get_movie_similarity(level3Id): index_three = list(index_three).index(level3Id) return corr_matrixthree[index_three] def get_movie_recommend
def get_movie_similarity(level3Id):
index_three = list(index_three).index(level3Id)
return corr_matrixthree[index_three]
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`
根据pearson coreation得分,将与用户购买的ITME相似的ITME按升序排列,以获取与用户购买的ITME最相似的项目的功能
def get_movie_similarity(level3Id):
index_three = list(index_three).index(level3Id)
return corr_matrixthree[index_three]
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`
我生成的相似度矩阵是用户和他们购买的物品之间的相似度矩阵,其值是您在每个物品上花费的金额。
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
索引\u三是数据集中所有项目的索引
然而,corr_矩阵3是使用pearson分数生成的ITME之间的相似性矩阵。
这是我的数据集
你能帮帮我吗?
我可以分享我在jupyter笔记本上的代码 在您定义的每个函数中,您都使用了index_三个变量 在函数
get\u movie\u similarity
中,使用like-
index_three = list(index_three).index(level3Id)
为了使上述语句起作用,索引树
中应该有一些值。
因此,如果可以的话,至少要通过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 my_func():
index3 =5000
print(index3)
index3=10;
print(index3)
my_func()
def my_func():
print(index3)
index3=10;
print(index3)
my_func()
输出:
10
5000
10
10
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
10
11
11
注意:即使有两个index3
您可能会认为它们是相同的。但他们不是
my_func
中的index3
是一个局部变量。而程序中的一个(不在函数中的一个)index3
是不同的!。因此,在上面的代码中发生的是,首先print(index3)
在我的代码中打印index3(不是在任何函数中..只是在我的程序中),然后调用my_func()
,并print(index3)
在my_func()
中打印局部变量index3
看看这个
def my_func():
index3 =5000
print(index3)
index3=10;
print(index3)
my_func()
def my_func():
print(index3)
index3=10;
print(index3)
my_func()
输出:
10
5000
10
10
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
10
11
11
现在查看两次index3
,它是相同的10
,这意味着它将全局变量打印两次
现在你的问题来了!:
def my_func():
index3 =index3+1
index3=10;
print(index3)
my_func()
输出:
10
5000
10
10
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
10
11
11
输出:
10
5000
10
10
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
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
你明白了吗?这就是为什么你的程序会显示这个错误。这正是分配前引用的局部变量“索引三”的意思
索引三应该是什么??这是一份清单吗??你能解释一下吗?