<jupyter_start><jupyter_text>Drop row with NaN value for winPlacePerc<jupyter_code>wrangled.drop(2744604, inplace=True)<jupyter_output><empty_output><jupyter_text>Anomaly<jupyter_code># 움직이지 않고 킬 한 경우
wrangled['killsWithoutMoving'] = (wrangled['kills'] > 0) & (wrangled['walkDistance'] == 0)
wrangled = wrangled.drop(wrangled[wrangled['killsWithoutMoving']==True].index)
wrangled = wrangled.drop(columns=['killsWithoutMoving'])
# Drop columns with 'flare' or'crash' or 'normal' matchtype
wrangled = wrangled[wrangled.matchType!='flarefpp']
wrangled = wrangled[wrangled.matchType!='flaretpp']
wrangled = wrangled[wrangled.matchType!='crashfpp']
wrangled = wrangled[wrangled.matchType!='crashtpp']
wrangled = wrangled[wrangled.matchType!='normal-squad-fpp']
wrangled = wrangled[wrangled.matchType!='normal-duo-fpp']
wrangled = wrangled[wrangled.matchType!='normal-solo-fpp']
wrangled = wrangled[wrangled.matchType!='normal-squad']
wrangled = wrangled[wrangled.matchType!='normal-duo']
wrangled = wrangled[wrangled.matchType!='normal-solo']
# wrangled = keep.copy()<jupyter_output><empty_output><jupyter_text>Group<jupyter_code>def get_max(df, max_of, to):
    return df.merge(df.groupby(['groupId'])[max_of].sum().to_frame(to), how='left', on=['groupId'])
    
def get_mean(df, mean_of, to):
    return df.merge(df.groupby(['groupId'])[mean_of].sum().to_frame(to), how='left', on=['groupId'])

def get_sum(df, sum_of, to):
    return df.merge(df.groupby(['groupId'])[sum_of].sum().to_frame(to), how='left', on=['groupId'])
# 팀원 수
wrangled = wrangled.merge(wrangled.groupby(['groupId']).size().to_frame('players_in_team'), how='left', on=['groupId'])
# 팀 전체 킬 수
wrangled = get_sum(wrangled, 'kills', 'total_kills')
# 팀 최고 킬 수
wrangled = get_max(wrangled, 'kills', 'max_kills')
# 킬 수 삭제
# wrangled.drop(columns = ['kills'], inplace=True)
# 팀 전체 기절 수
wrangled = get_sum(wrangled, 'DBNOs', 'total_DBNOs')
# 팀 최고 기절 수
wrangled = get_max(wrangled, 'DBNOs', 'max_DBNOs')
# 기절 수 삭제
# wrangled.drop(columns = ['DBNOs'], inplace=True)
# 팀 전체 데미지량
wrangled = get_sum(wrangled, 'damageDealt', 'total_damage')
# 팀 최고 데미지량
wrangled = get_max(wrangled, 'damageDealt', 'max_damage')

# wrangled.drop(columns = ['damageDealt'], inplace=True)
# 팀 전체 어시스트 수
wrangled = get_sum(wrangled, 'assists', 'total_assists')
# 팀 평균 어시스트 수
wrangled = get_mean(wrangled, 'assists', 'avg_assists')

# wrangled.drop(columns = ['assists'], inplace=True)
# 팀 최고 헤드샷
wrangled = get_max(wrangled, 'headshotKills', 'max_headshot')

# wrangled.drop(columns = ['headshotKills'], inplace=True)
# 팀 최고 kill place
wrangled = get_max(wrangled, 'killPlace', 'max_killplace')

# wrangled.drop(columns = ['killPlace'], inplace=True)
# 팀 최고 heals & boosts 수
wrangled = get_max(wrangled, 'heals', 'max_heals')
wrangled = get_max(wrangled, 'boosts', 'max_boosts')

# wrangled.drop(columns = ['heals'], inplace=True)
# wrangled.drop(columns = ['boosts'], inplace=True)
# totalDistance
wrangled['totalDistance'] = wrangled['walkDistance'] + wrangled['swimDistance'] + wrangled['rideDistance']
wrangled = get_max(wrangled, 'totalDistance', 'max_distance')

# 팀 최고 이동거리
wrangled = get_max(wrangled, 'walkDistance', 'max_walk')
wrangled = get_max(wrangled, 'rideDistance', 'max_ride')
wrangled = get_max(wrangled, 'swimDistance', 'max_swim')

# wrangled.drop(columns = ['walkDistance'], inplace=True)
# wrangled.drop(columns = ['rideDistance'], inplace=True)
# wrangled.drop(columns = ['swimDistance'], inplace=True)
# wrangled.drop(columns = ['totalDistance'], inplace=True)
# 팀 최고 무기 수
wrangled = get_max(wrangled, 'weaponsAcquired', 'max_weapons')

# wrangled.drop(columns = ['weaponsAcquired'], inplace=True)
# 팀 평균 소생 & 팀킬 수
wrangled = get_mean(wrangled, 'revives', 'avg_revives')
wrangled = get_mean(wrangled, 'teamKills', 'avg_teamKills')

# wrangled.drop(columns = ['revives'], inplace=True)
# wrangled.drop(columns = ['teamKills'], inplace=True)
wrangled = get_max(wrangled, 'killStreaks', 'max_killStreaks')
# wrangled.drop(columns = ['killStreaks'], inplace=True)

wrangled = get_max(wrangled, 'longestKill', 'max_longestKill')
# wrangled.drop(columns = ['longestKill'], inplace=True)

wrangled = get_max(wrangled, 'roadKills', 'max_roadKills')
# wrangled.drop(columns = ['roadKills'], inplace=True)

wrangled = get_max(wrangled, 'vehicleDestroys', 'max_vehicleDestroys')
# wrangled.drop(columns = ['vehicleDestroys'], inplace=True)<jupyter_output><empty_output><jupyter_text>Create features<jupyter_code># teamplay
wrangled['teamwork'] = wrangled['avg_assists'] + wrangled['avg_revives'] - wrangled['avg_teamKills']
# duo나 squad인데 인원수가 적은 경우(솔듀오 솔쿼드 듀쿼드 삼쿼드)
wrangled['less'] = 0
wrangled.loc[((wrangled['matchType'] == 'duo') | (wrangled['matchType'] == 'duo-fpp')) & (wrangled['players_in_team'] < 2), 'less'] = 1
wrangled.loc[((wrangled['matchType'] == 'squad') | (wrangled['matchType'] == 'squad-fpp')) & (wrangled['players_in_team'] < 4), 'less'] = 1
# matchType one hot encoding
wrangled = pd.get_dummies(wrangled, columns=['matchType'])
matchType_encoding = wrangled.filter(regex='matchType')
wrangled = pd.concat([wrangled, matchType_encoding], axis=1)
wrangled['headshotKills_over_kills'] = wrangled['max_headshot'] / wrangled['max_kills']
wrangled['headshotKills_over_kills'].fillna(0, inplace=True) # replace NaN value
wrangled['headshotKills_over_kills'].replace(np.inf, 0, inplace=True) # replace infinite value
wrangled['killPlace_over_maxPlace'] = wrangled['max_killplace'] / wrangled['maxPlace']
wrangled['killPlace_over_maxPlace'].fillna(0, inplace=True)
wrangled['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_heals'] = wrangled['max_distance'] / wrangled['max_heals']
wrangled['totalDistance_over_heals'].fillna(0, inplace=True)
wrangled['totalDistance_over_heals'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_boosts'] = wrangled['max_distance'] / wrangled['max_boosts']
wrangled['totalDistance_over_boosts'].fillna(0, inplace=True)
wrangled['totalDistance_over_boosts'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_kills'] = wrangled['max_distance'] / wrangled['max_kills']
wrangled['totalDistance_over_kills'].fillna(0, inplace=True)
wrangled['totalDistance_over_kills'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_damage'] = wrangled['max_distance'] / wrangled['max_damage']
wrangled['totalDistance_over_damage'].fillna(0, inplace=True)
wrangled['totalDistance_over_damage'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_DBNOs'] = wrangled['max_distance'] / wrangled['max_DBNOs']
wrangled['totalDistance_over_DBNOs'].fillna(0, inplace=True)
wrangled['totalDistance_over_DBNOs'].replace(np.inf, 0, inplace=True)
wrangled['totalDistance_over_weapons'] = wrangled['max_distance'] / wrangled['max_weapons']
wrangled['totalDistance_over_weapons'].fillna(0, inplace=True)
wrangled['totalDistance_over_weapons'].replace(np.inf, 0, inplace=True)<jupyter_output><empty_output><jupyter_text>Normalizae Feature<jupyter_code>wrangled['playersJoined'] = wrangled.groupby('matchId')['matchId'].transform('count')
wrangled['killsNorm'] = wrangled['kills']*((100-wrangled['playersJoined'])/100 + 1)
wrangled['damageDealtNorm'] = wrangled['damageDealt']*((100-wrangled['playersJoined'])/100 + 1)

wrangled['killsNorm'] = wrangled['max_kills']*((100-wrangled['playersJoined'])/100 + 1)
wrangled['damageDealtNorm'] = wrangled['max_damage']*((100-wrangled['playersJoined'])/100 + 1)<jupyter_output><empty_output><jupyter_text>Drop Features<jupyter_code># ID 
wrangled.drop(columns = ['Id'], inplace=True)
wrangled.drop(columns = ['groupId'], inplace=True)
wrangled.drop(columns = ['matchId'], inplace=True)
# Points ... 0값이 너무 많음
wrangled.drop(columns = ['killPoints'], inplace=True)
wrangled.drop(columns = ['rankPoints'], inplace=True)
wrangled.drop(columns = ['winPoints'], inplace=True)
# 중복된 column 삭제, index 값 초기화
wrangled = wrangled.loc[:,~wrangled.columns.duplicated()]
wrangled = wrangled.reset_index()
wrangled.drop(columns = ['index'], inplace=True)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>import sklearn
from sklearn import metrics
# Metric used for the PUBG competition (Mean Absolute Error (MAE))
from sklearn.metrics import mean_absolute_error
from sklearn.linear_model import LinearRegression

# Function to print the MAE (Mean Absolute Error) score
# This is the metric used by Kaggle in this competition
def print_score(m : LinearRegression):
    res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train), 
           'mae val: ', mean_absolute_error(m.predict(X_valid), y_valid)]
    if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
    print(res)
import xgboost
sample_size = 400000
sample = wrangled.iloc[0:sample_size]
original = sample.drop(columns=['winPlacePerc'])
target = sample['winPlacePerc']
# Function for splitting training and validation data
def split_vals(a, n : int): 
    return a[:n].copy(), a[n:].copy()

val_perc = 0.1 # % to use for validation set
n_valid = int(val_perc * sample_size) 
n_trn = len(original)-n_valid

# Split data
raw_train, raw_valid = split_vals(sample, n_trn)
X_train, X_valid = split_vals(original, n_trn)
y_train, y_valid = split_vals(target, n_trn)
model = xgboost.XGBRegressor(random_state=42, n_estimators=400, subsample = 0.8, colsample_bytree=1,max_depth=7, learning_rate=0.08)
model.fit(X_train, y_train)
print_score(model)<jupyter_output>[19:16:39] WARNING: C:/Jenkins/workspace/xgboost-win64_release_0.90/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
['mae train: ', 0.044055931834205385, 'mae val: ', 0.047380806182211646]<jupyter_text>Feature Importance<jupyter_code>def xgb_feat_importance(m, df):
     return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}).sort_values('imp', ascending=False)
# What are the most predictive features according to our basic random forest model
fi = xgb_feat_importance(model, original)

# Plot a feature importance graph for the 20 most important features
fi.plot('cols', 'imp', figsize=(14,15), legend=False, kind = 'barh', title='Important features')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1600905244514415, 'fraction_numerical': 0.013677063859096724, 'mean_word_length': 8.58867924528302, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '595443', 'license': 'MIT', 'n_tokens_mistral': 3981, 'n_tokens_neox': 3880, 'n_words': 638, 'path': 'learn_by_group_only.ipynb', 'repo_name': 'alstjgg/PUBG_Finish_Placement_Prediction', 'size': '10163'}