Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<compute_test_metric>
%matplotlib inline
plt.style.use('fivethirtyeight')
 warnings.simplefilter('ignore')
 print('Setup complete' )
cv_score = np.mean(fold_scores)
 print(f'5-fold CV multi-class AUC-ROC: {cv_score}')
 print(f'Individual CV scores: {fold_scores}' )<load_from_csv>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train.head()
sample_submission = pd.read_csv(sample_path)
 sample_submission[['healthy', 'multiple_diseases', 'rust', 'scab']] = fold_preds
sample_submission.head(10 )<save_to_csv>
num_features = [col for col in train.columns if train[col].dtype in ['int64', 'float64']]
num_features.remove('Id')
 num_features.remove('SalePrice')
 num_analysis = train[num_features].copy()
for col in num_features:
if num_analysis[col].isnull().sum() > 0:
num_analysis[col] = SimpleImputer(strategy='median' ).fit_transform(num_analysis[col].values.reshape(-1,1))
clf = ExtraTreesRegressor(random_state=42)
 h = clf.fit(num_analysis, train.SalePrice)
 feature_imp = pd.DataFrame(sorted(zip(clf.feature_importances_,num_features)) , columns=['Value','Feature'])
 plt.figure(figsize=(16,10))
sns.barplot(x='Value', y='Feature', data=feature_imp.sort_values(by='Value', ascending=False))
plt.title('Most important numerical features with ExtraTreesRegressor')
 del clf, h;
sample_submission.to_csv('submission.csv',index=False )<install_modules>
cat_features = [col for col in train.columns if train[col].dtype =='object']
cat_analysis = train[cat_features].copy()
for col in cat_analysis:
if cat_analysis[col].isnull().sum() > 0:
cat_analysis[col] = SimpleImputer(strategy='constant' ).fit_transform(cat_analysis[col].values.reshape(-1,1))
target_enc = TargetEncoder(cols=cat_features)
 cat_analysis = target_enc.fit_transform(cat_analysis, train.SalePrice)
 clf = ExtraTreesRegressor(random_state=42)
 h = clf.fit(cat_analysis, train.SalePrice)
 feature_imp = pd.DataFrame(sorted(zip(clf.feature_importances_,cat_features)) , columns=['Value','Feature'])
 plt.figure(figsize=(16,10))
sns.barplot(x='Value', y='Feature', data=feature_imp.sort_values(by='Value', ascending=False))
plt.title('Most important categorical features with ExtraTreesRegressor')
 del clf, h;
!pip install -q efficientnet<set_options>
for col in ['FireplaceQu', 'Fence', 'Alley', 'MiscFeature', 'PoolQC']:
train[col].fillna('NotAvailable', inplace=True)
 test[col].fillna('NotAvailable', inplace=True)
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
train[col].fillna('NotAvailable', inplace=True)
 test[col].fillna('NotAvailable', inplace=True)
 garage_min = min(train['GarageYrBlt'].min() , test['GarageYrBlt'].min())-1
train['GarageYrBlt'].fillna(garage_min, inplace=True)
 test['GarageYrBlt'].fillna(garage_min, inplace=True)
 for col in ['GarageArea', 'GarageCars']:
train[col].fillna(0, inplace=True)
 test[col].fillna(0, inplace=True)
 for col in ['BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath']:
train[col].fillna(0, inplace=True)
 test[col].fillna(0, inplace=True)
 for col in ['BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']:
train[col].fillna('NotAvailable', inplace=True)
 test[col].fillna('NotAvailable', inplace=True)
 for col in ['MasVnrType', 'MSZoning', 'Functional', 'Electrical', 'KitchenQual', 'Exterior1st',
'Exterior2nd', 'SaleType','MSSubClass', 'Utilities', 'MasVnrArea']:
imputer = SimpleImputer(strategy='most_frequent')
 train[col] = imputer.fit_transform(train[col].values.reshape(-1,1))
test[col] = imputer.transform(test[col].values.reshape(-1,1))
imputer = KNNImputer(n_neighbors=5)
 train[['LotFrontage', 'LotArea']] = imputer.fit_transform(train[['LotFrontage', 'LotArea']])
 test[['LotFrontage', 'LotArea']] = imputer.transform(test[['LotFrontage', 'LotArea']])
 print('Imputation finished!')
 print('Number of missing values in the train set:', train.isnull().sum().sum())
 print('Number of missing values in the test set:', test.isnull().sum().sum() )
%matplotlib inline
warnings.filterwarnings('ignore' )<define_variables>
cat_features = [col for col in train.columns if train[col].dtype =='object']
cat_features = cat_features + ['MSSubClass','MoSold','YrSold']
num_features = [col for col in train.columns if train[col].dtype in ['int64', 'float64'] and col not in cat_features]
num_features.remove('Id')
 num_features.remove('SalePrice')
 target_enc = TargetEncoder(cols=cat_features)
 train_TE = train[num_features].join(target_enc.fit_transform(train[cat_features], train.SalePrice))
clf = ExtraTreesRegressor(random_state=0)
 h = clf.fit(train_TE, train.SalePrice)
 feature_imp = pd.DataFrame(sorted(zip(clf.feature_importances_,train_TE)) , columns=['Value','Feature'])
 plt.figure(figsize=(20,14))
sns.barplot(x='Value', y='Feature', data=feature_imp.sort_values(by='Value', ascending=False))
plt.title('Feature importance with ExtraTreesRegressor')
 del clf, h;
def seed_everything(seed=13):
np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ['TF_KERAS'] = '1'
random.seed(seed)
 seed_everything(42 )<choose_model_class>
cols_to_keep = list(feature_imp.sort_values(by='Value', ascending=False ).reset_index(drop=True ).loc[:59, 'Feature'])
 print('Keeping the first {:d} most informative features'.format(len(cols_to_keep)) )
def TPU() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 return strategy
strategy = TPU()<define_variables>
outliers_id = set(train.loc[train.GrLivArea > 4500, 'Id'])
 print('Outliers saved in outliers_id' )
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
AUTO = tf.data.experimental.AUTOTUNE<define_variables>
X_train = train.loc[~train.Id.isin(outliers_id), cols_to_keep].reset_index(drop=True)
 X_test = test[cols_to_keep]
cat_cols = [col for col in cols_to_keep if col in cat_features]
num_cols = [col for col in cols_to_keep if col not in cat_features]
lasso_preprocessor = ColumnTransformer(transformers=[ 
('num', PowerTransformer(standardize=True), num_cols), 
('cat', OneHotEncoder(handle_unknown='ignore'), cat_cols)
 ] )
IMG_SIZE_h = 512
IMG_SIZE_w = 512
FOLDS = 5
SEED = 42
EPOCHS = 50
BATCH_SIZE = 8*strategy.num_replicas_in_sync<load_from_csv>
oof_lasso = np.zeros(len(X_train))
preds_lasso = np.zeros(len(X_test))
out_preds_lasso = np.zeros(2)
 kf = KFold(n_splits=5, random_state=42, shuffle=True)
 print('Training {:d} Lasso models...
 '.format(kf.n_splits))
for i,(idxT, idxV)in enumerate(kf.split(X_train, y_train)) :
print('Fold', i)
 print(' rows of train =', len(idxT),'rows of holdout =', len(idxV))
train_prep = lasso_preprocessor.fit_transform(X_train.loc[idxT])
 valid_prep = lasso_preprocessor.transform(X_train.loc[idxV])
 out_prep = lasso_preprocessor.transform(train.loc[train.Id.isin(outliers_id), cols_to_keep])
 test_prep = lasso_preprocessor.transform(X_test)
 clf = Lasso(alpha=0.0005)
 h = clf.fit(train_prep, y_train.loc[idxT])
 oof_lasso[idxV] += clf.predict(valid_prep)
 out_preds_lasso += clf.predict(out_prep)/kf.n_splits
preds_lasso += clf.predict(test_prep)/kf.n_splits
del h, clf
print('
OOF CV:', mean_squared_error(y_train, oof_lasso, squared=False))
path='.. /input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(path+'train.csv')
 train_id = train['image_id']
train.pop('image_id')
 y_train = train.to_numpy().astype('float32')
 category_names = ['healthy','multiple_diseases','rust','scab']
root = 'images'
images_paths = [(os.path.join(GCS_DS_PATH,root,idee+'.jpg')) for idee in train_id]<load_from_csv>
lasso_submission = np.exp(preds_lasso)
 lasso_submission[1089] = train.loc[train.GrLivArea>4500, 'SalePrice'].mean()
DIR_INPUT = '/kaggle/input/plant-pathology-2020-fgvc7'
train_df = pd.read_csv(DIR_INPUT + '/train.csv')
 test_df = pd.read_csv(DIR_INPUT + '/test.csv')
 cols = list(train_df.columns[1:])
 train_paths = train_df['image_id'].apply(lambda x: os.path.join(GCS_DS_PATH , 'images' , x + '.jpg')).values
test_paths = test_df['image_id'].apply(lambda x: os.path.join(GCS_DS_PATH , 'images' , x + '.jpg')).values
train_labels = train_df.iloc[:,1:].values<count_values>
tree_preprocessor = ColumnTransformer(transformers=[ 
('num', 'passthrough', num_cols), 
('cat', TargetEncoder(cols=cat_cols), cat_cols)
 ] )
train_df.iloc[:,1:-1].sum(axis=1 ).value_counts()<compute_train_metric>
oof_LGBM = np.zeros(len(X_train))
preds_LGBM = np.zeros(len(X_test))
out_preds_LGBM = np.zeros(2)
 kf = KFold(n_splits=5, random_state=42, shuffle=True)
 print('Training {:d} LGBM models...
 '.format(kf.n_splits))
for i,(idxT, idxV)in enumerate(kf.split(X_train, y_train)) :
print('Fold', i)
 print(' rows of train =', len(idxT),'rows of holdout =', len(idxV))
train_prep = tree_preprocessor.fit_transform(X_train.loc[idxT], y_train.loc[idxT])
 valid_prep = tree_preprocessor.transform(X_train.loc[idxV])
 out_prep = tree_preprocessor.transform(train.loc[train.Id.isin(outliers_id), cols_to_keep])
 test_prep = tree_preprocessor.transform(X_test)
 clf = LGBMRegressor(n_estimators=5000, random_state=42, num_leaves=8, colsample_bytree=0.8)
 h = clf.fit(train_prep, y_train.loc[idxT],
eval_set=[(valid_prep, y_train.loc[idxV])],
verbose=300, early_stopping_rounds=200, eval_metric='rmse')
 oof_LGBM[idxV] += clf.predict(valid_prep)
 out_preds_LGBM += clf.predict(out_prep)/kf.n_splits
preds_LGBM += clf.predict(test_prep)/kf.n_splits
del h, clf
print('
OOF CV', mean_squared_error(y_train, oof_LGBM, squared=False))
def calculate_hash(im):
md5 = hashlib.md5()
md5.update(np.array(im ).tostring())
 return md5.hexdigest()
def get_image_meta(image_id, image_src, dataset='train'):
im = Image.open(image_src)
 extrema = im.getextrema()
meta = {
'image_id': image_id,
'dataset': dataset,
'hash': calculate_hash(im),
'r_min': extrema[0][0],
'r_max': extrema[0][1],
'g_min': extrema[1][0],
'g_max': extrema[1][1],
'b_min': extrema[2][0],
'b_max': extrema[2][1],
'height': im.size[0],
'width': im.size[1],
'format': im.format,
'mode': im.mode
}
return meta<categorify>
LGBM_submission = np.exp(preds_LGBM)
 LGBM_submission[1089] = train.loc[train.GrLivArea>4500, 'SalePrice'].mean()
<categorify><EOS> 
kaggle_sub = best_weight*lasso_submission +(1-best_weight)*LGBM_submission
output = pd.DataFrame({'Id': test.Id, 'SalePrice': kaggle_sub})
 output.to_csv('my_submission.csv', index=False)
 print('Your submission was successfully saved!' )