該代碼獲取係數並使用它來修改以下DF。它使用另一個函數scaling()並且工作正常。一旦這個代碼運行,如果我得到它打印的最終結果,new_df它是成功,但未能返回我new_df並引發錯誤,如下所示:UnboundLocalError:分配前引用的局部變量'graph_df'
def scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date):
scaling_dict = scaling(normalised_graph_points_on_xaxis(company_name, company_code, start_date, end_date),
coefficient, delta, company_name)
print(scaling_dict)
sub_mse = scaling_dict.get('Sub_MSE')
add_mse = scaling_dict.get('Add_MSE')
if delta > 0.0001:
if abs(add_mse-sub_mse) > 0.05:
if add_mse > sub_mse:
coefficient = coefficient + delta
scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date)
elif add_mse < sub_mse:
coefficient = coefficient - delta
scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date)
else:
delta = delta/2
scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date)
else:
new_coefficient = coefficient
print('Co-efficient:', new_coefficient)
graph_df = normalised_graph_points_on_xaxis(company_name, company_code, start_date, end_date)
print(graph_df)
for index, row in graph_df.iterrows():
new_df = graph_df
graph_df.set_value(index, twitter_sentiment, row[twitter_sentiment] * coefficient)
return new_df
錯誤
File "/Users/Pankaj/PycharmProjects/untitled/scaling_sentiment_graph.py", line 52, in scale_for_all_companies
scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date)
File "/Users/Pankaj/PycharmProjects/untitled/scaling_sentiment_graph.py", line 67, in scale_for_all_companies
return graph_df
UnboundLocalError: local variable 'graph_df' referenced before assignment
我看過類似這樣的帖子,但沒有一個迎合這個特殊情況。請幫忙!謝謝。
如果'如果delta> 0.0001:'是'True','new_df'永遠不會存在,所以你不能'返回'它。 – roganjosh
對不起,錯誤是'UnboundLocalError:在賦值前引用的局部變量'new_df' –
而我在函數中傳遞的第一個三角形是0.01大於0.001,但在整個程序中,三角洲不斷下降。 –