Kaggle competition: 2019  ML competition with KISTI
<SOS> metric: accuracy Kaggle data source: 2019-ml-competition-with-kisti<feature_engineering>
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]<save_to_csv>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_train.head()
sample_submission.to_csv('submission.csv', index=False )<load_from_csv>
df_train['FamilySize'] = df_train['SibSp'] + df_train['Parch'] + 1
df_test['FamilySize'] = df_test['SibSp'] + df_test['Parch'] + 1
train = pd.read_csv(".. /input/ykc-cup-1st/train.csv")
 train.head()<load_from_csv>
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 )
test = pd.read_csv(".. /input/ykc-cup-1st/test.csv")
 test.head()<load_from_csv>
df_train['Fare_r'] = 0
df_train.loc[df_train['Fare'] < 2.065, 'Fare_r'] = 0
df_train.loc[(2.065 <= df_train['Fare'])&(df_train['Fare'] < 2.44), 'Fare_r'] = 1
df_train.loc[(2.44 <= df_train['Fare'])&(df_train['Fare'] < 3.18), 'Fare_r'] = 2
df_train.loc[(3.18 <= df_train['Fare'])&(df_train['Fare'] < 3.75), 'Fare_r'] = 3
df_train.loc[3.75 <= df_train['Fare'], 'Fare_r'] = 4
sample_submission = pd.read_csv(".. /input/ykc-cup-1st/sample_submission.csv")
 sample_submission.head()<categorify>
df_test['Fare_r'] = 0
df_test.loc[df_test['Fare'] < 2.065, 'Fare_r'] = 0
df_test.loc[(2.065 <= df_test['Fare'])&(df_test['Fare'] < 2.44), 'Fare_r'] = 1
df_test.loc[(2.44 <= df_test['Fare'])&(df_test['Fare'] < 3.18), 'Fare_r'] = 2
df_test.loc[(3.18 <= df_test['Fare'])&(df_test['Fare'] < 3.75), 'Fare_r'] = 3
df_test.loc[3.75 <= df_test['Fare'], 'Fare_r'] = 4
cat = ["store_id", "genre_name", "area_name", "day_of_week"]
for c in cat:
le = LabelEncoder()
train[c] = le.fit_transform(train[c].fillna("na"))
test[c] = le.transform(test[c].fillna("na"))
train.head()<define_variables>
df_train['Initial']= df_train.Name.str.extract('([A-Za-z]+)\.')
 df_test['Initial']= df_test.Name.str.extract('([A-Za-z]+)\.' )
features = ["store_id", "day_of_week", "genre_name", "area_name", "latitude", "longitude"]
target = "log_visitors"
n_split = 5<train_model>
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 )
preds_test = []
kfold = KFold(n_splits=n_split, shuffle = True, random_state=42)
 for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train)) :
print(f"--------fold {i_fold}-------")
 x_tr = train.loc[train_idx, features]
y_tr = train.loc[train_idx, target]
x_va = train.loc[valid_idx, features]
y_va = train.loc[valid_idx, target]
model = LGBMRegressor()
model.fit(x_tr, y_tr)
 pred_val = model.predict(x_va)
 rmse = np.sqrt(np.mean(( pred_val - y_va)** 2))
corr = np.corrcoef(pred_val, y_va)[0,1]
print("rmse : ", rmse)
 print("corr : ", corr)
 pred_test = model.predict(test[features])
 preds_test.append(pred_test )<save_to_csv>
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
final_pred = np.mean(np.vstack(preds_test), axis = 0)
 sample_submission["log_visitors"] = final_pred
sample_submission.to_csv("submission.csv", index = False )<load_from_csv>
df_train['Embarked'].fillna('S', inplace=True )
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df.head()<prepare_x_and_y>
df_train['Age_cat'] = 0
df_train.loc[df_train['Age'] < 21, 'Age_cat'] = 0
df_train.loc[(21 <= df_train['Age'])&(df_train['Age'] < 27), 'Age_cat'] = 1
df_train.loc[(27 <= 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
y_train = df["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Honoured"]
X_train = pd.get_dummies(df[features])
 X_test = pd.get_dummies(df_test[features] )<train_model>
df_test['Age_cat'] = 0
df_test.loc[df_train['Age'] < 21, 'Age_cat'] = 0
df_test.loc[(21 <= df_train['Age'])&(df_test['Age'] < 27), 'Age_cat'] = 1
df_test.loc[(27 <= df_train['Age'])&(df_test['Age'] < 33), 'Age_cat'] = 2
df_test.loc[(33 <= df_train['Age'])&(df_test['Age'] < 39), 'Age_cat'] = 3
df_test.loc[39 <= df_train['Age'], 'Age_cat'] = 4
rf = RandomForestClassifier(n_estimators=200, max_depth=3, random_state=1)
 rf.fit(X_train, y_train)
 scores = cross_val_score(rf, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores)
 print("Mean: ", scores.mean())
 print("Standard Deviation: ", scores.std())
<compute_train_metric>
print(df_train['Age_cat'].value_counts() )
lr = LogisticRegression()
lr.fit(X_train, y_train)
 scores = cross_val_score(lr, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores)
 print("Mean: ", scores.mean())
 print("Standard Deviation: ", scores.std() )<compute_train_metric>
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' )
model_svm = svm.SVC()
model_svm.fit(X_train, y_train)
 scores_svm = cross_val_score(model_svm, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_svm)
 print("Mean: ", scores_svm.mean())
 print("Standard Deviation: ", scores_svm.std() )<train_model>
df_train.drop(['Age'], axis=1, inplace=True)
 df_test.drop(['Age'], axis=1, inplace=True)
 df_train.head()
model_gbc = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
 model_gbc.fit(X_train, y_train)
 scores_gbc = cross_val_score(model_gbc, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gbc)
 print("Mean: ", scores_gbc.mean())
 print("Standard Deviation: ", scores_gbc.std() )<train_on_grid>
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})
model_bagging = BaggingClassifier()
model_bagging.fit(X_train, y_train)
 scores_bagging = cross_val_score(model_bagging, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_bagging)
 print("Mean: ", scores_bagging.mean())
 print("Standard Deviation: ", scores_bagging.std() )<train_on_grid>
df_train['Embarked'].unique()
df_train['Embarked'].value_counts()
model_gnb = GaussianNB()
model_gnb.fit(X_train, y_train)
 scores_gnb = cross_val_score(model_gnb, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gnb)
 print("Mean: ", scores_gnb.mean())
 print("Standard Deviation: ", scores_gnb.std() )<compute_train_metric>
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} )
model_xgb = XGBClassifier().fit(X_train, y_train)
 model_xgb.score(X_train, y_train)
 scores_xgb = cross_val_score(model_xgb, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gnb)
 print("Mean: ", scores_gnb.mean())
 print("Standard Deviation: ", scores_gnb.std() )<save_to_csv>
df_train['Embarked'].isnull().any()
eclf1 = VotingClassifier(estimators=[('rf', rf),('lr', lr),('svm', model_svm),('gbc', model_gbc),('bagging', model_bagging),('gnb', model_gnb),('xgb', model_xgb)], voting='hard')
 eclf1 = eclf1.fit(X_train, y_train)
 y_pred = eclf1.predict(X_test)
 print(y_pred)
 output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<define_variables>
df_train['Sex'] = df_train['Sex'].map({'female': 0, 'male': 1})
 df_test['Sex'] = df_test['Sex'].map({'female': 0, 'male': 1} )
train_path = ".. /input/competitive-data-science-predict-future-sales/sales_train.csv"
test_path = ".. /input/competitive-data-science-predict-future-sales/test.csv"
items_path = ".. /input/competitive-data-science-predict-future-sales/items.csv"
shops_path = ".. /input/competitive-data-science-predict-future-sales/shops.csv"
item_cat_path = ".. /input/competitive-data-science-predict-future-sales/item_categories.csv"<load_from_csv>
df_train = pd.get_dummies(df_train, columns=['Initial'], prefix='Initial')
 df_test = pd.get_dummies(df_test, columns=['Initial'], prefix='Initial' )
items = pd.read_csv(items_path)
 cats = pd.read_csv(item_cat_path)
 shops = pd.read_csv(shops_path)
 train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
<feature_engineering>
df_train = pd.get_dummies(df_train, columns=['Embarked'], prefix='Embarked')
 df_test = pd.get_dummies(df_test, columns=['Embarked'], prefix='Embarked' )
train = train[train.item_price > 0].reset_index(drop = True)
 train.loc[train.item_cnt_day < 1, "item_cnt_day"] = 0 
<feature_engineering>
df_train.drop(['PassengerId', 'Name', 'SibSp', 'Parch'], axis=1, inplace=True)
 df_test.drop(['PassengerId', 'Name', 'SibSp', 'Parch'], axis=1, inplace=True )
train.loc[train.shop_id == 0, "shop_id"] = 57
test.loc[test.shop_id == 0 , "shop_id"] = 57
train.loc[train.shop_id == 1, "shop_id"] = 58
test.loc[test.shop_id == 1 , "shop_id"] = 58
train.loc[train.shop_id == 11, "shop_id"] = 10
test.loc[test.shop_id == 11, "shop_id"] = 10
train.loc[train.shop_id == 40, "shop_id"] = 39
test.loc[test.shop_id == 40, "shop_id"] = 39<feature_engineering>
%matplotlib inline
shops.loc[ shops.shop_name == 'Сергиев Посад ТЦ "7Я"',"shop_name" ] = 'СергиевПосад ТЦ "7Я"'
shops["city"] = shops.shop_name.str.split(" " ).map(lambda x: x[0])
 shops["category"] = shops.shop_name.str.split(" " ).map(lambda x: x[1])
 shops.loc[shops.city == "!Якутск", "city"] = "Якутск" 
<feature_engineering>
X_train = df_train.drop('Survived', axis=1 ).values
target_label = df_train['Survived'].values
X_test = df_test.values
shops.category = shops.category.apply(lambda x: x if(x in category)else "etc" )<count_unique_values>
X_tr, X_vld, y_tr, y_vld = train_test_split(X_train,target_label, test_size=0.3, random_state=2018 )
for cat in shops.category.unique() :
print(cat, len(shops[shops.category == cat]))<categorify>
xgb= XGBClassifier()
xgb.fit(X_tr, y_tr)
 prediction = xgb.predict(X_vld)
 print('xgb : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_xgb = 100 * metrics.accuracy_score(prediction, y_vld )
shops["shop_category"] = LabelEncoder().fit_transform(shops.category)
 shops["shop_city"] = LabelEncoder().fit_transform(shops.city )<define_variables>
model = RandomForestClassifier()
model.fit(X_tr, y_tr)
 prediction = model.predict(X_vld)
 print('Random Forest1 : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_random_forest1 = 100 * metrics.accuracy_score(prediction, y_vld )
shops = shops[["shop_id", "shop_category", "shop_city"]]<feature_engineering>
random_forest = RandomForestClassifier()
random_forest.fit(X_tr, y_tr)
 prediction = random_forest.predict(X_vld)
 print('Random Forest2 : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_random_forest2 =100 * metrics.accuracy_score(prediction, y_vld )
cats["type_code"] = cats.item_category_name.apply(lambda x: x.split(" ")[0] ).astype(str)
 cats.loc[(cats.type_code == "Игровые")|(cats.type_code == "Аксессуары"), "category" ] = "Игры"<categorify>
logreg = LogisticRegression()
logreg.fit(X_tr, y_tr)
 prediction = logreg.predict(X_vld)
 print('Logistic : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_log = 100 * metrics.accuracy_score(prediction, y_vld )
cats.type_code = cats.type_code.apply(lambda x: x if(x in category)else "etc" )<count_unique_values>
svc = SVC()
svc.fit(X_tr, y_tr)
 prediction = svc.predict(X_vld)
 print(' Support Vector Machines: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_svc=100 * metrics.accuracy_score(prediction, y_vld )
for cat in cats.type_code.unique() :
print(cat, len(cats[cats.type_code == cat]))<categorify>
knn = KNeighborsClassifier(n_neighbors=26)
 knn.fit(X_tr, y_tr)
 prediction = knn.predict(X_vld)
 print(' k-Nearest Neighbor : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_knn=100 * metrics.accuracy_score(prediction, y_vld )
cats.type_code = LabelEncoder().fit_transform(cats.type_code)
 cats["split"] = cats.item_category_name.apply(lambda x: x.split("-"))
cats["subtype"] = cats.split.apply(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats["subtype_code"] = LabelEncoder().fit_transform(cats["subtype"])
 cats = cats[["item_category_id", "subtype_code", "type_code"]]<categorify>
gaussian = GaussianNB()
gaussian.fit(X_tr, y_tr)
 prediction = gaussian.predict(X_vld)
 print('naive Bayes : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_gaussian=100 * metrics.accuracy_score(prediction, y_vld )
def name_correction(x):
x = x.lower()
x = x.partition('[')[0]
x = x.partition('(')[0]
x = re.sub('[^A-Za-z0-9А-Яа-я]+', ' ', x)
 x = x.replace(' ', ' ')
 x = x.strip()
return x<feature_engineering>
perceptron = Perceptron()
perceptron.fit(X_tr, y_tr)
 prediction = perceptron.predict(X_vld)
 print('Perceptron : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_perceptron=100 * metrics.accuracy_score(prediction, y_vld )
items = pd.read_csv(items_path)
 items["name1"], items["name2"] = items.item_name.str.split("[", 1 ).str
items["name1"], items["name3"] = items.item_name.str.split("(", 1 ).str
items["name2"] = items.name2.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items["name3"] = items.name3.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items = items.fillna('0')
 items["item_name"] = items["item_name"].apply(lambda x: name_correction(x))
items.name2 = items.name2.apply(lambda x: x[:-1] if x !="0" else "0" )<feature_engineering>
linear_svc = LinearSVC()
linear_svc.fit(X_tr, y_tr)
 prediction = linear_svc.predict(X_vld)
 print('Linear SVC: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_linear_svc=100 * metrics.accuracy_score(prediction, y_vld )
items["type"] = items.name2.apply(lambda x: x[0:8] if x.split(" ")[0] == "xbox" else x.split(" ")[0])
 items.loc[(items.type == "x360")|(items.type == "xbox360")|(items.type == "xbox 360"),"type"] = "xbox 360"
items.loc[ items.type == "", "type"] = "mac"
items.type = items.type.apply(lambda x: x.replace(" ", ""))
items.loc[(items.type == 'pc')|(items.type == 'pс')|(items.type == "pc"), "type" ] = "pc"
items.loc[ items.type == 'рs3' , "type"] = "ps3" 
<groupby>
sgd = SGDClassifier()
sgd.fit(X_tr, y_tr)
 prediction = sgd.predict(X_vld)
 print('Stochastic Gradient Descent: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_sgd=100 * metrics.accuracy_score(prediction, y_vld )
group_sum = items.groupby(["type"] ).agg({"item_id": "count"})
 print(group_sum.reset_index())
 group_sum = group_sum.reset_index()<drop_column>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_tr, y_tr)
 prediction = decision_tree.predict(X_vld)
 print('Decision Tree: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_decision_tree= 100 * metrics.accuracy_score(prediction, y_vld )
items.name2 = items.name2.apply(lambda x: "etc" if(x in drop_cols)else x)
 items = items.drop(["type"], axis = 1 )<categorify>
gbk = GradientBoostingClassifier()
gbk.fit(X_tr,y_tr)
 prediction = gbk.predict(X_vld)
 acc_gbk = round(accuracy_score(prediction,y_vld)*100,2)
 print(acc_gbk )
items.name2 = LabelEncoder().fit_transform(items.name2)
 items.name3 = LabelEncoder().fit_transform(items.name3)
 items.drop(["item_name", "name1"],axis = 1, inplace= True)
 items.head()<data_type_conversions>
models = pd.DataFrame({
'Model': ['XGB','Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest1','Random Forest2', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_xgb, acc_svc, acc_knn, acc_log,
acc_random_forest1,acc_random_forest2, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
 print(models.sort_values(by='Score', ascending=False))
ts = time.time()
matrix = []
cols = ["date_block_num", "shop_id", "item_id"]
for i in range(34):
sales = train[train.date_block_num == i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype = np.int16))
matrix = pd.DataFrame(np.vstack(matrix), columns = cols)
 matrix["date_block_num"] = matrix["date_block_num"].astype(np.int8)
 matrix["shop_id"] = matrix["shop_id"].astype(np.int8)
 matrix["item_id"] = matrix["item_id"].astype(np.int16)
 matrix.sort_values(cols, inplace = True)
 time.time() - ts<feature_engineering>
submission = pd.read_csv('.. /input/sample_submission.csv' )
train["revenue"] = train["item_cnt_day"] * train["item_price"]<merge>
prediction = logreg.predict(X_test)
 submission['Survived'] = prediction
ts = time.time()
group = train.groupby(["date_block_num", "shop_id", "item_id"] ).agg({"item_cnt_day": ["sum"]})
 group.columns = ["item_cnt_month"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = cols, how = "left")
 matrix["item_cnt_month"] = matrix["item_cnt_month"].fillna(0 ).clip(0,20 ).astype(np.float16)
 time.time() - ts 
<data_type_conversions>
submission.to_csv('./my_first_submission.csv', index=False )