Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<data_type_conversions>
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
cor_impcol = x.columns[xgb.feature_importances_.astype(bool)]
x = x[x.columns[xgb.feature_importances_.astype(bool)]]
cor_impcol<import_modules>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout<split>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
trainx, testx, trainy, testy = train_test_split(corx[cor_impcol], cory, test_size=0.1, random_state=4355)
 inpshape = trainx.shape
inpshape<choose_model_class>
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
model = Sequential([
Dense(10, activation='relu', input_shape=inpshape),
Dense(13, activation='relu'),
Dropout(0.3),
Dense(17, activation='relu'),
Dropout(0.3),
Dense(13, activation='relu'),
Dense(10, activation='relu'),
Dropout(0.3),
Dense(1, activation='sigmoid'),
])
 model.summary()
model.compile(metrics=['accuracy'], loss=['binary_crossentropy'] )<train_model>
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )
history = model.fit(trainx, trainy, batch_size=32, epochs=40, validation_split=0.1 )<compute_test_metric>
all_data = pd.get_dummies(all_data )
model.evaluate(testx, testy )<import_modules>
all_data = all_data.fillna(all_data.mean() )
import pandas as pd
import numpy as np<import_modules>
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice
import pandas as pd
import numpy as np<load_from_csv>
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse )
test_data = Test(pd.read_csv('.. /input/titanic/test.csv'))
test_data = test_data.get_data()<feature_engineering>
model_ridge = Ridge()
for i in impx.columns:
if i not in test_data.columns:
print(i)
<drop_column>
alphas = [0.05, 0.1, 0.3, 1, 3, 5, 10, 15, 30, 50, 75]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean()
for alpha in alphas]
for i in test_data.columns:
if i not in impx.columns:
print(i)
<sort_values>
model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y )
test_data = test_data[sorted(test_data)]<train_model>
rmse_cv(model_lasso ).mean()
Fmodel = xgboost(n_estimators=8, max_depth=3, random_state=345)
 Fmodel.fit(impx, impy)
 print(Fmodel.score(impx, impy))<compute_test_metric>
print("Lasso picked " + str(sum(coef != 0)) + " variables and eliminated the other " + str(sum(coef == 0)) + " variables" )
predS(Fmodel, test_data[impx.columns])
<import_modules>
imp_coef = pd.concat([coef.sort_values().head(10),
coef.sort_values().tail(10)] )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
import re
import xgboost as xgb
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier 
<load_from_csv>
import xgboost as xgb
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 print("Train set size:", train.shape)
 print("Test set size:", test.shape )<drop_column>
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=100 )
test_ID = test['PassengerId']
train=train.drop(columns=['PassengerId',"Ticket","Cabin"])
 test=test.drop(columns=["PassengerId","Ticket","Cabin"] )<feature_engineering>
model_xgb = xgb.XGBRegressor(n_estimators=360, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )
train['FamilySize'] = train['SibSp'] + train['Parch'] + 1
test['FamilySize'] = test['SibSp'] + test['Parch'] + 1 
<drop_column>
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))
train=train.drop(columns=["SibSp","Parch"])
 test=test.drop(columns=["SibSp","Parch"] )<feature_engineering>
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )
for dataset in [train,test]:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1<categorify>
preds = 0.7*lasso_preds + 0.3*xgb_preds
train['Embarked'] = train['Embarked'].fillna('S')
 train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test['Embarked'] = test['Embarked'].fillna('S')
 test['Embarked'] = test['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train['Sex'] = train['Sex'].map({'male': 0, 'female': 1})
 test['Sex'] = test['Sex'].map({'male': 0, 'female': 1})
<feature_engineering>
solution = pd.DataFrame({"id":test.Id, "SalePrice":preds})
 solution.to_csv("ridge_sol.csv", index = False )
def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in [train,test]:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in [train,test]:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
<drop_column>
from keras.layers import Dense
from keras.models import Sequential
from keras.regularizers import l1
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
train=train.drop(columns=["Name"])
 test=test.drop(columns=["Name"] )<prepare_x_and_y>
X_train = StandardScaler().fit_transform(X_train )
y=train.Survived
train=train.drop(columns=["Survived"])
 all_data = pd.concat(( train,test))<data_type_conversions>
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y, random_state = 3 )
all_data = all_data.fillna(all_data.mean() )<features_selection>
model = Sequential()
model.add(Dense(1, input_dim = X_train.shape[1], W_regularizer=l1(0.001)))
 model.compile(loss = "mse", optimizer = "adam" )
<split><EOS> 
hist = model.fit(X_tr, y_tr, validation_data =(X_val, y_val))