Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<import_modules>
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from xgboost import XGBRegressor
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import numpy as np
import pandas as pd
import os 
<set_options>
train_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 full_data = pd.concat([train_data,test_data] ).reset_index(drop = True)
 sale_price = train_data['SalePrice'].reset_index(drop=True)
 del full_data['SalePrice']
print(f'Train dataframe contains {train_data.shape[0]} rows and {train_data.shape[1]} columns.
 ')
 print(f'Test dataframe contains {test_data.shape[0]} rows and {test_data.shape[1]} columns.
 ')
 print(f'The merged dataframe contains {full_data.shape[0]} rows and {full_data.shape[1]} columns.' )
%matplotlib inline<load_from_csv>
cols_to_drop = []
for column in full_data:
if full_data[column].isnull().sum() / len(full_data)>= 0.4:
cols_to_drop.append(column)
 full_data.drop(cols_to_drop, axis=1, inplace=True)
 print(f'{len(cols_to_drop)} columns dropped, the full dataset now comprises of {full_data.shape[1]} variables.' )
data_dir = '.. /input/plant-seedlings-classification/'
train_dir = os.path.join(data_dir, 'train')
 test_dir = os.path.join(data_dir, 'test')
 sample_submission = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))<define_variables>
scaler = MinMaxScaler()
columns = full_data.columns.values
for column in columns:
if full_data[column].dtype == np.int64 or full_data[column].dtype == np.float64:
full_data[column] = full_data[column].fillna(full_data[column].median())
 full_data[column] = scaler.fit_transform(np.array(full_data[column] ).reshape(-1,1))
full_data.head()
train_dir = pathlib.Path(train_dir)
 image_count = len(list(train_dir.glob('*/*.png')))
 print(image_count )<define_variables>
count = 0
columns = full_data.columns.values
for column in columns:
if full_data[column].dtype not in(np.int64, np.float64)and full_data[column].nunique() > 6:
count += 1
full_data.drop(column, axis = 1, inplace = True)
 print(f'{count} columns dropped, the full dataset now comprises of {full_data.shape[1]} variables.' )
batch_size = 32
img_height = 260
img_width = 260<create_dataframe>
full_data = full_data.fillna(full_data.mode().iloc[0] )
train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
seed = 7,
image_size =(img_height, img_width),
batch_size = batch_size)
<define_variables>
labelencoder = LabelEncoder()
cols_to_drop = []
columns = full_data.columns.values
for column in columns:
if full_data[column].dtype not in(np.int64, np.float64)and full_data[column].nunique() > 2:
dummies = pd.get_dummies(full_data[column], prefix = str(column))
cols_to_drop.append(column)
 full_data = pd.concat([full_data, dummies], axis = 1)
 elif full_data[column].dtype not in(np.int64, np.float64)and full_data[column].nunique() < 3:
full_data[column] = labelencoder.fit_transform(full_data[column])
 cols_to_drop.append(column)
 full_data.drop(cols_to_drop, axis = 1, inplace = True)
 print(f'The new dataframe comprises of {test_data.shape[0]} rows and {test_data.shape[1]} columns.
 ' )
class_names = train_ds.class_names
num_classes = len(class_names)
 print(class_names)
 print(num_classes)
<choose_model_class>
train_df = full_data[:train_data.shape[0]]
test_df = full_data[train_data.shape[0]:]
img_augmentation = Sequential(
 [
preprocessing.RandomRotation(factor=0.15),
preprocessing.RandomFlip() ,
preprocessing.RandomContrast(factor=0.1),
],
name="img_augmentation",
)<categorify>
tf.random.set_seed(42)
 model = tf.keras.Sequential([
tf.keras.layers.Dense(1000),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(100),
tf.keras.layers.Dense(100),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(1)
 ])
 model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0003),
metrics=['mae'])
 history = model.fit(train_df,sale_price, epochs=400, verbose=0)
 pd.DataFrame(history.history ).plot()
plt.ylabel("loss")
 plt.xlabel("epochs")
 plt.show()
DNN_predictions = model.predict(test_df)
 DNN_predictions = tf.squeeze(DNN_predictions, axis = 1)
 DNN_predictions = np.array(DNN_predictions)
 DNN_predictions
def input_preprocess(image, label):
label = tf.one_hot(label, 12)
 return image, label<categorify>
xgboost = XGBRegressor(learning_rate=0.008,
n_estimators=6000,
max_depth=8,
min_child_weight=0,
gamma=0.6,
subsample=0.7,
colsample_bytree=0.7,
objective='reg:squarederror',
nthread=-1,
scale_pos_weight=1,
seed=27,
reg_alpha=0.00006,
random_state=42)
 xgb = xgboost.fit(train_df,sale_price)
 training_accuracy = xgb.score(train_df,sale_price)
 print("Training accuracy: %.2f%%" %(training_accuracy * 100.0))
xgb_predictions = xgb.predict(test_df )
<choose_model_class><EOS> 
predictions = [(xgb_pred + DNN_pred)/ 2 for xgb_pred, DNN_pred in zip(xgb_predictions, DNN_predictions)]
submission = pd.DataFrame({'ID':test_data['Id'],'SalePrice':predictions})
 submission.to_csv('submission.csv',index = False)
 submission