Kaggle competition: Seleksi Calon Asisten GAIB
<SOS> metric: meanfscore Kaggle data source: seleksi-calon-asisten-gaib<sort_values>
pd.options.display.max_columns = 999
warnings.simplefilter(action='ignore')
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(train['Embarked'].dropna().mode() [0])
 train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<data_type_conversions>
test = pd.read_csv(".. /input/seleksidukungaib/test.csv")
 train = pd.read_csv(".. /input/seleksidukungaib/train.csv")
 sample_submission = pd.read_csv(".. /input/seleksidukungaib/sample_submission.csv" )
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train.head()<data_type_conversions>
dropped_column = ['idx', 'userId', 'num_transfer_trx', 'max_transfer_trx',
'min_transfer_trx', 'date', 'date_collected', 'isUpgradedUser']
test['Fare'].fillna(test['Fare'].dropna().median() , inplace=True)
 test.head()<data_type_conversions>
data = pd.concat([train,test],ignore_index=True)
 data = data.drop(dropped_column, axis = 1 )
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 train = train.drop(['FareBand'], axis=1)
 combine = [train, test]
train.head(10 )<data_type_conversions>
data = data.drop(['average_transfer_trx'], axis = 1 )
for dataset in combine:
dataset['Sex']=dataset['Sex'].map({'female':1,'male':0} ).astype(int)
 train.head(10 )<drop_column>
data.loc[data.isActive.isnull() == True]
data = data.dropna(subset=["isActive"] )
train=train.drop(['Name','PassengerId'],axis=1)
 test=test.drop(['Name'],axis=1 )<prepare_x_and_y>
data['premium'] = data['premium'].fillna(data['premium'].mode() )
X_train=train.drop(['Survived'],axis=1)
 Y_train=train.Survived
X_test=test.drop('PassengerId',axis=1 ).copy()<choose_model_class>
for column in data.columns:
if(column != "isChurned"):
data[column] = data[column].fillna(data[column].median() )
k_fold=KFold(len(Y_train),n_folds=10,shuffle=True,random_state=0 )<import_modules>
categorical_features = ['premium', 'super', 'pinEnabled']
le = LabelEncoder()
for col in categorical_features:
data[col] = le.fit_transform(list(data[col].values))
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC,LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import SGDClassifier<compute_train_metric>
Q3 = data.quantile(0.85 )
svc=SVC()
svc.fit(X_train, Y_train)
 Y_pred = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, Y_train)* 100, 2)
 acc_svc<find_best_model_class>
numerik_col = ['average_recharge_trx','average_topup_trx','max_recharge_trx','max_topup_trx',
'min_recharge_trx','min_topup_trx','num_recharge_trx','num_topup_trx','num_transaction',
'random_number','total_transaction']
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn<predict_on_test>
data['num_transaction_plus_num_recharge'] = data['num_transaction'] + data['num_recharge_trx']
data.drop(['num_transaction', 'num_recharge_trx'], axis=1, inplace = True)
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian<compute_train_metric>
train = data[~data.isChurned.isnull() ]
test = data[data.isChurned.isnull() ]
numerik_col = ['max_recharge_trx','average_recharge_trx',
'average_topup_trx', 'max_topup_trx',
'min_recharge_trx','min_topup_trx','num_topup_trx',
'random_number','total_transaction',
]
for col in(numerik_col):
train[col]=(( train[col]-train[col].min())/(train[col].max() -train[col].min()))
test[col]=(( test[col]-test[col].min())/(test[col].max() -test[col].min()))
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc<compute_train_metric>
train.duplicated().value_counts()
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 acc_sgd<choose_model_class>
train.drop_duplicates(keep = 'first', inplace = True)
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_pred = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree<train_model>
train.corr().style.background_gradient(cmap='coolwarm' )
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred1 = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest<create_dataframe>
min_cor = ['isActive', 'isVerifiedPhone', 'blocked', 'super', 'random_number']
for col in min_cor:
print('====== ', col, " ======")
 print(train[col].value_counts())
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN','Random Forest', 'Naive Bayes','Stochastic Gradient Decent', 'Linear SVC','Decision Tree'],
'Score': [acc_svc, acc_knn,acc_random_forest, acc_gaussian, acc_sgd, acc_linear_svc, acc_decision_tree]})
 models.sort_values(by='Score', ascending=False )<load_from_csv>
drop_from_cor = ['isActive', 'isVerifiedPhone', 'blocked', 'super', 'random_number']
train.drop(drop_from_cor, axis = 1, inplace = True)
 test.drop(drop_from_cor, axis = 1, inplace = True )
submission=pd.read_csv('.. /input/gender_submission.csv' )<save_to_csv>
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.tree import ExtraTreeClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm.classes import OneClassSVM
from sklearn.neural_network.multilayer_perceptron import MLPClassifier
from sklearn.neighbors.classification import RadiusNeighborsClassifier
from sklearn.neighbors.classification import KNeighborsClassifier
from sklearn.multioutput import ClassifierChain
from sklearn.multioutput import MultiOutputClassifier
from sklearn.multiclass import OutputCodeClassifier
from sklearn.multiclass import OneVsOneClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model.stochastic_gradient import SGDClassifier
from sklearn.linear_model.ridge import RidgeClassifierCV
from sklearn.linear_model.ridge import RidgeClassifier
from sklearn.linear_model.passive_aggressive import PassiveAggressiveClassifier
from sklearn.gaussian_process.gpc import GaussianProcessClassifier
from sklearn.ensemble.weight_boosting import AdaBoostClassifier
from sklearn.ensemble.gradient_boosting import GradientBoostingClassifier
from sklearn.ensemble.bagging import BaggingClassifier
from sklearn.ensemble.forest import ExtraTreesClassifier
from sklearn.ensemble.forest import RandomForestClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.calibration import CalibratedClassifierCV
from sklearn.naive_bayes import GaussianNB
from sklearn.semi_supervised import LabelPropagation
from sklearn.semi_supervised import LabelSpreading
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import NearestCentroid
from sklearn.svm import NuSVC
from sklearn.linear_model import Perceptron
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.svm import SVC
import xgboost as xgb
from xgboost import XGBClassifier
submission['Survived']= Y_pred1
submission['PassengerId']=test['PassengerId']
pd.DataFrame(submission,columns=['PassengerId','Survived'] ).to_csv('randomforest.csv',index=False)
<compute_train_metric>
Y = train["isChurned"]
X = train.drop(["isChurned"], axis = 1 )
scoring = 'accuracy'
results = cross_val_score(random_forest, X_train, Y_train, cv=k_fold, n_jobs=1, scoring=scoring)
 results<define_search_space>
random_state = 1
X_train, X_valid, y_train, y_valid = train_test_split(X, Y, test_size = 0.2, random_state = random_state )
param_grid = {"n_estimators": [40,50,60],
"max_depth": [3, 5],
"min_samples_split": [5, 10],
"min_samples_leaf": [5, 6],
"max_leaf_nodes": [10, 15],
"min_weight_fraction_leaf": [0.1]}
param_grid<train_on_grid>
def get_kfold() :
return KFold(n_splits=5, shuffle=True, random_state=1 )
grid_search = GridSearchCV(random_forest, param_grid=param_grid,scoring='accuracy',cv=k_fold,n_jobs=-1)
 grid_search.fit(X_train, Y_train )<find_best_params>
all_model = [RandomForestClassifier() ,ExtraTreeClassifier() , LogisticRegression() ,RidgeClassifier() ,
DecisionTreeClassifier() , KNeighborsClassifier() , PassiveAggressiveClassifier() ,
]
print(grid_search.best_score_)
 print(grid_search.best_params_ )<predict_on_test>
params = {'loss_function':'Logloss',
'eval_metric':'F1',
'iterations' : 1000,
'learning_rate': 0.01,
'verbose': 1000,
'random_seed': random_state
}
cbc = CatBoostClassifier(**params )
xgb=XGBClassifier(max_depth=5, n_estimators=900, learning_rate=1,gamma=0,min_child_weight=2, reg_alpha=0.1,subsample=0.8,cv=k_fold)
 xgb.fit(X_train,Y_train)
 Y_pred = xgb.predict(X_test)
 acc_xgb = round(xgb.score(X_train, Y_train)* 100, 2)
 acc_xgb<set_options>
data_dmatrix = xgb.DMatrix(data=X,label=Y )
print(os.listdir(".. /input"))
warnings.filterwarnings('ignore')
<load_from_csv>
params = {"objective":"binary:logistic",'colsample_bytree': 0.3,'learning_rate': 0.1,
'max_depth': 10, 'alpha': 10}
try:
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv")
 print('Files are loaded')
 except:
print('Something went wrong.' )<concatenate>
for model in all_model:
scores = cross_val_score(model, X, Y, cv=get_kfold() , scoring='f1')
 print("Model : " , model , " has f1 score : " , scores.mean() )
ship = df_train.append(df_test, ignore_index=True)
<count_values>
df_train['Survived'].value_counts(normalize=True )<count_missing_values>
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV
print('"ship" columns with null values:
', ship.isnull().sum() )<create_dataframe>
model = LogisticRegression()
solvers = ['newton-cg', 'lbfgs', 'liblinear']
penalty = ['l2']
c_values = [100, 10, 1.0, 0.1, 0.01]
try:
ship_orig = ship.copy()
print('Copies have been made')
 except:
print('Something went wrong.' )<drop_column>
grid = dict(solver=solvers,penalty=penalty,C=c_values)
 cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)
 grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='f1',error_score=0, verbose = 3)
 grid_result = grid_search.fit(X, Y)
columns_to_drop = ['PassengerId', 'Ticket']
ship = ship.drop(columns_to_drop, axis=1)
 print('The following columns have been dropped: ', columns_to_drop)
 del columns_to_drop<count_missing_values>
print("Best: %f using %s" %(grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f(%f)with: %r" %(mean, stdev, param))
print('Missing Values in "ship" data: ', list(col for col in ship.columns if ship[col].isnull().any() == True))<count_values>
model = LogisticRegression(C= 1.0, penalty= 'l2', solver = 'newton-cg' )
ship['Embarked'].value_counts()<categorify>
scores = cross_val_score(model, X, Y, cv=get_kfold() , scoring='f1')
 print(scores.mean() )
ship["Embarked"] = ship["Embarked"].fillna("S")
 print('Embarked column blanks have been filled with the value "S"' )<categorify>
model = model.fit(X,Y )
median_value = ship["Fare"].median()
print('The median value for the "Fare" column is: ', median_value)
 ship["Fare"].fillna(median_value, inplace=True)
 print('Fare column blanks have been filled with the median value of the column')
 del median_value<count_missing_values>
test = test.drop(['isChurned'], axis = True )
print('Number of null values in Age column:', ship['Age'].isnull().sum())
 print('The mean of the Age column: ', ship['Age'].mean() )<feature_engineering>
prediction = model.predict(test )
imputer_tool = SimpleImputer()
ship_numeric = ship.select_dtypes(exclude=['object'])
 cols_with_missing = ['Age']
for col in cols_with_missing:
ship_numeric.loc[:, col + '_was_missing'] = ship_numeric[col].isnull()
ship_numeric_imp = imputer_tool.fit_transform(ship_numeric.values)
 ship_numeric = pd.DataFrame(ship_numeric_imp, index=ship_numeric.index, columns=ship_numeric.columns)
 ship_numeric.head()<filter>
prediction = prediction.astype(int )
ship_numeric.loc[lambda df: df.Age_was_missing == 1, :][:5]<prepare_output>
sample_submission["isChurned"] = prediction
sample_submission["isChurned"].value_counts()
<count_missing_values><EOS> 
sample_submission.to_csv("submission.csv", index = False )