Kaggle competition: Задержка рейса самолета
<SOS> metric: RMSE Kaggle data source: departure-delayed<find_best_model_class>
warnings.filterwarnings('ignore')
 sns.set(style="white")
 %matplotlib inline
%matplotlib inline
y_val_gr=[]
for el in y_validation:
if el=='moroccan':
y_val_gr.append(1)
 else:
y_val_gr.append(0)
 y_train_gr=[]
for el in y_train:
if el=='moroccan':
y_train_gr.append(1)
 else:
y_train_gr.append(0)
 ridge_gr=RidgeClassifier().fit(X_train,y_train_gr ).predict(X_validation)
 print(metrics.classification_report(y_val_gr, ridge_gr))<define_variables>
def rmse(y_test, y_pred):
return np.sqrt(((y_test - y_pred)** 2 ).mean())
 def rmse_cv(estimator, X_test, y_test):

 return - np.sqrt(((estimator.predict(X_test)- y_test)** 2 ).mean())
 def rmse_log(estimator, X_test, y_test):

 return - np.sqrt(((np.exp(estimator.predict(X_test)) - np.exp(y_test)) ** 2 ).mean())
 def submit_file(y_pred, filename):
pd.Series(y_pred, name='DelayTime' ).to_csv('{}.csv'.format(filename),
index_label='id', header=True)
 def hacking_score(pred, y_train):
zeros_submit_score = 68.79140
return pred + zeros_submit_score - np.sqrt(np.square(y_train.values ).mean() )
final_res=[]
for i,j,ai,k,l,m in zip(ridge_ch,ridge_vt,ridge_gr,onevsall,logreg,sdg):
if i==1 and j==0 and ai==0:
if k=='southern_us' or k=='indian':
res='russian'
elif j==1 and i==0 and ai==0:
if k=='italian' or k=='vietnamese' or k=='british':
res='thai'
elif j==0 and i==0 and ai==1:
if k=='southern_us' or k=='indian' or k=='filipino' or k=='cajun_creole':
res='moroccan'
else:
res=k
final_res.append(res)
 print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, final_res))
print(metrics.classification_report(y_validation, final_res, target_names=set(list(y_validation))))<save_to_csv>
train_df = pd.read_csv('.. /input/train_features.csv')
 test_df = pd.read_csv('.. /input/test_features.csv')
 target = pd.read_csv('.. /input/train_target.csv', index_col='id')
 train_df.head()
<load_from_disk>
train_df['DepHour'] = train_df['CRSDepTime'] // 100
train_df['DepHour'].replace(to_replace=24, value=0, inplace=True)
 train_df['DepTime'] = train_df['DepHour'] + train_df['CRSDepTime'] % 100 / 60
test_df['DepHour'] = test_df['CRSDepTime'] // 100
test_df['DepHour'].replace(to_replace=24, value=0, inplace=True)
 test_df['DepTime'] = test_df['DepHour'] + test_df['CRSDepTime'] % 100 / 60
train_df['ArrHour'] = train_df['CRSArrTime'] // 100
train_df['ArrHour'].replace(to_replace=24, value=0, inplace=True)
 train_df['ArrTime'] = train_df['ArrHour'] + train_df['CRSArrTime'] % 100 / 60
test_df['ArrHour'] = test_df['CRSArrTime'] // 100
test_df['ArrHour'].replace(to_replace=24, value=0, inplace=True)
 test_df['ArrTime'] = test_df['ArrHour'] + test_df['CRSArrTime'] % 100 / 60
test_df.drop(['CRSDepTime', 'CRSArrTime', 'Year', ], axis=1, inplace=True)
 train_df.drop(['CRSDepTime', 'CRSArrTime', 'Year', ], axis=1, inplace=True)
 train_df['target'] = target
with open('.. /input/train.json')as json_file:
train_data = json.load(json_file)
 with open('.. /input/test.json')as json_file:
test_data = json.load(json_file)
 cuisines = []
for i in range(0, len(train_data)) :
cuisines.append(train_data[i]['cuisine'])
 cuisines = list(set(cuisines))
ingredients_train = []
for i in range(0, len(train_data)) :
for j in range(0, len(train_data[i]['ingredients'])) :
ingredients_train.append(train_data[i]['ingredients'][j])
 ingredients_train = list(set(ingredients_train))
ingredients = ingredients_train
for i in range(0, len(train_data)) :
train_data[i]['cuisine'] = list.index(cuisines, train_data[i]['cuisine'])+ 1
for j in range(0, len(train_data[i]['ingredients'])) :
train_data[i]['ingredients'][j] = list.index(ingredients, train_data[i]['ingredients'][j])+ 1<define_variables>
test_df.drop(['CRSElapsedTime', ], axis=1, inplace=True)
 train_df.drop(['CRSElapsedTime', ], axis=1, inplace=True )
train_features = []
train_targets = []
for i in range(0, len(train_data)) :
train_features.append(train_data[i]['ingredients'])
 train_targets.append([train_data[i]['cuisine']])
 oneHotFeatures = np.zeros(( len(train_data), len(ingredients)))
 for i in range(0, len(train_data)) :
for j in range(0, len(ingredients)) :
if j + 1 in train_data[i]['ingredients']:
oneHotFeatures[i][j] = 1
else:
oneHotFeatures[i][j] = 0
oneHotTargets = np.zeros(( len(train_data), len(cuisines)))
 for i in range(0, len(train_data)) :
for j in range(0, len(cuisines)) :
if j + 1 == train_data[i]['cuisine']:
oneHotTargets[i][j] = 1
else:
oneHotTargets[i][j] = 0
oneHotFeatures = np.int32(oneHotFeatures)
 train_features = oneHotFeatures
oneHotTargets = np.int32(oneHotTargets)
 train_targets = oneHotTargets<choose_model_class>
train_df.fillna('EVIL', inplace=True)
 test_df.fillna('EVIL', inplace=True )
model = Sequential()
model.add(Dense(200, activation='sigmoid'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(400, activation='sigmoid'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(300, activation='sigmoid'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(20, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])
 model.fit(train_features[0:36819], train_targets[0:36819], epochs=10 )<feature_engineering>
test_features = []
for i in range(0, len(test_data)) :
test_features.append(test_data[i]['ingredients'])
 oneHotFeatures = np.zeros(( len(test_data), len(ingredients)))
 for i in range(0, len(test_data)) :
for j in range(0, len(ingredients)) :
if ingredients[j] in test_data[i]['ingredients']:
oneHotFeatures[i][j] = 1
else:
oneHotFeatures[i][j] = 0
oneHotFeatures = np.int32(oneHotFeatures)
 test_features = oneHotFeatures<save_to_csv>
OHE = OneHotEncoder(handle_unknown='ignore')
 ohe_x_train = OHE.fit_transform(train_df.iloc[:, [0,1,2,3,4,5,6,7,9,11]])
 num_x_train = train_df.iloc[:, [8, 10, 12]]
ohe_x_test = OHE.transform(test_df.iloc[:, [0,1,2,3,4,5,6,7,9,11]])
 num_x_test = test_df.iloc[:, [8, 10, 12]]
X = hstack([ohe_x_train, num_x_train] ).tocsr()
X_test = hstack([ohe_x_test, num_x_test] ).tocsr()
y = train_df.target.values
predictions = model.predict(test_features)
 cuisinePredictions = []
for i in range(0, len(predictions)) :
cuisinePredictions.append(cuisines[list.index(list(predictions[i]), max(predictions[i])) ])
 ids = np.int32(np.array([test_data[i]['id'] for i in range(0, len(test_data)) ]))
submission = pd.DataFrame(ids, columns={"id"})
 submission['cuisine'] = cuisinePredictions
submission = submission[["id", "cuisine"]]
submission.to_csv('cuisine-prediction-submission-6.csv', index=False )<save_to_csv>
def CB_cv_scoring(X_train, y_train, X_test, nfolds=5):
predict = np.zeros(X_test.shape[0])
 oof = np.zeros(X_train.shape[0])
 folds = StratifiedKFold(n_splits=nfolds, shuffle=True, random_state=870)
 for fold_,(ind_trn, ind_valid)in enumerate(folds.split(X_train, y_train)) :
print("Fold {}".format(fold_))
clf = CatBoostRegressor(
 random_seed=42,
logging_level='Silent',
early_stopping_rounds=200, 
)
 categorical_features_indices = [0,1,2,3,4,5,6,7,9,11]
clf.fit(
 X_train.iloc[ind_trn], y_train.iloc[ind_trn],
cat_features=categorical_features_indices,
eval_set=(X_train.iloc[ind_valid], y_train.iloc[ind_valid]), 
);
oof[ind_valid] = clf.predict(X_train.iloc[ind_valid],)
 pred = clf.predict(X_test)/ nfolds
predict += pred
print("CV score: {:<8.5f}".format(rmse(y_train, oof)))
 return np.array(predict )
predictions = model.predict(test_features)
 cuisinePredictions = []
for i in range(0, len(predictions)) :
cuisinePredictions.append(cuisines[list.index(list(predictions[i]), max(predictions[i])) ])
 ids = np.int32(np.array([test_data[i]['id'] for i in range(0, len(test_data)) ]))
submission = pd.DataFrame(ids, columns={"id"})
 submission['cuisine'] = cuisinePredictions
submission = submission[["id", "cuisine"]]
submission.to_csv('cuisine-prediction-submission-0.csv', index=False )<load_from_disk>
lgb_pred = LGB_cv_scoring(X, y, X_test)
 submit_file(lgb_pred, 'lgb_pred' )
train_df = pd.read_json(".. /input/train.json")
 test_df = pd.read_json(".. /input/test.json" )<prepare_x_and_y>
cat_pred = CB_cv_scoring(train_df.drop('target', axis=1), train_df.target, test_df)
 submit_file(cat_pred, 'cat_pred' )
Y_train = train_df["cuisine"].apply(lambda x: x.lower())
 X_train = train_df["ingredients"].apply(lambda x: ' '.join(lemmatizer.lemmatize(unidecode.unidecode(i)) for i in x ).strip().lower())
 test_data = test_df["ingredients"].apply(lambda x: ' '.join(lemmatizer.lemmatize(unidecode.unidecode(i)) for i in x ).strip().lower())
 k = Y_train.nunique()<categorify>
mix_pred =(lgb_pred + cat_pred)/ 2
submit_file(mix_pred, 'mix_pred' )
<feature_engineering><EOS> 
hack_mix_pred = hacking_score(mix_pred, y)
 submit_file(hack_mix_pred, 'hack_mix_pred' )