Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<normalization>
import pandas as pd
import os
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False)
 x_train= min_max_scaler.fit_transform(x_train)
 test=min_max_scaler.fit_transform(test)
<load_from_csv>
SUBMISSIONS_PATH = '/kaggle/input/submissions/'
mlp = MLPRegressor(solver='adam',activation='logistic', batch_size=1000,hidden_layer_sizes=(170, 110, 110, 110,100,1),alpha=1e-5, random_state=1,max_iter=200,verbose=True,learning_rate_init=0.001,early_stopping=True,tol=0.00001)
 mlp.fit(x_train, y)
 del x_train,y
gc.collect()
pred = mlp.predict(test)
 del test
gc.collect()
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 test= pd.read_csv('.. /input/test_V2.csv')
 df_sub['winPlacePerc'] = pred
df_sub = df_sub.merge(test[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 df_sub_group = df_sub.groupby(["matchId", "groupId"] ).first().reset_index()
df_sub_group["rank"] = df_sub_group.groupby(["matchId"])["winPlacePerc"].rank()
df_sub_group = df_sub_group.merge(
 df_sub_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 df_sub_group["adjusted_perc"] =(df_sub_group["rank"] - 1)/(df_sub_group["numGroups"] - 1)
 df_sub = df_sub.merge(df_sub_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 df_sub["winPlacePerc"] = df_sub["adjusted_perc"]
df_sub.loc[df_sub.maxPlace == 0, "winPlacePerc"] = 0
df_sub.loc[df_sub.maxPlace == 1, "winPlacePerc"] = 1
subset = df_sub.loc[df_sub.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
df_sub.loc[df_sub.maxPlace > 1, "winPlacePerc"] = new_perc
df_sub.loc[(df_sub.maxPlace > 1)&(df_sub.numGroups == 1), "winPlacePerc"] = 0
assert df_sub["winPlacePerc"].isnull().sum() == 0
print(df_sub[["Id", "winPlacePerc"]].head())
 df_sub[["Id", "winPlacePerc"]].to_csv("submission.csv", index=False)
 print('预测结束' )<define_variables>
submissions_all = []
for dirname, _, filenames in os.walk(SUBMISSIONS_PATH):
for filename in filenames:
submissions_all.append(os.path.join(dirname, filename))
submissions_all.sort()
print(submissions_all )
e_labels = ['matchId','groupId','killPlace', 'boosts', 'walkDistance', 'weaponsAcquired', 'damageDealt', 'heals',
'kills', 'longestKill', 'killStreaks', 'rideDistance', 'winPlacePerc', 'matchType']
variables = ['killPlace', 'boosts', 'walkDistance', 'weaponsAcquired', 'damageDealt', 'heals',
'kills', 'longestKill', 'killStreaks', 'rideDistance', 'winPlacePerc']
labels = [
'killPlace_g_norm', 'boosts_g_norm', 'walkDistance_g_norm',
'weaponsAcquired_g_norm', 'damageDealt_g_norm', 'heals_g_norm',
'kills_g_norm', 'longestKill_g_norm', 'killStreaks_g_norm',
'rideDistance_group', 'duo', 'duo-fpp', 'solo', 'solo-fpp', 'squad',
'squad-fpp']
def feature_engineering(pubg_data):

 pubg_data = pubg_data[e_labels]
pubg_group_means = pubg_data.groupby(['matchId','groupId'] ).mean().reset_index()
pubg_match_means = pubg_data.groupby(['matchId'] ).mean().reset_index()
pubg_engineered = pd.merge(pubg_data, pubg_group_means,
suffixes=["", "_group"], how = "left", on = ['matchId', 'groupId'])
 pubg_engineered = pd.merge(pubg_engineered, pubg_match_means,
suffixes=["_player", "_match"], how = "left", on = ['matchId'])
 for variable in variables:
pubg_engineered[variable+'_norm'] = pubg_engineered[variable+'_player']/(pubg_engineered[variable+'_match']+0.1)
 for variable in variables:
pubg_engineered[variable+'_g_norm'] = pubg_engineered[variable+'_group']/(pubg_engineered[variable+'_match']+0.1)
 one_hot = pd.get_dummies(pubg_engineered['matchType'])
 pubg_engineered = pubg_engineered.drop('matchType',axis = 1)
 pubg_engineered = pubg_engineered.join(one_hot)
 pubg_data = pubg_engineered.reset_index(drop=True)
 pubg_x = pubg_data[labels]
pubg_x = pubg_x.clip(lower=None, upper= pubg_x.quantile(0.99), axis = 1)
 pubg_y = pubg_data['winPlacePerc_player']
return pubg_x, pubg_y<load_from_csv>
def ensemble(submissions_all, sub_idx,weights=[]):
submission_with_weight = []
for i in range(len(sub_idx)) :
print(f"I'm taking submission {submissions_all[sub_idx[i]]} with weight {weights[i]}")
 submission = pd.read_csv(submissions_all[sub_idx[i]])
 submission = submission.loc[:, ['healthy', 'multiple_diseases', 'rust', 'scab']].values
submission_with_weight.append(submission*weights[i])
 submission_avg = sum(submission_with_weight)
 return submission_avg
pubg_data_all = pd.read_csv(".. /input/train_V2.csv")
 pubg_data = pubg_data_all.dropna()<prepare_x_and_y>
def make_submission_file(submission_avg, submissions_all):
submission_df = pd.read_csv(submissions_all[0])
 submission_df.iloc[:, 1:] = 0
submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = submission_avg
submission_df.to_csv('submission.csv', index=False )
<normalization><EOS> 
submission_avg = ensemble(submissions_all,[0,2,5], [0.1, 0.8,0.1])
 make_submission_file(submission_avg,submissions_all )