2016-12-30 1 views
0

두 개의 데이터 프레임 'matches_df'및 'ratings_df'가 있습니다. 경기 데이터 프레임은 플레이어, 날짜 및 2 인 게임 경기의 승자를 저장합니다. 등급 데이터 프레임은 각 플레이어의 현재 등급을 임의의 값으로 저장합니다. 나중에 다시 설정하기 전에이 프레임을 업데이트하고 싶습니다.파이썬은 함수 호출 사이에서 변경 가능한 전역 데이터 프레임을 재설정합니다.

matches_df 

date | player_1 | player_2 | winner 
1/11 'A'   'B'  'A' 
2/11 'C'   'B'  'C' 
3/11 'A'   'D'  'A' 
4/11 'A'   'C'  'C' 

ratings_df 

player | rating 
'A'  1000 
'B'  1000 
'C'  1000 
'D'  1000 

다음과 같은 알고리즘 업데이트 등급 (sudocode)이 있습니다.

def update_ratings(match,parameter): 
    #(1) use current ratings to predict the likelihood of either player winning the match 
    #(2) using the outcome of the match to update player ratings 
    #(3) update the two players current ratings in the global dataframe based on the result of the match. 
    #(4) Return the square of the forecast's prediction error. 

모델의 예측 정확도에서 다른 매개 변수 값의 성능을 비교하고 싶습니다. 그러나 나는 '등급'데이터 프레임의 사본을 만들거나 함수 호출간에 등급 데이터 프레임을 재설정하는 데 어려움을 겪고 있습니다. 주어진 매개 변수 값의 성능을 계산할 때 다음 코드를 사용하고 있습니다.

def calc_brier(parameter,matches_df): 
    #reset dataframe to initial values (1000 for all players) 
    start_ratings = np.repeat(1000.0,len(unique_players)) 
    ratings_df = pd.DataFrame(data=[start_ratings],columns=unique_players) 
    brier = 0 
    for index, row in matches_df.iterrows(): 
     brier += update_ratings(row,parameter) 
    return brier 

그러나 올바른 결과를 제공하지는 않습니다. 글로벌 등급 데이터 프레임은 'calc_brier'함수를 호출 할 때 재설정되지 않으며 결과적으로 동일한 매개 변수를 여러 번 호출하면 calc_brier 함수가 일관되지 않습니다. 'calc_brier'를 호출하기 전후에 글로벌 등급 데이터 프레임을 올바르게 재설정하거나 다른 매개 변수 값의 성능을 비교하는 궁극적 인 목표를 달성하기 위해 대체 구조를 사용하려면 어떻게해야합니까?

답변

0

등급을 저장하기 위해 데이터 프레임 대신 사전을 사용하는 경우에 작동합니다. 다음은 작동하는 버전입니다 (등급 df는 이름이 키와 등급 인 사전을 1000에서 시작한 값으로 사용). 원래 코드가 무엇이 잘못되었는지 확실하지 않습니다.

def calc_brier(parameter): 
    for player in unique_players: 
     ratings_dict[player]=1000.0 
    brier = 0 
    for index, row in matches_df.iterrows(): 
     brier += update_ratings(row,k_factor) 
    return brier 
관련 문제