Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<compute_train_metric>
print('Compelete Imports' )
clf = MultinomialNB()
clf.fit(xtrain_ctv, ytrain)
 predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))<train_model>
file_path_train='.. /input/house-prices-advanced-regression-techniques/train.csv'
file_path_test='.. /input/house-prices-advanced-regression-techniques/test.csv'
dp_train = pd.read_csv(file_path_train, index_col='Id')
 dp_test=pd.read_csv(file_path_test,index_col='Id')
 dp_train.dropna(axis=0, subset=['SalePrice'], inplace=True)
 y = dp_train.SalePrice
X = dp_train.drop(['SalePrice'], axis=1)
 X_test=dp_test
print('Read Done!')
classifier = Pipeline([('vect', CountVectorizer()), 
('tfidf', TfidfTransformer()), 
('clf', MultinomialNB()),
])
 classifier.fit(xvalid, yvalid)
<define_search_space>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,random_state=0)
 print('Split Done!' )
parameters = {'vect__ngram_range': [(1, 1)],
'tfidf__use_idf': [True],
'clf__alpha':(0.05, 0.01)
 }

 gs_clf = GridSearchCV(classifier, parameters)
 gs_clf.fit(xtrain, ytrain )<find_best_params>
pd.concat([dp_train.isnull().sum() ,
100* dp_train.isnull().sum() /len(dp_train)],
axis=1 ).rename(columns={0:'Missing Records',1:'Percentage(%)'} )
gs_clf.best_params_<predict_on_test>
category_colums=[colum for colum in X.columns if X[colum].dtype == 'object']
len(category_colums )
gs_clf.refit
gs_clf.fit(xtrain, ytrain)
 y_pred_valid = gs_clf.predict_proba(xvalid)
 print("logloss: %0.3f " % multiclass_logloss(y_pred_valid, predictions))
y_pred_valid_f1 = gs_clf.predict(xvalid)
 print(classification_report(yvalid, y_pred_valid_f1))
y_pred_proba = gs_clf.predict_proba(X_Test)
 tdf = pd.DataFrame(y_pred_proba)
 tdf.columns = ['EAP', 'HPL', 'MWS']
submission = pd.read_csv('.. /input/spooky-author-identification/sample_submission.csv')
 result = pd.concat([submission[['id']], tdf], axis=1)
 result.head()
result.to_csv('naive_bayes__2.csv', index=False )<feature_engineering>
y.isna().sum()
embeddings_index = {}
f = open(glove_file)
 for line in tqdm(f):
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))<feature_engineering>
X_train.isna().sum()
def sent2vec(s):
words = str(s ).lower()
words = word_tokenize(words)
 words = [w for w in words if not w in stop_words]
words = [w for w in words if w.isalpha() ]
M = []
for w in words:
try:
M.append(embeddings_index[w])
 except:
continue
M = np.array(M)
 v = M.sum(axis=0)
 if type(v)!= np.ndarray:
return np.zeros(300)
 return v / np.sqrt(( v ** 2 ).sum() )<compute_train_metric>
cols_with_missing = [col for col in X_train.columns
if X_train[col].isnull().any() ]
len(cols_with_missing )
clf.fit(xtrain_ctv, ytrain)
 predictions = clf.predict_proba(xvalid_ctv)
 print("logloss: %0.3f " % multiclass_logloss(yvalid, predictions))<install_modules>
def score_dataset(X_train, X_valid, y_train, y_valid):
model = RandomForestRegressor(n_estimators=10, random_state=0)
 model.fit(X_train, y_train)
 preds = model.predict(X_valid)
 return mean_absolute_error(y_valid, preds )
!pip install tensorflow-gpu==1.14.0 --quiet
!pip install keras==2.2.4 --quiet<set_options>
X.drop(columns=bad_cols,axis=1,inplace=True)
 X_test.drop(columns=bad_cols,axis=1,inplace=True )
!pip install tta-wrapper --quiet
seed = 0
seed_everything(seed)
 warnings.filterwarnings("ignore" )<feature_engineering>
category_colums=[colum for colum in X.columns if X[colum].dtype == 'object']
len(category_colums )
train = pd.read_csv('.. /input/understanding_cloud_organization/train.csv')
 submission = pd.read_csv('.. /input/understanding_cloud_organization/sample_submission.csv')
 train['image'] = train['Image_Label'].apply(lambda x: x.split('_')[0])
 train['label'] = train['Image_Label'].apply(lambda x: x.split('_')[1])
 submission['image'] = submission['Image_Label'].apply(lambda x: x.split('_')[0])
 test = pd.DataFrame(submission['image'].unique() , columns=['image'])
 train_df = pd.pivot_table(train, index=['image'], values=['EncodedPixels'], columns=['label'], aggfunc=np.min ).reset_index()
train_df.columns = ['image', 'Fish_mask', 'Flower_mask', 'Gravel_mask', 'Sugar_mask']
print('Compete set samples:', len(train_df))
print('Test samples:', len(submission))
display(train.head() )<split>
my_imputer = SimpleImputer(strategy='most_frequent')
 imputed_X=pd.DataFrame(my_imputer.fit_transform(X))
imputed_X_test = pd.DataFrame(my_imputer.transform(X_test))
imputed_X.columns = X.columns
imputed_X_test.columns = X_test.columns
X_train, X_val = train_test_split(train_df, test_size=0.2, random_state=seed)
 X_train['set'] = 'train'
X_val['set'] = 'validation'
test['set'] = 'test'
print('Train samples: ', len(X_train))
print('Validation samples: ', len(X_val))<define_variables>
imputed_X_lable = X.copy()
imputed_X_test_lable = X_test.copy()
label_encoder = LabelEncoder()
for col in category_colums:
imputed_X_lable[col] = label_encoder.fit_transform(imputed_X[col])
 imputed_X_test_lable[col] = label_encoder.transform(imputed_X_test[col])
BACKBONE = 'resnet18'
BATCH_SIZE = 16
EPOCHS = 40
LEARNING_RATE = 1e-3
HEIGHT = 384
WIDTH = 480
CHANNELS = 3
N_CLASSES = 4
ES_PATIENCE = 5
RLROP_PATIENCE = 3
DECAY_DROP = 0.2
model_path = 'uNet_%s_%sx%s.h5' %(BACKBONE, HEIGHT, WIDTH )<create_dataframe>
parameters = [{
'n_estimators': list(range(100, 1001, 100)) ,
'learning_rate': [x / 100 for x in(range(5, 100, 10)) ],
'max_depth': list(range(6, 70, 6))
}]
parameters
train_generator = DataGenerator(
 directory=train_images_dest_path,
dataframe=X_train,
target_df=train,
batch_size=BATCH_SIZE,
target_size=(HEIGHT, WIDTH),
n_channels=CHANNELS,
n_classes=N_CLASSES,
preprocessing=preprocessing,
augmentation=augmentation,
seed=seed)
 valid_generator = DataGenerator(
 directory=validation_images_dest_path,
dataframe=X_val,
target_df=train,
batch_size=BATCH_SIZE,
target_size=(HEIGHT, WIDTH),
n_channels=CHANNELS,
n_classes=N_CLASSES,
preprocessing=preprocessing,
seed=seed )<choose_model_class>
gsearch = GridSearchCV(estimator=XGBRegressor() ,
param_grid = parameters,
scoring='neg_mean_absolute_error',
n_jobs=4,
cv=5,
verbose=7)
 gsearch.fit(imputed_X_lable,y)
 gsearch.best_params_.get('n_estimators'),gsearch.best_params_.get('learning_rate'),gsearch.best_params_.get('max_depth' )
model = sm.Unet(backbone_name=BACKBONE,
encoder_weights='imagenet',
classes=N_CLASSES,
activation='sigmoid',
input_shape=(HEIGHT, WIDTH, CHANNELS))
checkpoint = ModelCheckpoint(model_path, monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True)
 es = EarlyStopping(monitor='val_loss', mode='min', patience=ES_PATIENCE, restore_best_weights=True, verbose=1)
 rlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)
 metric_list = [dice_coef, sm.metrics.iou_score]
callback_list = [checkpoint, es, rlrop]
optimizer = RAdam(learning_rate=LEARNING_RATE, warmup_proportion=0.1)
 model.compile(optimizer=optimizer, loss=sm.losses.bce_dice_loss, metrics=metric_list)
 model.summary()<train_model>
best_n_estimators=gsearch.best_params_.get('n_estimators')
 best_learning_rate=gsearch.best_params_.get('learning_rate')
 best_max_depth=gsearch.best_params_.get('max_depth')
 best_n_estimators,best_learning_rate,best_max_depth
STEP_SIZE_TRAIN = len(X_train)//BATCH_SIZE
STEP_SIZE_VALID = len(X_val)//BATCH_SIZE
history = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
callbacks=callback_list,
epochs=EPOCHS,
verbose=2 ).history<define_search_space>
best_model=XGBRegressor(n_estimators=best_n_estimators,
learning_rate=best_learning_rate,
max_depth=best_max_depth)
 best_model.fit(imputed_X_lable,y )
class_names = ['Fish ', 'Flower', 'Gravel', 'Sugar ']
best_tresholds = [.5,.5,.5,.35]
best_masks = [25000, 20000, 22500, 15000]
for index, name in enumerate(class_names):
print('%s treshold=%.2f mask size=%d' %(name, best_tresholds[index], best_masks[index]))<compute_train_metric>
my_preds_test=best_model.predict(imputed_X_test_lable)
 my_preds_test
train_metrics = get_metrics(model, train, X_train, train_images_dest_path, best_tresholds, best_masks, seed=seed, preprocessing=preprocessing, set_name='Train')
 display(train_metrics)
 validation_metrics = get_metrics(model, train, X_val, validation_images_dest_path, best_tresholds, best_masks, seed=seed, preprocessing=preprocessing, set_name='Validation')
 display(validation_metrics )<normalization>
my_output=pd.DataFrame(
 {'Id':X_test.index,
'SalePrice':my_preds_test})
 my_output.to_csv('submission.csv',index=False)
 print('create output done!' )