Kaggle competition: 2019  ML competition with KISTI
<SOS> metric: accuracy Kaggle data source: 2019-ml-competition-with-kisti<predict_on_test>
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline
predictions = gbdt.predict(test_ohe)
 predictions.shape<prepare_output>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_train[['Ticket', 'Survived']].groupby(['Survived'], as_index=True ).count()
df_train[['Ticket', 'Pclass']].groupby(['Pclass'], as_index=True ).count()
submit = pd.DataFrame(test['PassengerId'],columns=['PassengerId'])
 submit['Survived'] = predictions
submit.head()<save_to_csv>
for col in df_train.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_train[col].isnull().sum() / df_train[col].shape[0]))
print(msg )
submit.to_csv("Submissions.csv",index=False)
 print("Finished saving the file" )<load_from_csv>
for col in df_test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_test[col].isnull().sum() / df_test[col].shape[0]))
print(msg )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
for col in df_test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_test[col].isnull().sum() / df_test[col].shape[0]))
print(msg )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<count_missing_values>
df_test.loc[df_test.Fare.isnull() , 'Fare'] = df_test['Fare'].mean()
df_train['Fare'] = df_train['Fare'].map(lambda i: np.log(i)if i > 0 else 0)
 df_test['Fare'] = df_test['Fare'].map(lambda i: np.log(i)if i > 0 else 0 )
missing_data=train_data.isnull().sum()
missing_data<count_missing_values>
df_train['Initial']= df_train.Name.str.extract('([A-Za-z]+)\.')
 df_test['Initial']= df_test.Name.str.extract('([A-Za-z]+)\.' )
test_data.isnull().sum()<drop_column>
df_train['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True)
 df_test['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True )
train_data.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 test_data.drop(['Name','Ticket','Cabin'],axis=1,inplace=True )<count_missing_values>
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mr'),'Age'] = 33
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mrs'),'Age'] = 36
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Master'),'Age'] = 5
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Miss'),'Age'] = 22
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Other'),'Age'] = 46
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mr'),'Age'] = 33
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mrs'),'Age'] = 36
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Master'),'Age'] = 5
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Miss'),'Age'] = 22
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Other'),'Age'] = 46
train_data.isnull().sum()
train_data.head()<count_missing_values>
df_train['Embarked'].fillna('S', inplace=True )
test_data.isnull().sum()<feature_engineering>
df_train['Age_cat'] = 0
df_train.loc[df_train['Age'] < 20, 'Age_cat'] = 0
df_train.loc[(20 <= df_train['Age'])&(df_train['Age'] < 26), 'Age_cat'] = 1
df_train.loc[(26 <= df_train['Age'])&(df_train['Age'] < 33), 'Age_cat'] = 2
df_train.loc[(33 <= df_train['Age'])&(df_train['Age'] < 39), 'Age_cat'] = 3
df_train.loc[(39 <= df_train['Age']), 'Age_cat'] = 4
df_test['Age_cat'] = 0
df_test.loc[df_test['Age'] < 20, 'Age_cat'] = 0
df_test.loc[(20 <= df_test['Age'])&(df_test['Age'] < 26), 'Age_cat'] = 1
df_test.loc[(26 <= df_test['Age'])&(df_test['Age'] < 33), 'Age_cat'] = 2
df_test.loc[(33 <= df_test['Age'])&(df_test['Age'] < 39), 'Age_cat'] = 3
df_test.loc[(39 <= df_test['Age']), 'Age_cat'] = 4
train_data['Age'] = train_data['Age'].fillna(train_data['Age'].mean())
 test_data['Age'] = test_data['Age'].fillna(test_data['Age'].mean())
 test_data['Fare'] = test_data['Fare'].fillna(test_data['Fare'].median() )<correct_missing_values>
train_data.dropna(subset = ["Embarked"], inplace=True )<categorify>
print(df_train['Age_cat'].value_counts() )
train_data = pd.get_dummies(train_data, columns=["Sex"], drop_first=True)
 train_data = pd.get_dummies(train_data, columns=["Embarked"],drop_first=True )<categorify>
df_train['Initial'] = df_train['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4})
 df_test['Initial'] = df_test['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4} )
test_data = pd.get_dummies(test_data, columns=["Sex"], drop_first=True)
 test_data = pd.get_dummies(test_data, columns=["Embarked"],drop_first=True )<prepare_x_and_y>
df_train['Embarked'] = df_train['Embarked'].map({'C': 0, 'Q': 1, 'S': 2})
 df_test['Embarked'] = df_test['Embarked'].map({'C': 0, 'Q': 1, 'S': 2})
y = train_data["Survived"]
X = train_data.drop(['Survived'], axis=1 )<normalization>
df_train['Sex'] = df_train['Sex'].map({'female': 0, 'male': 1})
 df_test['Sex'] = df_test['Sex'].map({'female': 0, 'male': 1})
sc=StandardScaler()
sc.fit(train_data.drop(['Survived', 'PassengerId'], axis = 1))
X_train = sc.transform(train_data.drop(['Survived', 'PassengerId'], axis = 1))
X_train<train_model>
df_train = pd.get_dummies(df_train, columns=['Age_cat'], prefix='Age_cat')
 df_test = pd.get_dummies(df_test, columns=['Age_cat'], prefix='Age_cat' )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.23, random_state = 5)
 model_LR = LogisticRegression(max_iter=5000)
 model_LR.fit(X_train, y_train)
 LR_predict = model_LR.predict(X_test)
 LR_score = model_LR.score(X_test,y_test)
 model_X = XGBClassifier(eta=0.1, n_estimators=50,
max_depth=5, subsample=0.6, colsample_bytree=0.7,objective= 'binary:logistic',
scale_pos_weight=1, seed=27)
 model_X.fit(X_train, y_train)
 X_predict = model_X.predict(X_test)
 X_score = model_X.score(X_test,y_test)
 rfc = RandomForestClassifier(n_estimators=6)
 rfc.fit(X_train, y_train)
 RFC_predict = rfc.predict(X_test)
 RFC_score = rfc.score(X_test,y_test)
 model_DTC = DecisionTreeClassifier(max_depth=7, min_samples_leaf=6, min_samples_split=2)
 model_DTC.fit(X_train, y_train)
 DTC_predict = model_DTC.predict(X_test)
 DTC_score = model_DTC.score(X_test,y_test)
 model_GB = GradientBoostingClassifier(random_state=10, n_estimators=1500,min_samples_split=100, max_depth=6)
 model_GB.fit(X, y)
 GB_predict = model_GB.predict(X_test)
 GB_score = model_GB.score(X_test,y_test)
 x1=LR_score, X_score, RFC_score, DTC_score, GB_score
print(x1)
 sns.distplot(a=x1, kde=True)
 plt.legend()
plt.title('Accuracy estimates of Logistic regression, XGB, decision trees, Random Forest, and Gradient Boosting' )<find_best_model_class>
df_train = pd.get_dummies(df_train, columns=['Initial'], prefix='Initial')
 df_test = pd.get_dummies(df_test, columns=['Initial'], prefix='Initial' )
gnb= GaussianNB()
gnb.fit(X_train, y_train)
 prediction = gnb.predict(X_test)
 cross_scores = cross_val_score(gnb,X_train,y_train,cv=8)
 print(cross_scores)
 sns.distplot(a=cross_scores, kde=True)
 plt.legend()<find_best_model_class>
df_train = pd.get_dummies(df_train, columns=['Embarked'], prefix='Embarked')
 df_test = pd.get_dummies(df_test, columns=['Embarked'], prefix='Embarked' )
neigh= KNeighborsClassifier(n_neighbors=5, leaf_size=30)
 neigh.fit(X_train, y_train)
 KN_predict = neigh.predict(X_test)
 cross_scores = cross_val_score(neigh,X_train,y_train,cv=8)
 print(cross_scores)
 print(accuracy_score(KN_predict, y_test))<save_to_csv>
df_train.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin'], axis=1, inplace=True)
 df_test.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin'], axis=1, inplace=True)
predictions = model_X.predict(test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.model_selection import train_test_split
np.random.seed(0)
 %matplotlib inline 
<set_options>
X_train = df_train.drop('Survived', axis=1 ).values
target_label = df_train['Survived'].values
X_test = df_test.values
sns.set(style = 'white' , context = 'notebook' , palette = 'deep')
 rcParams['figure.figsize'] = 10,6<load_from_csv>
X_tr, X_vld, y_tr, y_vld = train_test_split(X_train, target_label, test_size=0.34, random_state=900 )
data_path = ".. /input/digit-recognizer/"
train = pd.read_csv(join(data_path,"train.csv"))
test = pd.read_csv(join(data_path,"test.csv"))<categorify>
from sklearn.metrics import accuracy_score
def process_data(data):
data = data/255.0
data = data.values.reshape(-1,28,28,1)
 return data
X_train = process_data(X_train)
 Y_train = to_categorical(Y_train,num_classes = 10 )<choose_model_class>
model = GradientBoostingClassifier()
model.fit(X_tr,y_tr)
 pred = model.predict(X_vld )
num = 5
model = [0]*num
for i in range(num):
model[i] = Sequential()
model[i].add(Conv2D(filters = 32, kernel_size =(5,5), padding = "same", activation = "relu", input_shape =(28,28,1)))
 model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters = 32, kernel_size =(5,5), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(pool_size =(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.25))
model[i].add(Flatten())
 model[i].add(Dense(1024, activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.25))
model[i].add(Dense(10, activation = "softmax"))
model[i].compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"])
 model[0].summary()<choose_model_class>
print('총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(pred, y_vld)) )
datagen = ImageDataGenerator(featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = False,
samplewise_std_normalization = False,
zca_whitening = False,
rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = False,
vertical_flip = False
)<split>
submission = pd.read_csv('.. /input/sample_submission.csv' )
epochs = 32
batch_size = 256
history = [0]*num
for i in range(num):
random_seed = i
X_train_, X_val_, Y_train_, Y_val_ = train_test_split(X_train, Y_train, test_size = 0.2, random_state=random_seed)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 early_stopping = EarlyStopping(monitor='val_accuracy',
min_delta=0,
patience=10,
verbose =1,
mode='auto')
 history[i] = model[i].fit_generator(datagen.flow(X_train_, Y_train_, batch_size = batch_size), epochs = epochs, validation_data =(X_val_,Y_val_), verbose = 1, steps_per_epoch = X_train.shape[0]//batch_size, callbacks=[learning_rate_reduction,early_stopping])
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(i+1,epochs,max(history[i].history['accuracy']),max(history[i].history['val_accuracy'])) )<predict_on_test>
prediction = model.predict(X_test)
 submission['Survived'] = prediction
<predict_on_test><EOS> 
submission.to_csv('./submission.csv', index=False )