Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
%matplotlib inline
warnings.filterwarnings('ignore')
 sns.set_palette('cool' )
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"] = "Якутск"<categorify>
training_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
shops["shop_category"] = LabelEncoder().fit_transform(shops.category)
 shops["shop_city"] = LabelEncoder().fit_transform(shops.city)
 shops = shops[["shop_id", "shop_category", "shop_city"]]<feature_engineering>
training_data.isnull().sum()
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>
test_data.isnull().sum()
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>
training_data.PassengerId.nunique()
passengerId = test_data['PassengerId']
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>
training_data.drop(labels='PassengerId', axis=1, inplace=True)
 test_data.drop(labels='PassengerId', axis=1, inplace=True )
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>
print(training_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean() )
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>
group_sum = items.groupby(["type"] ).agg({"item_id": "count"})
 group_sum = group_sum.reset_index()
drop_cols = []
for cat in group_sum.type.unique() :
if group_sum.loc[(group_sum.type == cat), "item_id"].values[0] <40:
drop_cols.append(cat)
 items.name2 = items.name2.apply(lambda x: "other" if(x in drop_cols)else x)
 items = items.drop(["type"], axis = 1 )<categorify>
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>
training_data.Name.nunique()
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>
training_data['Title'] = training_data['Name'].apply(lambda x: x.split(',')[1] ).apply(lambda x: x.split() [0])
 test_data['Title'] = test_data['Name'].apply(lambda x: x.split(',')[1] ).apply(lambda x: x.split() [0])
 training_data['Name_Len'] = training_data['Name'].apply(lambda x: len(x))
test_data['Name_Len'] = test_data['Name'].apply(lambda x: len(x))
training_data.drop(labels='Name', axis=1, inplace=True)
 test_data.drop(labels='Name', axis=1, inplace=True )
train["revenue"] = train["item_cnt_day"] * train["item_price"]<merge>
test_data.Name_Len =(test_data.Name_Len/10 ).astype(np.int64)+1
training_data.Name_Len =(training_data.Name_Len/10 ).astype(np.int64)+1
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 ).astype(np.float16)
 time.time() - ts<data_type_conversions>
print(training_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean() )
test["date_block_num"] = 34
test["date_block_num"] = test["date_block_num"].astype(np.int8)
 test["shop_id"] = test.shop_id.astype(np.int8)
 test["item_id"] = test.item_id.astype(np.int16 )<concatenate>
print(training_data[['Name_Len', 'Survived']].groupby(['Name_Len'], as_index=False ).mean() )
ts = time.time()
matrix = pd.concat([matrix, test.drop(["ID"],axis = 1)], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace = True)
 time.time() - ts<data_type_conversions>
print(training_data[['Sex', 'Survived']].groupby(['Sex'], as_index = False ).mean() )
ts = time.time()
matrix = pd.merge(matrix, shops, on = ["shop_id"], how = "left")
 matrix = pd.merge(matrix, items, on = ["item_id"], how = "left")
 matrix = pd.merge(matrix, cats, on = ["item_category_id"], how = "left")
 matrix["shop_city"] = matrix["shop_city"].astype(np.int8)
 matrix["shop_category"] = matrix["shop_category"].astype(np.int8)
 matrix["item_category_id"] = matrix["item_category_id"].astype(np.int8)
 matrix["subtype_code"] = matrix["subtype_code"].astype(np.int8)
 matrix["name2"] = matrix["name2"].astype(np.int8)
 matrix["name3"] = matrix["name3"].astype(np.int16)
 matrix["type_code"] = matrix["type_code"].astype(np.int8)
 time.time() - ts<merge>
def lag_feature(df,lags, cols):
for col in cols:
print(col)
 tmp = df[["date_block_num", "shop_id","item_id",col ]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ["date_block_num", "shop_id", "item_id", col + "_lag_"+str(i)]
shifted.date_block_num = shifted.date_block_num + i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df<merge>
training_data.Age.isnull().sum()
training_age_n = training_data.Age.dropna(axis=0 )
ts = time.time()
group = matrix.groupby(["date_block_num"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1], ["date_avg_item_cnt"])
 matrix.drop(["date_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts<merge>
full_data = pd.concat([training_data, test_data] )
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix.date_item_avg_item_cnt = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ['date_item_avg_item_cnt'])
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
train_age_mean = full_data.Age.mean()
train_age_std = full_data.Age.std()
train_age_null = training_data.Age.isnull().sum()
rand_tr_age = np.random.randint(train_age_mean - train_age_std, train_age_mean + train_age_std, size=train_age_null)
 training_data['Age'][np.isnan(training_data['Age'])] = rand_tr_age
training_data['Age'] = training_data['Age'].astype(int)+ 1
test_age_mean = full_data.Age.mean()
test_age_std = full_data.Age.std()
test_age_null = test_data.Age.isnull().sum()
rand_ts_age = np.random.randint(test_age_mean - test_age_std, test_age_mean + test_age_std, size=test_age_null)
 test_data['Age'][np.isnan(test_data['Age'])] = rand_ts_age
test_data['Age'] = test_data['Age'].astype(int)
 training_data.Age =(training_data.Age/15 ).astype(np.int64)
 test_data.Age =(test_data.Age/15 ).astype(np.int64)+ 1
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_avg_item_cnt"])
 matrix.drop(["date_shop_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts<merge>
print(training_data[['Age', 'Survived']].groupby(['Age'], as_index = False ).mean() )
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id","item_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_item_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id","item_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_item_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_item_avg_item_cnt"])
 matrix.drop(["date_shop_item_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts<merge>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix.date_shop_subtype_avg_item_cnt = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_shop_subtype_avg_item_cnt'])
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
print(training_data[['FamilySize', 'Survived']].groupby(training_data['FamilySize'], as_index=False ).mean() )
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_city_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', "shop_city"], how='left')
 matrix.date_city_avg_item_cnt = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_city_avg_item_cnt'])
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
training_data['isAlone'] = training_data['FamilySize'].map(lambda x: 1 if x == 1 else 0)
 test_data['isAlone'] = test_data['FamilySize'].map(lambda x: 1 if x == 1 else 0 )
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'shop_city'], how='left')
 matrix.date_item_city_avg_item_cnt = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_item_city_avg_item_cnt'])
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<data_type_conversions>
training_data.drop(labels=['SibSp', 'Parch'], axis=1, inplace=True)
 test_data.drop(labels=['SibSp', 'Parch'], axis=1, inplace=True)
 training_data.head()
ts = time.time()
group = train.groupby(["item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["item_id"], how = "left")
 matrix["item_avg_item_price"] = matrix.item_avg_item_price.astype(np.float16)
 group = train.groupby(["date_block_num","item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["date_item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["date_block_num","item_id"], how = "left")
 matrix["date_item_avg_item_price"] = matrix.date_item_avg_item_price.astype(np.float16)
 lags = [1, 2, 3]
matrix = lag_feature(matrix, lags, ["date_item_avg_item_price"])
 for i in lags:
matrix["delta_price_lag_" + str(i)] =(matrix["date_item_avg_item_price_lag_" + str(i)]- matrix["item_avg_item_price"])/ matrix["item_avg_item_price"]
def select_trends(row):
for i in lags:
if row["delta_price_lag_" + str(i)]:
return row["delta_price_lag_" + str(i)]
return 0
matrix["delta_price_lag"] = matrix.apply(select_trends, axis = 1)
 matrix["delta_price_lag"] = matrix.delta_price_lag.astype(np.float16)
 matrix["delta_price_lag"].fillna(0 ,inplace = True)
 features_to_drop = ["item_avg_item_price", "date_item_avg_item_price"]
for i in lags:
features_to_drop.append("date_item_avg_item_price_lag_" + str(i))
features_to_drop.append("delta_price_lag_" + str(i))
matrix.drop(features_to_drop, axis = 1, inplace = True)
 time.time() - ts<merge>
training_data['Ticket_Len'] = training_data['Ticket'].apply(lambda x: len(x))
test_data['Ticket_Len'] = test_data['Ticket'].apply(lambda x: len(x))
ts = time.time()
group = train.groupby(["date_block_num","shop_id"] ).agg({"revenue": ["sum"] })
 group.columns = ["date_shop_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group , on = ["date_block_num", "shop_id"], how = "left")
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(["shop_id"] ).agg({ "date_block_num":["mean"] })
 group.columns = ["shop_avg_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["shop_id"], how = "left")
 matrix["shop_avg_revenue"] = matrix.shop_avg_revenue.astype(np.float32)
 matrix["delta_revenue"] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix["delta_revenue"] = matrix["delta_revenue"].astype(np.float32)
 matrix = lag_feature(matrix, [1], ["delta_revenue"])
 matrix["delta_revenue_lag_1"] = matrix["delta_revenue_lag_1"].astype(np.float32)
 matrix.drop(["date_shop_revenue", "shop_avg_revenue", "delta_revenue"] ,axis = 1, inplace = True)
 time.time() - ts<categorify>
print(training_data[['Ticket_Len', 'Survived']].groupby(training_data['Ticket_Len'], as_index=False ).mean() )
matrix["month"] = matrix["date_block_num"] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix["days"] = matrix["month"].map(days ).astype(np.int8 )<feature_engineering>
training_data.drop(labels='Ticket', axis=1, inplace=True)
 test_data.drop(labels='Ticket', axis=1, inplace=True)
 training_data.head()
ts = time.time()
matrix["item_shop_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id","shop_id"])["date_block_num"].transform('min')
 matrix["item_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id"])["date_block_num"].transform('min')
 time.time() - ts<feature_engineering>
training_data.Fare =(training_data.Fare /20 ).astype(np.int64)+ 1
test_data.Fare =(test_data.Fare /20 ).astype(np.int64)+ 1
ts = time.time()
matrix = matrix[matrix["date_block_num"] > 3]
time.time() - ts<set_options>
print(training_data[['Fare','Survived']].groupby(['Fare'], as_index = False ).mean() )
rcParams['figure.figsize'] = 12, 4<drop_column>
data = matrix.copy()
del matrix
gc.collect()<prepare_x_and_y>
cabin_null = float(test_data.Cabin.isnull().sum())
 print(cabin_null/len(test_data)*100 )
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<set_options>
cabin_null = float(training_data.Cabin.isnull().sum())
 print(cabin_null/len(training_data)*100 )
del data
gc.collect() ;<train_model>
training_data['hasCabin'] = training_data.Cabin.notnull().astype(int)
 test_data['hasCabin'] = test_data.Cabin.notnull().astype(int )
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 20)
 time.time() - ts<save_to_csv>
training_data.drop(labels='Cabin', axis=1, inplace=True)
 training_data.head()
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False )<load_from_csv>
test_data.drop(labels='Cabin', axis=1, inplace=True)
 test_data.head()
test = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv')
 item_cat = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 train = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 shop = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv' )<count_values>
train['item_id'].value_counts(ascending=False)[:10]<filter>
training_data['Embarked'] = training_data['Embarked'].fillna('S' )
items.loc[items['item_id']==20949]<sort_values>
print(training_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index = False ).mean() )
train['item_cnt_day'].sort_values(ascending=False)[:10]<filter>
print(training_data[['Embarked', 'Fare']].groupby(['Embarked'], as_index = False ).mean() )
train[train['item_cnt_day'] == 2169]<filter>
train[train['item_cnt_day'] == 1000]<filter>
training_data.head()
items[items['item_id'] == 11373]<filter>
X = training_data.iloc[:, 1:12].values
y = training_data.iloc[:, 0].values
train = train[train['item_cnt_day'] < 2000]<sort_values>
label_encoder_sex_tr = LabelEncoder()
label_encoder_title_tr = LabelEncoder()
label_encoder_embarked_tr = LabelEncoder()
X[:, 1] = label_encoder_sex_tr.fit_transform(X[:, 1])
 X[:, 5] = label_encoder_title_tr.fit_transform(X[:, 5])
 X[:, 4] = label_encoder_embarked_tr.fit_transform(X[:, 4])
train['item_price'].sort_values(ascending=False)[:10]<filter>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.17 )
train[train['item_price'] == 307980]<filter>
scaler_x = MinMaxScaler(( -1,1))
X_train = scaler_x.fit_transform(X_train)
 X_test = scaler_x.transform(X_test)
items[items['item_id'] == 6066]<filter>
accuracies = []
train = train[train['item_price'] < 300000]<sort_values>
classifier = LogisticRegression()
classifier.fit(X_train, y_train )
train['item_price'].sort_values() [:5]<filter>
lr_score = classifier.score(X_test, y_test)
 accuracies.append(lr_score)
 print(lr_score )
train[train['item_price'] == -1]<filter>
svm = SVC(kernel='linear')
 svm.fit(X_train, y_train )
items[items['item_id'] == 2973]<feature_engineering>
svm_score = svm.score(X_test, y_test)
 accuracies.append(svm_score)
 print(svm_score )
price_correction = train[(train['shop_id'] == 32)&(train['item_id'] == 2973)&(train['date_block_num'] == 4)&(train['item_price'] > 0)].item_price.median()
train.loc[train['item_price'] < 0, 'item_price'] = price_correction<sort_values>
k_svm = SVC(kernel='rbf')
 k_svm.fit(X_train, y_train )
train['item_price'].sort_values(ascending=False )<count_values>
k_svm_score = k_svm.score(X_test, y_test)
 accuracies.append(k_svm_score)
 print(k_svm_score )
test['item_id'].value_counts(ascending=False)[:5]<count_unique_values>
knn = KNeighborsClassifier(p=2, n_neighbors=10)
 knn.fit(X_train, y_train )
shop_train = train['shop_id'].nunique()
shop_test = test['shop_id'].nunique()<feature_engineering>
knn_score = knn.score(X_test, y_test)
 accuracies.append(knn_score)
 print(knn_score )
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'] == 10, 'shop_id'] = 11
test.loc[test['shop_id'] == 10, 'shop_id'] = 11<string_transform>
rdmf = RandomForestClassifier(n_estimators=20, criterion='entropy')
 rdmf.fit(X_train, y_train )
cities = shop['shop_name'].str.split(' ' ).map(lambda row: row[0] )<feature_engineering>
rdmf_score = rdmf.score(X_test, y_test)
 rdmf_score_tr = rdmf.score(X_train, y_train)
 accuracies.append(rdmf_score)
 print(rdmf_score)
 print(rdmf_score_tr )
shop['city'] = shop['shop_name'].str.split(' ' ).map(lambda row: row[0])
 shop.loc[shop.city == '!Якутск', 'city'] = 'Якутск'<categorify>
xgb = XGBClassifier()
xgb.fit(X_train, y_train )
pr = preprocessing.LabelEncoder()
pr.fit_transform(shop['city'] )<categorify>
xgb_score = xgb.score(X_test, y_test)
 accuracies.append(xgb_score)
 print(xgb_score )
shop['cities_label'] = pr.fit_transform(shop['city'])
 shop.drop(['shop_name', 'city'], axis = 1, inplace=True )<count_unique_values>
myLabels = ['Logistic Regression', 'SVM', 'Kernel SVM', 'KNN', 'Random Forest', 'Xgboost']
item_train = train['item_id'].nunique()
item_test = test['item_id'].nunique()<concatenate>
test_data['Title'] = test_data['Title'].replace('Dona.', 'Mrs.')
 test_data.head()
len(set(item_test_list ).difference(item_train_list))<filter>
titanic_test = test_data.iloc[:, 0:11].values
items.loc[~items['item_category_id'].isin(items_in_test)].T<categorify>
titanic_test[:, 1] = label_encoder_sex_tr.transform(titanic_test[:, 1])
 titanic_test[:, 5] = label_encoder_title_tr.transform(titanic_test[:, 5])
 titanic_test[:, 4] = label_encoder_embarked_tr.transform(titanic_test[:, 4] )
le = preprocessing.LabelEncoder()
main_items = item_cat['item_category_name'].str.split('-')
 item_cat['main_category_id'] = main_items.map(lambda row: row[0].strip())
 item_cat['main_category_id'] = le.fit_transform(item_cat['main_category_id'] )<categorify>
titanic_test = scaler_x.transform(titanic_test )
item_cat['sub_category_id'] = main_items.map(lambda row: row[1].strip() if len(row)> 1 else row[0].strip())
 item_cat['sub_category_id'] = le.fit_transform(item_cat['sub_category_id'] )<data_type_conversions>
y_pred = rdmf.predict(titanic_test )
train['date'] = pd.to_datetime(train['date'], format='%d.%m.%Y' )<import_modules>
titanic_submission = pd.DataFrame({'PassengerId':passengerId, 'Survived':y_pred} )
<concatenate><EOS> 
titanic_submission.to_csv('rdmf_Titanic.csv', index=False )