Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<concatenate>
warnings.filterwarnings("ignore")
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols<sort_values>
dtrain=pd.read_csv('.. /input/train.csv')
 dtest=pd.read_csv('.. /input/test.csv')
 print(dtrain.shape)
 print(dtest.shape )
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values<feature_engineering>
test_ID=dtest['Id']
dtrain=dtrain.drop(['Id'],axis=1)
 dtest=dtest.drop(['Id'],axis=1 )
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]<save_to_csv>
y_train=dtrain['SalePrice']
data=pd.concat([dtrain,dtest],ignore_index=True)
 data=data.drop(['SalePrice'],axis=1)
 data.shape
sample_submission.to_csv('submission.csv', index=False )<load_from_csv>
total = data.isnull().sum().sort_values(ascending=False)
 percent =(( data.isnull().sum() /data.isnull().count() ).sort_values(ascending=False)) *100
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20)
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<drop_column>
data['PoolQC']=data['PoolQC'].fillna("None")
 data['MiscFeature']=data['MiscFeature'].fillna("None")
 data['Alley']=data['Alley'].fillna("None")
 data['Fence']=data['Fence'].fillna("None")
 data['FireplaceQu']=data['FireplaceQu'].fillna("None")
 data["LotFrontage"] = data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
test = test / 255.0<train_model>
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
data[col] = data[col].fillna('None' )
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
data[col] = data[col].fillna(0 )
Y_train = keras.utils.to_categorical(Y_train, 10 )<split>
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
data[col] = data[col].fillna('None' )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1 )<define_variables>
data["MasVnrType"] = data["MasVnrType"].fillna("None")
 data["MasVnrArea"] = data["MasVnrArea"].fillna(0 )
batch_size = 86
num_classes = 10
epochs = 10
input_shape =(28, 28, 1 )<choose_model_class>
data=data.drop(['GarageCars','1stFlrSF','2ndFlrSF','TotRmsAbvGrd','GarageYrBlt'],axis=1 )
batch_size = 86
num_classes = 10
epochs = 10
input_shape =(28, 28, 1)
 model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop() ,
metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001 )<init_hyperparams>
num_f=data.dtypes[data.dtypes!=object].index
skew_f=data[num_f].apply(lambda x: skew(x.dropna()))
skew_f=skew_f[skew_f>0.75]
skew_f=skew_f.index
data[skew_f]=np.log1p(data[skew_f] )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<load_from_csv>
data = pd.get_dummies(data)
 data.shape
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv-train/mnist_train.csv" )<load_from_csv>
x_train=data[:dtrain.shape[0]]
test=data[dtrain.shape[0]:]
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )<feature_engineering>
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
import xgboost as xgb
test['dataset'] = 'test'<feature_engineering>
dtrain=xgb.DMatrix(x_train,label=y_train)
 dtest=xgb.DMatrix(test )
train['dataset'] = 'train'<concatenate>
def_params={'min_child_weight': 1,
'max_depth': 6,
'subsample': 1.0,
'colsample_bytree': 1.0,
'reg_lambda': 1,
'reg_alpha': 0,
'learning-rate':0.3,
'silent':1
}
cv_res=xgb.cv(def_params,dtrain,nfold=5)
 cv_res.tail()
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()<concatenate>
warnings.filterwarnings('ignore', message='The objective has been evaluated at this point before.')
 params={'min_child_weight':(0, 50,),
'max_depth':(0, 10),
'subsample':(0.5, 1.0),
'colsample_bytree':(0.5, 1.0),
'reg_lambda':(1e-5,100,'log-uniform'),
'reg_alpha':(1e-5,100,'log-uniform'),
'learning-rate':(0.01,0.2,'log-uniform')
 }
bayes=BayesSearchCV(xgb.XGBRegressor() ,params,n_iter=10,scoring='neg_mean_squared_error',cv=5,random_state=42)
 res=bayes.fit(x_train,y_train)
 print(res.best_params_ )
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols<sort_values>
final_params={'colsample_bytree': 0.50, 'max_depth': 7, 'min_child_weight':13, 'reg_alpha': 0.112, 'reg_lambda': 0.0008, 'subsample': 0.65,'eta':0.11,'silent':1}
cv_res=xgb.cv(final_params,dtrain,num_boost_round=1000,early_stopping_rounds=100,nfold=5 )
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values<feature_engineering>
model_xgb=xgb.train(final_params,dtrain=dtrain,num_boost_round=151)
 final_pred=np.expm1(model_xgb.predict(dtest))
<save_to_csv><EOS> 
s=pd.DataFrame()
s['Id']=test_ID
s['SalePrice']=final_pred
s.to_csv('my_submission.csv',index=False )