2017-04-22 1 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'가 존재하지 않으므로'return' 할 수 없습니다. – roganjosh

+0

죄송합니다. 오류는'UnboundLocalError : 할당 전의 로컬 변수 'new_df'입니다. –

+0

그리고 함수에서 전달한 첫 번째 델타는 0.001보다 큰 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 

그리고 두 개 더 일을하는 것이 좋습니다 : 첫째, graph_df.iterrows()이 비어있는 경우, 다시 충돌하지 않습니다 확인하기 위해 graph_df.iterrows() 전에 어떻게 든 new_df 초기화합니다. 둘째, if abs(add_mse-sub_mse) > 0.05이 False 일 수 있는지 확인하여 재귀가 영구적으로 실행되지 않도록합니다.

한가지 더, 당신은 new_dfgraph_df을 변경 한 후 new_df를 반환

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

것을, 그래서 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

관련 문제