Kaggle competition: House Prices - Advanced Regression Techniques
def load_and_preprocess_data(max_features=50000, maxlen=70):
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: clean_text(x))
test_df["question_text"] = test_df["question_text"].progress_apply(lambda x: clean_text(x))
train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: clean_numbers(x))
test_df["question_text"] = test_df["question_text"].progress_apply(lambda x: clean_numbers(x))
train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: replace_typical_misspell(x))
test_df["question_text"] = test_df["question_text"].progress_apply(lambda x: replace_typical_misspell(x))
train_X = train_df["question_text"].fillna("_
test_X = test_df["question_text"].fillna("_
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train_df['target'].values
np.random.seed(2018)
 trn_idx = np.random.permutation(len(train_X))
train_X = train_X[trn_idx]
train_y = train_y[trn_idx]
return train_X, test_X, train_y, tokenizer.word_index<statistical_test>
%matplotlib inline
sns.set_style('whitegrid')
 pd.options.mode.chained_assignment = None
path=('.. /input/houses-2/houses.jfif')
 display(Image.open(path))
def load_glove(word_index):
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_fasttext(word_index):
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_para(word_index):
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix<set_options>
train= pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test= pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 data = pd.concat([train,test], axis = 0 , sort = False ).reset_index(drop=True)
 data.drop('SalePrice', axis =1 , inplace=True )
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight('{}_W'.format(self.name), shape=(input_shape[-1].value,),
initializer=self.init,
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight('{}_b'.format(self.name), shape=(input_shape[1].value,),
initializer='zero',
regularizer=self.b_regularizer,
constraint=self.b_constraint 
)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim<choose_model_class>
train["SalePrice"] = np.log1p(train["SalePrice"])
 multi_plotting(train,'SalePrice' )
def model_lstm_attention(embedding_matrix, maxlen=70, max_features=50000, units=64):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(units*2, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(units, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(units, activation='relu' )(x)
 outp= Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy', optimizer=Adam() , metrics=['accuracy'])
 return model
def model_gru_attention(embedding_matrix, maxlen=70, max_features=50000, units=64):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNGRU(units*2, return_sequences=True))(x)
 x = Bidirectional(CuDNNGRU(units, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(units, activation='relu' )(x)
 outp= Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy', optimizer=Adam() , metrics=['accuracy'])
 return model
def model_gru_attention_3(embedding_matrix, maxlen=70, max_features=50000, units=64):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNGRU(units*2, kernel_initializer=initializers.glorot_uniform(seed = SEED), return_sequences=True))(x)
 x = Bidirectional(CuDNNGRU(units, kernel_initializer=initializers.glorot_uniform(seed = SEED), return_sequences=True))(x)
 x = Bidirectional(CuDNNGRU(units//2, kernel_initializer=initializers.glorot_uniform(seed = SEED), return_sequences=True))(x)
 x = Attention(maxlen )(x)
 outp= Dense(1, kernel_initializer=initializers.he_uniform(seed=SEED), activation='sigmoid' )(x)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy', optimizer=Adam() , metrics=['accuracy'])
 return model<find_best_params>
def missing_rate(frame):
TL = frame.isna().sum() /len(frame)
 TCMR = TL[TL >0.6].index
frame.drop(TCMR, axis= 1 , inplace = True)
 print(f'Colums to be dropped:{TCMR}')
 missing_rate(train)
 missing_rate(data )
def train_pred(model, train_X, train_y, val_X, val_y, epochs=2):
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y))
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 best_thresh = 0.5
best_score = 0.0
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 score = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
if score > best_score:
best_thresh = thresh
best_score = score
print("Val F1 Score: {:.4f}".format(best_score))
pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 print('='*100)
 return pred_val_y, pred_test_y, best_score<define_variables>
numeric = data.dtypes[data.dtypes != 'object'].index
tnumeric = train.dtypes[train.dtypes != 'object'].index
integer = data[numeric]
tinteger = train[tnumeric]
integer.fillna(0, inplace=True)
 tinteger.fillna(0 , inplace = True )
embed_size = 300
max_features = 100000
maxlen = 40<train_model>
Final = Feature.merge(Target,left_on='level_1', right_on='index')
 Final
start_time = time.time()
train_X, test_X, train_y, word_index = load_and_preprocess_data(max_features=max_features, maxlen=maxlen)
 total_time =(time.time() - start_time)/60.0
print("Took {0} minutes".format(total_time))<train_model>
tinteger.drop(['GarageArea','GarageYrBlt','TotRmsAbvGrd','Id'],axis=1, inplace=True)
 integer.drop(['GarageArea','GarageYrBlt','TotRmsAbvGrd','Id'],axis=1, inplace=True )
start_time = time.time()
embedding_matrix_1 = load_glove(word_index)
 embedding_matrix_3 = load_para(word_index)
 total_time =(time.time() - start_time)/60.0
print("Took {0} minutes".format(total_time))<compute_test_metric>
tinteger['Porch'] = tinteger['3SsnPorch']+tinteger['ScreenPorch']+tinteger['EnclosedPorch']+tinteger['OpenPorchSF']
tinteger['YNPorch'] = tinteger['Porch'].apply(lambda x : 1 if x>0 else 0)
 tinteger.drop(['3SsnPorch','ScreenPorch','EnclosedPorch','OpenPorchSF','PoolArea'], axis= 1, inplace=True)
 integer['Porch'] = integer['3SsnPorch']+integer['ScreenPorch']+integer['EnclosedPorch']+integer['OpenPorchSF']
integer['YNPorch'] = integer['Porch'].apply(lambda x : 1 if x>0 else 0)
 integer.drop(['3SsnPorch','ScreenPorch','EnclosedPorch','OpenPorchSF','PoolArea'], axis= 1, inplace=True)
embedding_matrix = np.mean([embedding_matrix_1, embedding_matrix_3], axis = 0)
 print(np.shape(embedding_matrix))<compute_test_metric>
def cat_basmentfin(df):
df['Bsmntbuilt'] = 0
A= df[(df['BsmtFinSF1']>0)|(df['BsmtFinSF2']>0)&(df['BsmtUnfSF'] >= 0)].index
B= df[(df['BsmtFinSF1']==0)&(df['BsmtFinSF2']==0)&(df['BsmtUnfSF'] > 0)].index
C= df[(df['BsmtFinSF1']==0)&(df['BsmtFinSF2']==0)&(df['BsmtUnfSF'] == 0)].index
df.loc[A, 'Bsmntbuilt'] = 2
df.loc[B, 'Bsmntbuilt'] = 1
df.loc[C, 'Bsmntbuilt'] = 0
df.drop(['BsmtFinSF1','BsmtFinSF2','BsmtUnfSF'],axis= 1 , inplace=True)
def threshold_search(y_true, y_proba):
best_threshold = 0
best_score = 0
for threshold in [i * 0.01 for i in range(100)]:
score = f1_score(y_true=y_true, y_pred=y_proba > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
search_result = {'threshold': best_threshold, 'f1': best_score}
return search_result<split>
cat_basmentfin(tinteger)
 cat_basmentfin(integer )
train_meta = np.zeros(train_y.shape)
 test_meta = np.zeros(test_X.shape[0])
 splits = list(StratifiedKFold(n_splits=4, shuffle=True, random_state=SEED ).split(train_X, train_y))
for idx,(train_idx, valid_idx)in enumerate(splits):
X_train = train_X[train_idx]
y_train = train_y[train_idx]
X_val = train_X[valid_idx]
y_val = train_y[valid_idx]
tmp_result = []
model = model_gru_attention_3(embedding_matrix, maxlen=maxlen, max_features=max_features, units=64)
 pred_val_y, pred_test_y, best_score = train_pred(model, X_train, y_train, X_val, y_val, epochs = 2)
 tmp_result.append([pred_val_y, pred_test_y, best_score])
 model = model_gru_attention(embedding_matrix, maxlen=maxlen, max_features=max_features, units=64)
 pred_val_y, pred_test_y, best_score = train_pred(model, X_train, y_train, X_val, y_val, epochs = 2)
 tmp_result.append([pred_val_y, pred_test_y, best_score])
 model = model_lstm_attention(embedding_matrix_1, maxlen=maxlen, max_features=max_features, units=64)
 pred_val_y, pred_test_y, best_score = train_pred(model, X_train, y_train, X_val, y_val, epochs = 2)
 tmp_result.append([pred_val_y, pred_test_y, best_score])
 pred_val_y, pred_test_y, best_score = np.mean(tmp_result, axis=0)
 print("Mean Result: ", best_score)
 train_meta[valid_idx] = pred_val_y.reshape(-1)
 test_meta += pred_test_y.reshape(-1)/ len(splits )<save_to_csv>
def basmentbath(df):
No_BsmtFullBath=df[(df['BsmtFullBath']==0)&(df['BsmtHalfBath']==0)].index
Only_halfBath=df[(df['BsmtFullBath']==0)&(df['BsmtHalfBath']==1)].index
Two_halfBath =df[(df['BsmtFullBath']==0)&(df['BsmtHalfBath']==2)].index
Only_FullBath=df[(df['BsmtFullBath']==1)&(df['BsmtHalfBath']==0)].index
One_Full_Half = df[(df['BsmtFullBath']==1)&(df['BsmtHalfBath']==1)].index
Two_Full = df[(df['BsmtFullBath']==2)&(df['BsmtHalfBath']==0)].index
Three_Full = df[(df['BsmtFullBath']==3)&(df['BsmtHalfBath']==0)].index
df['BsmtBathCat'] = 0
df.loc[No_BsmtFullBath, 'BsmtBathCat'] = 0
df.loc[Only_halfBath, 'BsmtBathCat'] = 1
df.loc[Two_halfBath, 'BsmtBathCat'] = 2
df.loc[Only_FullBath, 'BsmtBathCat'] = 3
df.loc[One_Full_Half, 'BsmtBathCat'] = 4
df.loc[Two_Full, 'BsmtBathCat'] = 5
df.loc[Three_Full, 'BsmtBathCat'] = 6
df.drop(['BsmtFullBath','BsmtHalfBath'],axis= 1 , inplace=True )
search_result = threshold_search(train_y, train_meta)
 print(search_result)
 sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.prediction = test_meta > search_result['threshold']
sub.to_csv("submission.csv", index=False )<set_options>
integer['2ndFloor']=integer['2ndFlrSF'].apply(lambda x: 0 if x==0 else 1)
 tinteger['2ndFloor']=tinteger['2ndFlrSF'].apply(lambda x: 0 if x==0 else 1)
 integer.drop(['1stFlrSF','2ndFlrSF'],axis =1, inplace = True)
 tinteger.drop(['1stFlrSF','2ndFlrSF'],axis =1, inplace = True )
%matplotlib inline
pd.set_option('max_colwidth',400)
 warnings.filterwarnings("ignore", message="F-score is ill-defined and being set to 0.0 due to no predicted samples.")
<set_options>
tinteger= tinteger.join(train['Neighborhood'])
 lot_mean = tinteger.groupby("Neighborhood")["LotFrontage"].mean().round().to_dict()
tinteger.loc[(tinteger['LotFrontage']==0)&(tinteger['Neighborhood'].isin(lot_mean.keys())) , 'LotFrontage'] = tinteger['Neighborhood'].map(lot_mean)
 tinteger.drop('Neighborhood',axis=1 , inplace=True)
 integer= integer.join(data['Neighborhood'])
 lot_mean = integer.groupby("Neighborhood")["LotFrontage"].mean().round().to_dict()
integer.loc[(integer['LotFrontage']==0)&(integer['Neighborhood'].isin(lot_mean.keys())) , 'LotFrontage'] = integer['Neighborhood'].map(lot_mean)
 integer.drop('Neighborhood',axis=1 , inplace=True )
def seed_torch(seed=1029):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<load_from_csv>
integer.drop(['LowQualFinSF','MiscVal'],axis =1, inplace = True)
 tinteger.drop(['LowQualFinSF','MiscVal'],axis =1, inplace = True )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 sub = pd.read_csv('.. /input/sample_submission.csv' )<count_values>
integer.drop(['MoSold','YrSold','KitchenAbvGr'],axis =1, inplace = True)
 tinteger.drop(['MoSold','YrSold','KitchenAbvGr'],axis =1, inplace = True )
train["target"].value_counts()<split>
categorical = data[['KitchenQual','FireplaceQu','GarageQual','GarageFinish','GarageCond','BsmtCond','BsmtExposure','BsmtQual','BsmtFinType2',
'BsmtFinType1','ExterCond','ExterQual','HeatingQC','LandSlope','LotShape','PavedDrive','Street',
'BldgType','Functional','SaleType','GarageType','Electrical','Foundation','LandContour','RoofMatl','RoofStyle']]
tcategorical = train[['SalePrice','KitchenQual','FireplaceQu','GarageQual','GarageFinish','GarageCond','BsmtCond','BsmtExposure','BsmtQual','BsmtFinType2',
'BsmtFinType1','ExterCond','ExterQual','HeatingQC','LandSlope','LotShape','PavedDrive','Street',
'BldgType','Functional','SaleType','GarageType','Electrical','Foundation','LandContour','RoofMatl','RoofStyle']]
print('Average word length of questions in train is {0:.0f}.'.format(np.mean(train['question_text'].apply(lambda x: len(x.split())))))
 print('Average word length of questions in test is {0:.0f}.'.format(np.mean(test['question_text'].apply(lambda x: len(x.split())))) )<string_transform>
kit ={"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1,"None":0}
fire ={"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1,"None":0}
garagequa ={"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1,"None":0}
garage_finish = {"Fin":3,"RFn":2,"Unf":1,"None":0}
garagecond= {"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1,"None":0}
bsmtcond= {"Gd":4,"TA":3,"Fa":2,"Po":1,"None":0}
bsmtexposure = {"Gd":4,"Av":3,"Mn":2,"No":1,"None":0}
bsmtqual = {"Ex":4,"Gd":3,"TA":2,"Fa":1,"None":0}
bsmtype2={"GLQ":6,"ALQ":5,"BLQ":4,"Rec":3,"LwQ":2,"Unf":1,"None":0}
bsmtype1 = {"GLQ":6,"ALQ":5,"BLQ":4,"Rec":3,"LwQ":2,"Unf":1,"None":0}
extercond = {"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1}
exterqual = {"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1}
heatingqc = {"Ex":5,"Gd":4,"TA":3,"Fa":2,"Po":1}
landslope={"Gtl":3,"Mod":2,"Sev":1}
lotshape={"Reg":4,"IR1":3,"IR2":2,"IR3":1}
pavedrive= {"Y":3,"P":2,"N":1}
street={"Grvl":2,"Pave":1}
bldgtype = {'1Fam': 1, '2fmCon': 2, 'Duplex': 3, 'TwnhsE': 4, 'Twnhs': 5}
functional = {'Typ': 8,'Min1': 7,'Min2': 6,'Mod': 5,'Maj1': 4,'Maj2': 3,'Sev': 2,'Sal': 1,'None':1}
saletype ={'WD': 10,'CWD': 9,'VWD': 8,'New': 7,'COD': 6,'Con': 5,'ConLw': 4,'ConLI': 3,'ConLD': 2,'Oth': 1,'None':0}
garage={'2Types': 6, 'Attchd': 5, 'Basment': 4, 'BuiltIn': 3, 'CarPort': 2, 'Detchd': 1, 'None': 0}
electrical={'SBrkr': 5, 'FuseA': 4, 'FuseF': 3, 'FuseP': 2, 'Mix': 1,'None':0}
foundation={'BrkTil': 6, 'CBlock': 5, 'PConc': 4, 'Slab': 3, 'Stone': 2, 'Wood': 1}
landcontour={'Lvl': 4, 'Bnk': 3, 'HLS': 2, 'Low': 1}
roofmatl={'ClyTile': 8, 'CompShg': 7, 'Membran': 6, 'Metal': 5, 'Roll': 4, 'Tar&Grv': 3, 'WdShake': 2, 'WdShngl': 1}
roofstyle ={'Flat': 6, 'Gable': 5, 'Gambrel': 4, 'Hip': 3, 'Mansard': 2, 'Shed': 1}
print('Max word length of questions in train is {0:.0f}.'.format(np.max(train['question_text'].apply(lambda x: len(x.split())))))
 print('Max word length of questions in test is {0:.0f}.'.format(np.max(test['question_text'].apply(lambda x: len(x.split())))) )<compute_test_metric>
for i in tcategorical:
tcategorical[i].fillna('None',inplace=True)
 for i in categorical:
categorical[i].fillna('None',inplace=True )
print('Average character length of questions in train is {0:.0f}.'.format(np.mean(train['question_text'].apply(lambda x: len(x)))))
 print('Average character length of questions in test is {0:.0f}.'.format(np.mean(test['question_text'].apply(lambda x: len(x)))) )<define_variables>
list_dict =[kit,fire,garagequa,garage_finish,garagecond,bsmtcond,bsmtexposure,bsmtqual,bsmtype2,
bsmtype1,extercond,exterqual,heatingqc,landslope,lotshape,pavedrive,street,
bldgtype,functional,saletype,garage,electrical,foundation,landcontour,roofmatl,roofstyle]
def replace_t(i,diccionaty):
tcategorical[i].replace(diccionaty, inplace=True)
 def replace(i,diccionaty):
categorical[i].replace(diccionaty, inplace=True)
 for i,j in zip(tcategorical.columns[1:].to_list() ,list_dict):
replace_t(i,j)
 for i,j in zip(categorical.columns.to_list() ,list_dict):
replace(i,j)
 for i in categorical:
v= categorical[i].unique()
print(i, v )
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
def clean_text(x):
x = str(x)
 for punct in puncts:
x = x.replace(punct, f' {punct} ')
 return x
def clean_numbers(x):
x = re.sub('[0-9]{5,}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
return x
mispell_dict = {"aren't" : "are not",
"can't" : "cannot",
"couldn't" : "could not",
"didn't" : "did not",
"doesn't" : "does not",
"don't" : "do not",
"hadn't" : "had not",
"hasn't" : "has not",
"haven't" : "have not",
"he'd" : "he would",
"he'll" : "he will",
"he's" : "he is",
"i'd" : "I would",
"i'd" : "I had",
"i'll" : "I will",
"i'm" : "I am",
"isn't" : "is not",
"it's" : "it is",
"it'll":"it will",
"i've" : "I have",
"let's" : "let us",
"mightn't" : "might not",
"mustn't" : "must not",
"shan't" : "shall not",
"she'd" : "she would",
"she'll" : "she will",
"she's" : "she is",
"shouldn't" : "should not",
"that's" : "that is",
"there's" : "there is",
"they'd" : "they would",
"they'll" : "they will",
"they're" : "they are",
"they've" : "they have",
"we'd" : "we would",
"we're" : "we are",
"weren't" : "were not",
"we've" : "we have",
"what'll" : "what will",
"what're" : "what are",
"what's" : "what is",
"what've" : "what have",
"where's" : "where is",
"who'd" : "who would",
"who'll" : "who will",
"who're" : "who are",
"who's" : "who is",
"who've" : "who have",
"won't" : "will not",
"wouldn't" : "would not",
"you'd" : "you would",
"you'll" : "you will",
"you're" : "you are",
"you've" : "you have",
"'re": " are",
"wasn't": "was not",
"we'll":" will",
"didn't": "did not",
"tryin'":"trying"}
def _get_mispell(mispell_dict):
mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))
return mispell_dict, mispell_re
mispellings, mispellings_re = _get_mispell(mispell_dict)
 def replace_typical_misspell(text):
def replace(match):
return mispellings[match.group(0)]
return mispellings_re.sub(replace, text)
 train["question_text"] = train["question_text"].apply(lambda x: clean_text(x.lower()))
test["question_text"] = test["question_text"].apply(lambda x: clean_text(x.lower()))
train["question_text"] = train["question_text"].apply(lambda x: clean_numbers(x))
test["question_text"] = test["question_text"].apply(lambda x: clean_numbers(x))
train["question_text"] = train["question_text"].apply(lambda x: replace_typical_misspell(x))
test["question_text"] = test["question_text"].apply(lambda x: replace_typical_misspell(x))<train_model>
for i in tcategorical:
v = tcategorical[i].value_counts().to_frame().reset_index().iloc[0].to_list()
w= tcategorical[i].value_counts().reset_index().iloc[0][0]
if w == 0:
print(i,v )
max_features = 120000
tk = Tokenizer(lower = True, filters='', num_words=max_features)
 full_text = list(train['question_text'].values)+ list(test['question_text'].values)
 tk.fit_on_texts(full_text )<string_transform>
tcategorical.drop('FireplaceQu',axis=1, inplace=True)
 categorical.drop('FireplaceQu',axis=1, inplace=True )
train_tokenized = tk.texts_to_sequences(train['question_text'].fillna('missing'))
test_tokenized = tk.texts_to_sequences(test['question_text'].fillna('missing'))<categorify>
for i in nominal:
encoding = pd.get_dummies(nominal[i], prefix=i, drop_first= True)
 nominal= pd.concat([nominal,encoding],axis=1)
 nominal.drop(i,axis=1, inplace=True)
 nominal
max_len = 72
maxlen = 72
X_train = pad_sequences(train_tokenized, maxlen = max_len)
 X_test = pad_sequences(test_tokenized, maxlen = max_len )<prepare_x_and_y>
all_features = nominal.keys()
nominal = nominal.drop(nominal.loc[:,(nominal==0 ).sum() >=(nominal.shape[0]*0.9994)],axis=1)
 nominal = nominal.drop(nominal.loc[:,(nominal==1 ).sum() >=(nominal.shape[0]*0.9994)],axis=1)
 remain_features = nominal.keys()
remov_features = [st for st in all_features if st not in remain_features]
print(len(remov_features), 'features were removed:', remov_features )
y_train = train['target'].values<compute_test_metric>
new_data = pd.concat([integer,categorical,nominal], axis = 1 , sort=False)
 new_data
def sigmoid(x):
return 1 /(1 + np.exp(-x))<split>
train_data =new_data.iloc[:1460].reset_index(drop=True)
 test_data = new_data.iloc[1460:].reset_index(drop=True )
splits = list(StratifiedKFold(n_splits=4, shuffle=True, random_state=10 ).split(X_train, y_train))<statistical_test>
X = train_data
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
 LR = LinearRegression().fit(X_train,y_train)
 y_pred_LR = LR.predict(test_data)
 test_Id=test['Id']
my_submission = pd.DataFrame({'Id': test_Id, 'SalePrice': np.expm1(y_pred_LR)})
 my_submission.to_csv('KaggelsubmissionLR.csv', index=False )
embed_size = 300
embedding_path = ".. /input/embeddings/glove.840B.300d/glove.840B.300d.txt"
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embedding_index = dict(get_coefs(*o.split(" ")) for o in open(embedding_path, encoding='utf-8', errors='ignore'))
emb_mean,emb_std = -0.005838499, 0.48782197
word_index = tk.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words + 1, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embedding_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<compute_train_metric>
train_data = train_data.astype(int)
 test_data = test_data.astype(int)
 X = train_data
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42 )
<compute_test_metric><EOS> 
classifier = xgboost.XGBRegressor(base_score=0.25, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0,
importance_type='gain', learning_rate=0.05, max_delta_step=0,
max_depth=2, min_child_weight=4, missing=None, n_estimators=900,
n_jobs=1, nthread=None, objective='reg:linear', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=None, subsample=1, verbosity=0)
 classifier = classifier.fit(X_train,y_train)
 y_pred_XG = classifier.predict(test_data)
 sub = pd.DataFrame()
sub = pd.DataFrame()
sub['Id'] = test['Id']
sub['SalePrice'] = np.expm1(y_pred_XG)
 sub.to_csv('KaggelXBoostsubmission.csv',index=False )