2017-04-22 88 views
2

該代碼獲取係數並使用它來修改以下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 

我看過類似這樣的帖子,但沒有一個迎合這個特殊情況。請幫忙!謝謝。

+0

如果'如果delta> 0.0001:'是'True','new_df'永遠不會存在,所以你不能'返回'它。 – roganjosh

+0

對不起,錯誤是'UnboundLocalError:在賦值前引用的局部變量'new_df' –

+0

而我在函數中傳遞的第一個三角形是0.01大於0.001,但在整個程序中,三角洲不斷下降。 –

回答

0

試試吧。

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 
       return scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date) 

      elif add_mse < sub_mse: 
       coefficient = coefficient - delta 
       return scale_for_all_companies(coefficient, delta, company_name, company_code, start_date, end_date) 

     else: 
      delta = delta/2 
      return 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 

而且最好是做兩兩件事:其一,不知何故new_df初始化graph_df.iterrows()之前,以確保如果graph_df.iterrows()是空的,你就不會再崩潰。其次,檢查if abs(add_mse-sub_mse) > 0.05是否可以是False,所以遞歸不會永遠運行。

一件事是,

new_df = graph_df 
graph_df.set_value(index, twitter_sentiment, row[twitter_sentiment] * coefficient)` 

new_df後改變graph_df,然後返回new_df,所以你要graph_df變化是沒有用的。檢查這一個。也許你想先把graph_df.set_value然後new_df = graph_df

希望,這將有所幫助。

+1

謝謝,@Sklert。把我所說的'scale_for_all_companies'放回去的地方確實有幫助,現在它返回我正在尋找的數據幀。另外,關於'new_df = graph_df graph_df.set_value(index,twitter_sentiment,row [twitter_sentiment] *係數)'' '我很好的觀察,我完全錯過了它。謝謝你的幫助:) –

+0

@PankajSnehi,不客氣,goodluck =) – Sklert

相關問題