Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<predict_on_test>
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
plt.style.use('ggplot')
 warnings.filterwarnings('ignore')
 plt.rc('font', size=18)
 plt.rc('axes', titlesize=22)
 plt.rc('axes', labelsize=18)
 plt.rc('xtick', labelsize=12)
 plt.rc('ytick', labelsize=12)
 plt.rc('legend', fontsize=12)
 plt.rcParams['font.sans-serif'] = ['Verdana']
def format_1000(value, tick_number):
return int(value / 1_000)
 pd.options.mode.chained_assignment = None
pd.options.display.max_seq_items = 500
pd.options.display.max_rows = 500
pd.set_option('display.float_format', lambda x: '%.5f' % x)
 BASE_PATH = "/kaggle/input/house-prices-advanced-regression-techniques/"
val_preds = BiLSTM.predict(X_test)
 val_preds = np.round(val_preds ).astype(int)
 print(classification_report(y_test,val_preds,target_names = ['Not Relevant', 'Relevant']))<choose_model_class>
df = pd.read_csv(f"{BASE_PATH}train.csv")
 df_test = pd.read_csv(f"{BASE_PATH}test.csv")
 df.head()
input_text = Input(shape=(60,),dtype='int64')
 embedding_layer = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix],
trainable=False, mask_zero=True )(input_text)
 text_embed = SpatialDropout1D(0.4 )(embedding_layer)
 conv_layer = Conv1D(300, kernel_size=3, padding="valid", activation='relu' )(text_embed)
 conv_max_pool = MaxPooling1D(pool_size=2 )(conv_layer)
 gru_layer = Bidirectional(GRU(300, return_sequences=True))(conv_max_pool)
 sentence_embed = GlobalMaxPooling1D()(gru_layer)
 dense_layer = Dense(256, activation='relu' )(sentence_embed)
 output = Dense(1, activation='sigmoid' )(dense_layer)
 CNNRNN = Model(input_text, output)
 CNNRNN.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(6e-6))
lr_callback = CyclicLR()
history = CNNRNN.fit(X_train,y_train, batch_size=5, epochs=3,
validation_data=(X_test,y_test), callbacks=[lr_callback] )<predict_on_test>
missing = [(c, df[c].isna().mean() *100)for c in df]
missing = pd.DataFrame(missing, columns=["column_name", "percentage"])
 missing = missing[missing.percentage > 0]
display(missing.sort_values("percentage", ascending=False))
val_preds = CNNRNN.predict(X_test)
 val_preds = np.round(val_preds ).astype(int)
 print(classification_report(y_test,val_preds,target_names = ['Not Relevant', 'Relevant']))<choose_model_class>
df[df.SalePrice > upper]
input_text = Input(shape=(60,),dtype='int64')
 embedding_layer = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix],
trainable=False, mask_zero=True )(input_text)
 text_embed = SpatialDropout1D(0.4 )(embedding_layer)
 gru_layer = Bidirectional(GRU(300, return_sequences=True))(text_embed)
 conv_layer = Conv1D(64, kernel_size=2, padding="valid", kernel_initializer="he_uniform" )(gru_layer)
 avg_pool = GlobalAveragePooling1D()(conv_layer)
 max_pool = GlobalMaxPooling1D()(conv_layer)
 sentence_embed = concatenate([avg_pool, max_pool])
 dense_layer = Dense(256, activation='relu' )(sentence_embed)
 output = Dense(1, activation='sigmoid' )(dense_layer)
 RNNCNN = Model(input_text, output)
 RNNCNN.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(6e-6))
lr_callback = CyclicLR()
history = RNNCNN.fit(X_train,y_train, batch_size=5, epochs=3,
validation_data=(X_test,y_test), callbacks=[lr_callback] )<predict_on_test>
display(df.select_dtypes("number" ).skew().sort_values(ascending=False))
val_preds = RNNCNN.predict(X_test)
 val_preds = np.round(val_preds ).astype(int)
 print(classification_report(y_test,val_preds,target_names = ['Not Relevant', 'Relevant']))<split>
df = pd.read_csv(f"{BASE_PATH}train.csv")
 df_test = pd.read_csv(f"{BASE_PATH}test.csv")
 feat = pd.concat([df, df_test] ).reset_index(drop=True ).copy()
train,_, y_train, _ = train_test_split(data['text'].values,data['target'].values,
test_size=0.2,shuffle=True,random_state=124, stratify=data['target'] )<string_transform>
missing = feat.columns[feat.isna().any() ]
print(missing )
MAX_SEQUENCE_LENGTH = 60
MAX_NB_WORDS = 30000
EMBEDDING_DIM = 300
sequences = tokenizer.texts_to_sequences(train)
 word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
pad_text = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH )<predict_on_test>
feat.Alley = feat.Alley.fillna("None")
 bsmt_cols = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual']
for col in bsmt_cols:
feat[col] = feat[col].fillna("None")
 for col in ['BsmtFullBath', 'BsmtHalfBath', 'BsmtUnfSF', 'BsmtFinSF1', 'BsmtFinSF2', 'TotalBsmtSF']:
feat[col] = feat[col].fillna(0 ).astype(int)
 feat.Electrical = feat.Electrical.fillna("SBrkr")
 feat.Exterior1st = feat.Exterior1st.fillna("VinylSd")
 feat.Exterior2nd = feat.Exterior2nd.fillna("None")
 feat.Fence = feat.Fence.fillna("None")
 feat.FireplaceQu = feat.FireplaceQu.fillna("None")
 feat.Functional = feat.Functional.fillna("Typ")
 grg_cols = ['GarageCond', 'GarageFinish', 'GarageQual', 'GarageType']
for col in grg_cols:
feat[col] = feat[col].fillna("None")
 for col in ['GarageArea', 'GarageCars', 'GarageYrBlt']:
feat[col] = feat[col].fillna(0 ).astype(int)
 to_fix = feat[feat.GarageYrBlt == 2207].index
feat.loc[to_fix, "GarageYrBlt"] = int(feat.GarageYrBlt.mean())
 feat.KitchenQual = feat.KitchenQual.fillna("TA")
 feat.LotFrontage = feat.LotFrontage.fillna(0 ).astype(int)
 feat.MSZoning = feat.MSZoning.fillna("RL")
 feat.MSSubClass = feat.MSSubClass.astype(str)
 feat.MasVnrType = feat.MasVnrType.fillna("None")
 feat.MasVnrArea = feat.MasVnrArea.fillna(0 ).astype(int)
 feat.MiscFeature = feat.MiscFeature.fillna("None")
 feat.PoolQC = feat.PoolQC.fillna("None")
 feat.SaleType = feat.SaleType.fillna("WD")
 feat.Utilities = feat.Utilities.fillna("AllPub" )
bilstm=BiLSTM.predict(pad_text)
 cr = CNNRNN.predict(pad_text)
 rc = RNNCNN.predict(pad_text )<create_dataframe>
feat = feat.replace({ "Alley": {"None" : 0, "Grvl" : 1, "Pave" : 2},
"BsmtCond": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"BsmtExposure": {"None" : 0, "No" : 2, "Mn" : 2, "Av": 3,
"Gd" : 4},
"BsmtFinType1": {"None" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3,
"BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtFinType2": {"None" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3,
"BLQ" : 4,
"ALQ" : 5, "GLQ" : 6},
"BsmtQual": {"None" : 0, "Po" : 1, "Fa" : 2, "TA": 3,
"Gd" : 4, "Ex" : 5},
"CentralAir": {"None" : 0, "N" : 1, "Y" : 2},
"ExterCond": {"None" : 0, "Po" : 1, "Fa" : 2, "TA": 3,
"Gd": 4, "Ex" : 5},
"ExterQual": {"None" : 0, "Po" : 1, "Fa" : 2, "TA": 3,
"Gd": 4, "Ex" : 5},
"Fence": {"None" : 0, "MnWw" : 1, "GdWo" : 2, "MnPrv": 3,
"GdPrv" : 4},
"FireplaceQu": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"Functional": {"None" : 0, "Sal" : 1, "Sev" : 2, "Maj2" : 3,
"Maj1" : 4, "Mod": 5, "Min2" : 6, "Min1" : 7,
"Typ" : 8},
"GarageCond": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"GarageQual": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"GarageFinish": {"None" : 0, "Unf" : 1, "RFn" : 2, "Fin" : 3},
"HeatingQC": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"KitchenQual": {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3,
"Gd" : 4, "Ex" : 5},
"LandContour": {"None" : 0, "Low" : 1, "HLS" : 2, "Bnk" : 3,
"Lvl" : 4},
"LandSlope": {"None" : 0, "Sev" : 1, "Mod" : 2, "Gtl" : 3},
"LotShape": {"None" : 0, "IR3" : 1, "IR2" : 2, "IR1" : 3,
"Reg" : 4},
"PavedDrive": {"None" : 0, "N" : 0, "P" : 1, "Y" : 2},
"PoolQC": {"None" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3,
"Ex" : 4},
"Street": {"None" : 0, "Grvl" : 1, "Pave" : 2},
"Utilities": {"None" : 0, "ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3,
"AllPub" : 4}} 
)
 feat.BsmtCond = feat.BsmtCond.astype(int )
prediction = pd.DataFrame({"BiLSTM":bilstm.flatten() ,"CR":cr.flatten() ,"RC":rc.flatten() ,"target":y_train} )<choose_model_class>
def onehot_encode(data):
df_numeric = data.select_dtypes(exclude=['object'])
 df_obj = data.select_dtypes(include=['object'] ).copy()
cols = []
for c in df_obj:
dummies = pd.get_dummies(df_obj[c])
 dummies.columns = [c + "_" + str(x)for x in dummies.columns]
cols.append(dummies)
 df_obj = pd.concat(cols, axis=1)
 data = pd.concat([df_numeric, df_obj], axis=1)
 data.reset_index(inplace=True, drop=True)
 return data
feat = onehot_encode(feat )
clf = Sequential([
Dense(3,activation = 'relu'),
Dense(1,activation= 'sigmoid')
 ])
 clf.compile(loss = 'binary_crossentropy',optimizer = Adam(3e-5),metrics = ['acc'] )<train_model>
seasons = {12 : 0, 1 : 0, 2 : 0,
3 : 1, 4 : 1, 5 : 1,
6 : 2, 7 : 2, 8 : 2,
9 : 3, 10 : 3, 11 : 3}
feat["SeasonSold"] = feat["MoSold"].map(seasons)
 feat["YrActualAge"] = feat["YrSold"] - feat["YearBuilt"]
feat['TotalSF1'] = feat['TotalBsmtSF'] + feat['1stFlrSF'] + feat['2ndFlrSF']
feat['TotalSF2'] =(feat['BsmtFinSF1'] + feat['BsmtFinSF2'] + feat['1stFlrSF'] + feat['2ndFlrSF'])
 feat["AllSF"] = feat["GrLivArea"] + feat["TotalBsmtSF"]
feat["AllFlrsSF"] = feat["1stFlrSF"] + feat["2ndFlrSF"]
feat["AllPorchSF"] = feat["OpenPorchSF"] + feat["EnclosedPorch"] + feat["3SsnPorch"] + feat["ScreenPorch"]
feat['TotalBath'] = 2 *(feat['FullBath'] +(0.5 * feat['HalfBath'])+ feat['BsmtFullBath'] +(0.5 * feat['BsmtHalfBath']))
feat["TotalBath"] = feat["TotalBath"].astype(int)
 feat['TotalPorch'] =(feat['OpenPorchSF'] + feat['3SsnPorch'] + feat['EnclosedPorch'] + feat['ScreenPorch'] + feat['WoodDeckSF'])
 feat["OverallScore"] = feat["OverallQual"] * feat["OverallCond"]
feat["GarageScore"] = feat["GarageQual"] * feat["GarageCond"]
feat["ExterScore"] = feat["ExterQual"] * feat["ExterCond"]
feat["KitchenScore"] = feat["KitchenAbvGr"] * feat["KitchenQual"]
feat["FireplaceScore"] = feat["Fireplaces"] * feat["FireplaceQu"]
feat["GarageScore"] = feat["GarageArea"] * feat["GarageQual"]
feat["PoolScore"] = feat["PoolArea"] * feat["PoolQC"]
feat['hasPool'] = feat['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 feat['has2ndFloor'] = feat['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 feat['hasGarage'] = feat['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 feat['hasBsmt'] = feat['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 feat['hasFireplace'] = feat['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
history =clf.fit(prediction.loc[:,['BiLSTM','CR','RC']].values,prediction.iloc[:,-1],validation_split= 0.1,batch_size = 5,epochs = 32 )<load_from_csv>
df = pd.read_csv(f"{BASE_PATH}train.csv")
 df_test = pd.read_csv(f"{BASE_PATH}test.csv")
 dtrain = feat[feat.SalePrice.notnull() ].copy()
dtest = feat[feat.SalePrice.isnull() ].copy()
dtest = dtest.drop("SalePrice", axis=1 ).reset_index(drop=True)
 print(f"Raw data shape : {df.shape} {df_test.shape}")
 print(f"Clean data shape : {dtrain.shape} {dtest.shape}" )
test = pd.read_csv(".. /input/nlp-getting-started/test.csv" )<string_transform>
X = df
y = df.SalePrice
metric = 'neg_mean_squared_log_error'
clf = DummyRegressor("median")
 kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
MAX_SEQUENCE_LENGTH = 60
MAX_NB_WORDS = 30000
EMBEDDING_DIM = 300
sequences = tokenizer.texts_to_sequences(test.text.values)
 pad_text = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH )<predict_on_test>
X = df[["GrLivArea"]]
y = df.SalePrice
metric = 'neg_mean_squared_log_error'
clf = LinearRegression()
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
bilstm_test = BiLSTM.predict(pad_text ).flatten()
cr_test = CNNRNN.predict(pad_text ).flatten()
rc_test = RNNCNN.predict(pad_text ).flatten()<predict_on_test>
X = df[["OverallCond"]]
y = df.SalePrice
metric = 'neg_mean_squared_log_error'
clf = LinearRegression()
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
test_predictions = clf.predict(np.stack([bilstm_test,cr_test,rc_test],axis=-1)).flatten()
test_predictions = np.round(test_predictions ).astype(int )<load_from_csv>
try:
X = df[["OverallQual"]]
y = df.SalePrice
metric = 'neg_mean_squared_log_error'
clf = LinearRegression()
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error")
 except Exception as e:
print("Oh no...An error has occured...")
 print(e )
submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 submission['target'] = test_predictions<save_to_csv>
X = df[df.OverallQual >= 3][["OverallQual"]]
y = df[df.OverallQual >= 3].SalePrice
metric = 'neg_mean_squared_log_error'
clf = LinearRegression()
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
submission.to_csv("/kaggle/working/submission.csv",index = False )<install_modules>
X = df.select_dtypes("object")
 y = df.SalePrice
metric = 'neg_mean_squared_log_error'
clf = make_pipeline(SimpleImputer(strategy='most_frequent', fill_value='missing'),
OneHotEncoder(handle_unknown="ignore"), LinearRegression())
 kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
!pip install -q tensorflow-text 
<import_modules>
df_fixed = df[df.OverallQual >= 3]
X = df_fixed.select_dtypes("number" ).drop(["SalePrice"], axis=1)
 y = df_fixed.SalePrice
metric = 'neg_mean_squared_log_error'
clf = make_pipeline(SimpleImputer() , StandardScaler() , LinearRegression())
 kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
import numpy as np
import pandas as pd
import numpy as np
import tensorflow as tf
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import time
import numpy as np
import tensorflow_hub as hub
import tensorflow_text<load_pretrained>
X = df.select_dtypes("number" ).drop(["SalePrice"], axis=1)
 y = np.log1p(df.SalePrice)
 metric = 'neg_mean_squared_error'
clf = make_pipeline(SimpleImputer() , StandardScaler() , LinearRegression())
 kfold = KFold(n_splits=10, shuffle=True, random_state=1)
 print(f"{np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f} Log Error" )
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5" )<load_from_csv>
classifiers = [
Ridge() ,
Lasso() ,
ElasticNet() ,
KernelRidge() ,
SVR() ,
RandomForestRegressor() ,
GradientBoostingRegressor() ,
lgb.LGBMRegressor() ,
xgb.XGBRegressor(objective="reg:squarederror"),
]
clf_names = [
"ridge ",
"lasso ",
"elastic ",
"kernlrdg ",
"svr ",
"rndmforest ",
"gbm ",
"lgbm ",
"xgboost ",
]
train_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<categorify>
X = dtrain.drop(["SalePrice"], axis=1)
 y = np.log1p(dtrain.SalePrice)
 metric = 'neg_mean_squared_error'
for clf_name, clf in zip(clf_names, classifiers):
kfold = KFold(n_splits=5, shuffle=True, random_state=1)
 print(f"{clf_name} {np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f}" )
def embedd(dataset):
data = []
for i in tqdm(dataset):
embeddings = use(i)
 embeddings = tf.reshape(embeddings, [-1] ).numpy()
data.append(embeddings)
 return data<categorify>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1)
 y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 metric = 'neg_mean_squared_error'
for clf_name, clf in zip(clf_names, classifiers):
kfold = KFold(n_splits=5, shuffle=True, random_state=1)
 print(f"{clf_name} {np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f}" )
x_training = np.reshape(train_df.text.values,(len(train_df.text.values),1))
x_topredict = np.reshape(test_df.text.values,(len(test_df.text.values),1))
print("Embedding training data...")
 x_training = embedd(x_training)
 print("Embedding data for prediction...")
 x_topredict = embedd(x_topredict )<prepare_x_and_y>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1)
 pca = PCA(n_components=250)
 X_pca = pca.fit_transform(X)
 y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 metric = 'neg_mean_squared_error'
for clf_name, clf in zip(clf_names, classifiers):
kfold = KFold(n_splits=5, shuffle=True, random_state=1)
 print(f"{clf_name} {np.sqrt(-cross_val_score(clf, X_pca, y, cv=kfold, scoring=metric)).mean() :.4f}" )
x_training = np.array(x_training)
 y_train = train_df.target.values
x_topredict = np.array(x_topredict )<split>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1 ).copy()
sk = pd.DataFrame(X.iloc[:, :60].skew() , columns=["skewness"])
 sk = sk[sk.skewness >.75]
for feature_ in sk.index:
X[feature_] = boxcox1p(X[feature_], 0.15)
 y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 metric = 'neg_mean_squared_error'
for clf_name, clf in zip(clf_names, classifiers):
kfold = KFold(n_splits=5, shuffle=True, random_state=1)
 print(f"{clf_name} {np.sqrt(-cross_val_score(clf, X, y, cv=kfold, scoring=metric)).mean() :.4f}" )
X_train, X_test, y_train, y_test = train_test_split(x_training, y_train,test_size = 0.25, random_state=7 )<train_model>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1 ).copy()
y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 sk = pd.DataFrame(X.iloc[:, :60].skew() , columns=["skewness"])
 sk = sk[sk.skewness >.75]
for feature_ in sk.index:
X[feature_] = boxcox1p(X[feature_], 0.15)
 clf = xgb.XGBRegressor(objective="reg:squarederror", n_jobs=-1)
 coeffs = clf.fit(X, y ).feature_importances_
df_co = pd.DataFrame(coeffs, columns=["importance_"])
 df_co.index = X.columns
df_co.sort_values("importance_", ascending=True, inplace=True)
 plt.figure(figsize=(16,9))
df_co.iloc[250:, :].importance_.plot(kind="barh")
 plt.title(f"XGBoost feature importance")
 plt.show()
clf = linear_model.RidgeClassifier()
clf.fit(X_train, y_train )<compute_test_metric>
classifiers = [
Ridge() ,
Lasso() ,
ElasticNet() ,
KernelRidge() ,
GradientBoostingRegressor() ,
lgb.LGBMRegressor() ,
xgb.XGBRegressor(objective="reg:squarederror"),
]
clf_names = [
"ridge ",
"lasso ",
"elastic ",
"kernlrdg ",
"gbm ",
"lgbm ",
"xgboost ",
]
y_pred = clf.predict(X_test)
 cm = confusion_matrix(y_test, y_pred)
 print(cm)
 print("The accuracy of the model in the tested data is: ",accuracy_score(y_test, y_pred))
print("The f1 score of the model in the tested data is: ",f1_score(y_test, y_pred))<train_on_grid>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1 ).copy()
y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 X_test = dtest.copy()
sk = pd.DataFrame(X.iloc[:, :60].skew() , columns=["skewness"])
 sk = sk[sk.skewness >.75]
for feature_ in sk.index:
X[feature_] = boxcox1p(X[feature_], 0.15)
 X_test[feature_] = boxcox1p(X_test[feature_], 0.15 )
log_clf = LogisticRegression()
rnd_clf = RandomForestClassifier()
svm_clf = SVC()
log_clf.fit(X_train, y_train)
 rnd_clf.fit(X_train, y_train)
 svm_clf.fit(X_train, y_train )<compute_test_metric>
y_pred = rnd_clf.predict(X_test)
 y_pred =(y_pred > 0.5)
 cm = confusion_matrix(y_test, y_pred)
 print(cm)
 print("The accuracy of the model Random Forest in the tested data is: ",accuracy_score(y_test, y_pred))
print("The f1 score of the model Random Forest in the tested data is: ",f1_score(y_test, y_pred)) 
<save_to_csv>
predictions = []
for clf_name, clf in zip(clf_names, classifiers):
clf.fit(X, y)
 preds = clf.predict(X_test)
 predictions.append(np.expm1(preds))
sample_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 y_pred = svm_clf.predict(x_topredict)
 y_pred =(y_pred > 0.5 ).astype(int)
 sample_submission["target"] = y_pred
sample_submission.to_csv("submission_SVC_universal_sentence_encoder.csv", index=False )<import_modules>
for idx, clf_name in enumerate(clf_names):
p = pd.DataFrame(predictions[idx])
 p_out = p[p[0] > 755_000].astype(int)
 if len(p_out)> 0:
p_out.columns = [f"{clf_name.strip().capitalize() } _ predicted SalePrice"]
display(p_out )
import numpy as np
import pandas as pd
import os
import random
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tqdm import tqdm
from PIL import Image
import pydicom
import pydicom
import matplotlib.pyplot as plt
from glob import glob
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import scipy.ndimage
from skimage import morphology
from skimage import measure
from skimage.transform import resize
from sklearn.cluster import KMeans
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import *<define_variables>
dtest.loc[1089][["YearBuilt", "YrSold", "YrActualAge"]]
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )<define_variables>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1)
 sk = pd.DataFrame(X.iloc[:, :60].skew() , columns=["skewness"])
 sk = sk[sk.skewness >.75]
for feature_ in sk.index:
X[feature_] = boxcox1p(X[feature_], 0.15)
 y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 metric = 'neg_mean_squared_error'
predictions = []
pred_stats = [df.SalePrice.describe() ]
for clf_name, clf in zip(clf_names, classifiers):
clf.fit(X, y)
 preds = clf.predict(X_test)
 predictions.append(np.expm1(preds))
pred_stats.append(pd.DataFrame(np.expm1(preds)).describe())
 pr_corr = pd.DataFrame(predictions ).T.corr()
pr_corr.columns = [x.strip() for x in clf_names]
pr_corr.index = [x.strip() for x in clf_names]
plt.figure(figsize=(16,9))
sns.heatmap(pr_corr, cmap="RdBu", square=True, cbar_kws={"shrink":.8})
 plt.title("Correlation of predictions of trained models")
 plt.tight_layout()
plt.show()
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE = 196<load_from_csv>
train_df = pd.read_csv(f"{ROOT}/train.csv")
 train_df.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )<categorify>
train_df['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = train_df.append([chunk, sub] )<feature_engineering>
X = dtrain[dtrain.GrLivArea < 4000].drop(["SalePrice"], axis=1)
 y = np.log1p(dtrain[dtrain.GrLivArea < 4000].SalePrice)
 X_test = dtest
X_test.loc[1089]["YrSold"] = 2009
X_test.loc[1089]["YrActualAge"] = 0
metric = 'neg_mean_squared_error'
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
sk = pd.DataFrame(X.iloc[:, :60].skew() , columns=["skewness"])
 sk = sk[sk.skewness >.75]
for feature_ in sk.index:
X[feature_] = boxcox1p(X[feature_], 0.15)
 X_test[feature_] = boxcox1p(X_test[feature_], 0.15 )
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )<merge>
ridge = make_pipeline(RobustScaler() , Ridge(alpha=15, random_state=1))
lasso = make_pipeline(RobustScaler() , Lasso(alpha=0.0005, random_state=1))
elastic = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005,
l1_ratio=1, random_state=1))
kernel = KernelRidge(alpha=1.0)
 gbm = GradientBoostingRegressor(n_estimators=2500, learning_rate=0.04,
max_depth=2, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state=1)
 lgbm = lgb.LGBMRegressor(objective='regression', num_leaves=5,
learning_rate=0.03, n_estimators=8000,
max_bin=55, bagging_fraction=0.8,
bagging_freq=5, feature_fraction=0.23,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf=6, min_sum_hessian_in_leaf=11)
 xgbreg = xgb.XGBRegressor(objective="reg:squarederror",
colsample_bytree=0.46, gamma=0.047,
learning_rate=0.04, max_depth=2,
min_child_weight=0.5, n_estimators=2000,
reg_alpha=0.46, reg_lambda=0.86,
subsample=0.52, random_state=1, n_jobs=-1 )
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<categorify>
classifiers = [ridge, lasso, elastic, kernel, gbm, lgbm, xgbreg]
clf_names = ["ridge ", "lasso ", "elastic", "kernel ", "gbm ", "lgbm ", "xgbreg "]
predictions_exp = []
for clf_name, clf in zip(clf_names, classifiers):
print(f"{clf_name} {np.sqrt(-cross_val_score(clf, X, y, scoring=metric ).mean()):.5f}")
 clf.fit(X, y)
 preds = clf.predict(X_test)
 predictions_exp.append(np.expm1(preds))
<feature_engineering><EOS> 
prediction_final = pd.DataFrame(predictions_exp ).mean().T.values
submission = pd.DataFrame({'Id': df_test.Id.values, 'SalePrice': prediction_final})
 submission.to_csv(f"submission.csv", index=False )