Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
name = 'Name'
name_pipeline = Pipeline(steps=[ 
('get_title', TitleSelector(name))
])
 title = 'Title'
title_pipeline = Pipeline(steps=[ 
('code_title', TitleCoder(title))
])
 age = 'Age'
age_pipeline = Pipeline(steps=[ 
('code_age', AgeCoder(age))
])
 sibsp = 'SibSp'
sibsp_pipeline = Pipeline(steps=[ 
('code_sibsp', SibSpCoder(sibsp))
])
 sibsp_pipeline2 = Pipeline(steps=[ 
('create_family_members', Family_members(sibsp))
])
 parch = 'Parch'
parch_pipeline = Pipeline(steps=[ 
('code_parch', ParchCoder(parch))
])
 family_members = 'Family_members'
family_members_pipeline = Pipeline(steps=[ 
('code_family_members', Family_membersCoder(family_members))
])
 ticket = 'Ticket'
ticket_pipeline = Pipeline(steps=[ 
('create_People_on_ticket', People_on_ticket(ticket)) ,
])
 people_on_ticket = 'People_on_ticket'
ticket_pipeline2 = Pipeline(steps=[ 
('code_People_on_ticket', People_on_ticketCoder(people_on_ticket)) ,
])
 is_alone_pipeline = Pipeline(steps=[ 
('create_is_alone', Is_Alone(people_on_ticket)) ,
])
 cabin = 'Cabin'
cabin_pipeline = Pipeline(steps=[ 
('code_cabin', Is_Cabin(cabin)) ,
])
 fare = 'Fare'
fare_pipeline = Pipeline(steps=[ 
('fare_class', FareClass(fare)) ,
])
 attribs1 = ['Pclass', 'Title_c', 'Sex', 'Age_c',
'Family_members_c', 'Embarked',
]
attribs1_pipeline = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy="most_frequent")) , 
('encoder', OneHotEncoder(sparse=False)) , 
('standarizer', StandardScaler())
 ])
 attribs2 = ['SibSp', 'Parch',
'Fare', 'Is_cabin']
attribs2_pipeline = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy="median")) , 
('standarizer', StandardScaler())
 ])
 preprocessor = ColumnTransformer(
 remainder = 'drop',
transformers=[ 
('first', attribs1_pipeline, attribs1), 
('second', attribs2_pipeline, attribs2)
 ])
 full_pipeline = FeatureUnion([ 
('name_pipeline', name_pipeline), 
('title_pipeline', title_pipeline), 
('age_pipeline', age_pipeline), 
('sibsp_pipeline2', sibsp_pipeline2), 
('family_members_pipeline', family_members_pipeline), 
('cabin_pipeline', cabin_pipeline), 
('preprocessor', preprocessor)
 ])
 full_pipeline2 = FeatureUnion([ 
('preprocessor', preprocessor)
 ] )<prepare_x_and_y>
develop_mode = False
if develop_mode:
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv', nrows=5000))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
else:
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
train_set_prepared = full_pipeline.fit_transform(train_set)
 train_set_prepared = full_pipeline2.fit_transform(train_set)
 X_train_prepared = train_set_prepared
y_train_prepared = train_set['Survived']
test_set_prepared = full_pipeline.fit_transform(test_set)
 test_set_prepared = full_pipeline2.fit_transform(test_set)
 X_test_prepared = test_set_prepared
y_test_prepared = test_set['Survived']<train_on_grid>
print('The sizes of the datasets are:')
 print('Training Dataset: ', df_train.shape)
 print('Testing Dataset: ', df_test.shape )
rf_model = RandomForestClassifier(random_state=42)
 params_grid = [
{'n_estimators': [100, 200, 300, 400, 500],
'criterion': ['gini', 'entropy'],
'min_samples_split': [2, 3, 4, 5],
'max_features': ['auto', 'log2', None],
'bootstrap': ['True', 'False']}
]
grid_search = GridSearchCV(rf_model, params_grid, cv=5, scoring="accuracy", n_jobs=1)
 grid_search.fit(X_train_prepared, y_train_prepared)
 grid_search.best_params_<train_on_grid>
group_tmp = df_train[df_train['matchId']=='df014fbee741c6']['groupId'].value_counts().sort_values(ascending=False )
params_grid2 = [
{'n_estimators': [120, 140, 160, 180, 200, 220, 240, 260, 280],
'criterion': ['gini', 'entropy'],
'min_samples_split': [4, 5],
'max_features': ['auto', None],
'bootstrap': ['True']}
]
grid_search2 = GridSearchCV(rf_model, params_grid2, cv=5, scoring="accuracy", n_jobs=1)
 grid_search2.fit(X_train_prepared, y_train_prepared)
 def indices_of_top_k(arr, k):
return np.sort(np.argpartition(np.array(arr), -k)[-k:])
 def fs_calculate_results() :
train_prediction = list()
test_prediction = list()
knn_model = KNeighborsClassifier()
feature_importances = grid_search2.best_estimator_.feature_importances_
for i in range(1,26):
indices_of_top = indices_of_top_k(feature_importances, i)
 X_train_restricted = X_train_prepared[:, indices_of_top]
X_test_restricted = X_test_prepared[:, indices_of_top]
params_grid_fs = [
{'n_neighbors': [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
'weights': ['uniform', 'distance'],
'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute'],
'metric': ['minkowski', 'euclidean', 'manhattan']}
]
grid_search_fs = GridSearchCV(knn_model, params_grid_fs, cv=5, scoring="accuracy", n_jobs=1)
 grid_search_fs.fit(X_train_restricted, y_train_prepared)
 train_prediction.append(grid_search_fs.best_score_)
 knn_final_model = grid_search_fs.best_estimator_
knn_predictions = knn_final_model.predict(X_test_restricted)
 test_prediction.append(accuracy_score(y_test_prepared, knn_predictions))
return train_prediction, test_prediction
train_pred, test_pred = fs_calculate_results()
fs_results = pd.DataFrame({'train_set': train_pred, 'test_set': test_pred})
 fs_results['train-test_difference'] = fs_results['train_set'] - fs_results['test_set']
fs_results.index += 1
fs_results = fs_results.sort_values('train-test_difference')
 fs_results<define_search_model>
warnings.filterwarnings('ignore' )
feature_importances = grid_search2.best_estimator_.feature_importances_
indices_of_top = indices_of_top_k(feature_importances, 25)
 X_train_prepared = X_train_prepared[:, indices_of_top]
X_test_prepared = X_test_prepared[:, indices_of_top]
knn_model = KNeighborsClassifier()
params_grid = [
{'n_neighbors': [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
'weights': ['uniform', 'distance'],
'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute'],
'metric': ['minkowski', 'euclidean', 'manhattan']}
]
grid_search = GridSearchCV(knn_model, params_grid, cv=5, scoring="accuracy", n_jobs=1)
 grid_search.fit(X_train_prepared, y_train_prepared)
 grid_search.best_params_<train_model>
def BuildFeature(is_train=True):

 y = None
test_idx = None
if is_train:
print("Reading train.csv")
 df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("Reading test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
df = reduce_mem_usage(df)
 print("Delete Unuseful Columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 if is_train:
print("Read Labels")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("Read Group mean features")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train:
df_out = agg.reset_index() [['matchId','groupId']]
else:
df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group max features")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group min features")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group size features")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("Read Match mean features")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("Read Match size features")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
print("Training dataset accuracy: ", grid_search.best_score_, sep="")
 knn_model = grid_search.best_estimator_
knn_predictions = knn_model.predict(X_test_prepared)
 print("Test dataset accuracy: ", accuracy_score(y_test_prepared, knn_predictions), sep="" )<train_model>
X_train, y_train, train_columns, _ = BuildFeature(is_train=True)
 X_test, _, _ , test_idx = BuildFeature(is_train=False )
knn_final_model = KNeighborsClassifier(algorithm='auto', metric='manhattan', n_neighbors=25, weights='uniform')
 knn_final_model.fit(X_train_prepared, y_train_prepared )<save_to_csv>
X_train =reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test )
titanic_sub_prepared = full_pipeline.fit_transform(titanic_sub)
 titanic_sub_prepared = full_pipeline2.fit_transform(titanic_sub)
 titanic_sub_prepared = titanic_sub_prepared[:, indices_of_top]
titanic_sub_predictions = knn_final_model.predict(titanic_sub_prepared)
 submission = pd.DataFrame({'PassengerId':titanic_sub['PassengerId'], 'Survived':titanic_sub_predictions})
 submission.to_csv('submission.csv', sep=',', index=False )<import_modules>
LR_model = LinearRegression(n_jobs=4, normalize=True)
 LR_model.fit(X_train,y_train )
if not sys.warnoptions:
warnings.simplefilter("ignore" )<load_from_csv>
LR_model.score(X_train,y_train )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<drop_column>
y_pred_train = LR_model.predict(X_train)
 y_pred_test = LR_model.predict(X_test )
train_ID = train['Id']
test_ID = test['Id']
train.drop("Id", axis = 1, inplace = True)
 test.drop("Id", axis = 1, inplace = True )<feature_engineering>
y_pred_test[y_pred_test>1] = 1
y_pred_test[y_pred_test<0] = 0
train["SalePrice"] = np.log1p(train["SalePrice"] )<prepare_x_and_y>
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )