Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<drop_column>
time1 = time.time()
x_train_data = np.delete(x_train_data, 0, 0 )<concatenate>
warnings.filterwarnings('ignore' )
x_train_data = np.transpose(x_train_data)
 x_test_data = np.transpose(x_test_data )<normalization>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(np.concatenate(( x_train_data, x_test_data), axis=0))
x_train_data = scaler.transform(x_train_data)
 x_test_data = scaler.transform(x_test_data )<train_model>
data = pd.concat(( train, test),ignore_index=True)
 data = data.drop("SalePrice",1 )
epoch_train = 1000
mlp = MLP(layer_size=[x_train_data.shape[1], 28, 28, 1], regularization=1, output_shrink=0.1, output_range=[0, 1], loss_type="hardmse")
 mlp.train(x_train_data, p_train_data, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1 )<predict_on_test>
s1 = data.isnull().sum() [data.isnull().sum() != 0]
s2 = s1/data.shape[0]
pd.concat({'Missing Values': s1,
'Missing Ratio': s2},axis=1 ).sort_values(by = 'Missing Ratio', ascending = False )
predictions = np.round(mlp.predict(x_test_data ).reshape(-1))<prepare_output>
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'GarageCars',
'GarageArea', 'MasVnrArea', 'BsmtHalfBath'):
data[col] = data[col].fillna(0)
 data['LotFrontage'] = data['LotFrontage'].fillna(data['LotFrontage'].median())
 for col in('PoolQC', 'Fence', 'FireplaceQu','GarageType','GarageFinish', 'GarageQual',
'GarageCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'MSSubClass', 'MiscFeature'):
data[col] = data[col].fillna('None')
 for col in('Exterior1st', 'Exterior2nd', 'KitchenQual', 'Electrical', 'SaleType',
'MSZoning', 'GarageYrBlt'):
data[col] = data[col].fillna(data[col].mode() [0])
 data["Functional"] = data["Functional"].fillna("Typ")
 data = data.drop(['Id', 'Utilities'],1 )
predictions = np.round(mlp.predict(x_test_data ).reshape(-1))
ids = list(test_data['PassengerId'].values)
 submission = pd.DataFrame(list(np.transpose([ids, predictions])))
 submission.columns = ['PassengerId', 'Survived']<feature_engineering>
TotalSF = data['1stFlrSF'] + data['2ndFlrSF'] + data['TotalBsmtSF']
data.insert(loc=0,column="TotalSF", value=TotalSF )
submission['Survived'] = np.int32(np.round(submission['Survived']))
submission['PassengerId'] = np.int32(np.round(submission['PassengerId']))<save_to_csv>
y_log = np.log1p(Y )
submission.to_csv('titanic-submission-ultimate-nn-1.csv', index=False )<set_options>
skew = abs(data.skew() ).sort_values(ascending = False)
 skew_features = skew[skew > 1].index
data[skew_features] = np.log1p(data[skew_features] )
%matplotlib inline<import_modules>
data = pd.get_dummies(data )
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import keras<load_from_csv>
X_train = data[:train.shape[0]]
X_test = data[train.shape[0]:]
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
from sklearn.linear_model import Lasso, ElasticNet
from sklearn.kernel_ridge import KernelRidge
import xgboost as xgb
from sklearn.model_selection import KFold, cross_val_score, GridSearchCV
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
<feature_engineering>
model = Lasso()
grid = GridSearchCV(estimator=model, param_grid={'alpha': [100,10,1,0.1,0.01,0.001]})
 grid.fit(X_train, y_log)
 print('Lasso Regression parameters:')
 print('Alpha: ',grid.best_estimator_.alpha )
X_train = X_train / 255.0
test = test / 255.0<categorify>
model = ElasticNet()
grid = GridSearchCV(estimator=model, param_grid={'alpha': [100,10,1,0.1,0.01,0.001],
'l1_ratio': [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]})
 grid.fit(X_train, y_log)
 print('Elastic Net parameters:')
 print('Alpha: ',grid.best_estimator_.alpha)
 print('l1_ratio: ',grid.best_estimator_.l1_ratio )
Y_train = to_categorical(Y_train, num_classes = 10 )<split>
model = KernelRidge(kernel='polynomial')
 grid = GridSearchCV(estimator=model, param_grid={'alpha': [1000,10,1],
'coef0': [1000,10,1],
'degree': [2,3]})
 grid.fit(X_train, y_log)
 print('Kernel Ridge Regression parameters:')
 print('alpha: ',grid.best_estimator_.alpha)
 print('coef0: ',grid.best_estimator_.coef0)
 print('degree: ',grid.best_estimator_.degree )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1 )<choose_model_class>
model = xgb.XGBRegressor()
grid = GridSearchCV(estimator=model, param_grid={'n_estimators': [300,400,500,600,700],
'max_depth': [2,3],
'learning_rate': [0.05,0.1]})
 grid.fit(X_train, y_log)
 print('XGB Regressor parameters:')
 print('alpha: ',grid.best_estimator_.n_estimators)
 print('max_depth: ',grid.best_estimator_.max_depth)
 print('degree: ',grid.best_estimator_.learning_rate )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),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"))<choose_model_class>
def rmsle_cv(model):
kf = KFold(5, shuffle=True, random_state=42 ).get_n_splits(X_train.values)
 rmse= np.sqrt(-cross_val_score(model, X_train.values, y_log, scoring="neg_mean_squared_error", cv = kf))
return(rmse )
model.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
model_lasso = Lasso(alpha=0.001)
 score = rmsle_cv(model_lasso)
 print("Lasso Regression Score: {:.4f}".format(score.mean()))
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
model_enet = ElasticNet(alpha=0.001,l1_ratio=0.5)
 score = rmsle_cv(model_enet)
 print("ElasticNet Score: {:.4f}".format(score.mean()))
epochs = 40
batch_size = 80<init_hyperparams>
model_krr = KernelRidge(alpha=1000, coef0=1000, degree=2, kernel='polynomial')
 score = rmsle_cv(model_krr)
 print("Kernel Ridge Regressor Score: {:.4f}".format(score.mean()))
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)
<train_model>
model_xgb = xgb.XGBRegressor(n_estimators=700, max_depth=2, learning_rate=0.05)
 score = rmsle_cv(model_xgb)
 print("XGBoost Score: {:.4f}".format(score.mean()))
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<predict_on_test>
model_krr.fit(X_train,y_log)
 p_krr = np.expm1(model_krr.predict(X_test))
model_enet.fit(X_train,y_log)
 p_enet = np.expm1(model_enet.predict(X_test))
model_lasso.fit(X_train,y_log)
 p_lasso = np.expm1(model_lasso.predict(X_test))
model_xgb.fit(X_train,y_log)
 p_xgb = np.expm1(model_xgb.predict(X_test))
<save_to_csv><EOS> 
p_average =(p_lasso + p_xgb + p_krr + p_enet)/4
out = pd.Series(p_average,index=test.loc[:,'Id'])
 out.name = 'SalePrice'
out.to_csv('average.csv', header=True, index_label='Id' )