Kaggle competition: House Prices - Advanced Regression Techniques
preds = np.concatenate(predictions )<predict_on_test>
warnings.filterwarnings('ignore')
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
labels = np.argmax(preds, axis =1)
<save_to_csv>
train.drop('Id', inplace = True, axis =1)
 test.drop('Id', inplace = True, axis =1)
submission = pd.DataFrame({'PhraseId': test_id, 'Sentiment': labels})
 submission.to_csv('submission.csv', index=False)
 submission.head()<import_modules>
test_A = test
from __future__ import absolute_import 
<define_variables>
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )
package_dir = ".. /input/ppbert/pytorch-pretrained-bert/pytorch-pretrained-BERT"
sys.path.append(package_dir )<import_modules>
train_A = train.drop(['SalePrice'], axis=1)
 y = pd.DataFrame(train['SalePrice'])
 features = pd.concat([train_A, test_A] ).reset_index(drop=True )
from __future__ import absolute_import<import_modules>
y_log = pd.DataFrame(np.log1p(y['SalePrice']))
distribution(y_log,'SalePrice' )
from __future__ import division<import_modules>
numerical_features = features.dtypes[features.dtypes != "object"].index
categorical_features = features.dtypes[features.dtypes == "object"].index
numerical_df = features[numerical_features]
categorical_df = features[categorical_features]
from __future__ import print_function<import_modules>
def fill_na_num_mean(df,column_name):
df[column_name] = df[column_name].transform(lambda x: x.fillna(round(x.mean() ,1)))
 return df
def fill_na_num_median(df,column_name):
df[column_name] = df[column_name].transform(lambda x: x.fillna(round(x.median() ,1)))
 return df
def fill_na_cat_none(df,column_name):
df[column_name].fillna('None',inplace=True)
 def fill_na_num_0(df,column_name):
df[column_name].fillna(0,inplace=True)
 def fill_na_mode(df,column_name):
df[column_name].fillna(df[column_name].mode() [0], inplace = True)
import numpy as np
import pandas as pd
from tqdm import tqdm
import os
import warnings<import_modules>
features.drop(['MiscFeature'], inplace = True, axis = 1)
 features.drop(['MiscVal'], inplace = True, axis = 1 )
from pytorch_pretrained_bert import BertTokenizer, BertForSequenceClassification, BertAdam
from pytorch_pretrained_bert import BertConfig
import gc 
<import_modules>
features.drop(['Fence'], inplace = True, axis =1)
 features.drop(['Alley'], inplace = True, axis = 1)
 features.drop(['PoolQC'], inplace = True, axis = 1)
 features.drop(['PoolArea'], inplace = True, axis =1 )
import torch.utils.data<set_options>
fill_na_mode(features,'LotFrontage' )
warnings.filterwarnings(action='once')
 device = torch.device('cuda' )<string_transform>
fill_na_cat_none(features,'MasVnrType')
 fill_na_num_0(features,'MasVnrArea')
def convert_lines(example, max_seq_length,tokenizer):
max_seq_length -=2
all_tokens = []
longer = 0
for text in tqdm(example):
tokens_a = tokenizer.tokenize(text)
 if len(tokens_a)>max_seq_length:
tokens_a = tokens_a[:max_seq_length]
longer += 1
one_token = tokenizer.convert_tokens_to_ids(["[CLS]"]+tokens_a+["[SEP]"])+[0] *(max_seq_length - len(tokens_a))
all_tokens.append(one_token)
 return np.array(all_tokens)
 MAX_SEQUENCE_LENGTH = 280
SEED = 1234
BATCH_SIZE = 32
BERT_MODEL_PATH = '.. /input/bert-pretrained-models/uncased_l-12_h-768_a-12/uncased_L-12_H-768_A-12/'
LARGE_BERT_MODEL_PATH = '.. /input/bert-pretrained-models/uncased_l-24_h-1024_a-16/uncased_L-24_H-1024_A-16/'
np.random.seed(SEED)
 torch.manual_seed(SEED)
 torch.cuda.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True 
<load_from_csv>
fill_na_num_0(features,'BsmtFinSF2')
 features[['BsmtFinSF1','BsmtFinType1']].head()
train_df =pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv' )<load_from_csv>
fill_na_cat_none(features,'BsmtFinType1')
 fill_na_num_0(features,'BsmtFinSF1')
 fill_na_cat_none(features,'BsmtFinType2')
 fill_na_num_0(features,'BsmtFinSF2')
 features[['TotalBsmtSF','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF']].head()
test_df = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv' )<define_variables>
fill_na_num_0(features,'TotalBsmtSF')
 fill_na_num_0(features,'BsmtUnfSF' )
BERT_SMALL_PATH = '.. /input/bert-pretrained-models/uncased_l-12_h-768_a-12/uncased_L-12_H-768_A-12/'
BERT_LARGE_PATH = '.. /input/bert-pretrained-models/uncased_l-24_h-1024_a-16/uncased_L-24_H-1024_A-16/'
JIGSAW_BERT_SMALL_MODEL_PATH = '.. /input/bert-fast-all-v1/bert_pytorch.bin'
JIGSAW_BERT_LARGE_MODEL_PATH = '.. /input/pretrained-b-j/jigsaw-bert-large-uncased-len-220-fp16/epoch-1/pytorch_model.bin'
JIGSAW_BERT_SMALL_JSON_PATH = '.. /input/google-ais-bert-model-for-nlp/bert_config.json'
JIGSAW_BERT_LARGE_JSON_PATH = '.. /input/pretrained-b-j/jigsaw-bert-large-uncased-len-220-fp16/epoch-1/config.json'
NUM_BERT_MODELS = 2
INFER_BATCH_SIZE = 64
test_preds = np.zeros(( test_df.shape[0],NUM_BERT_MODELS))
np.random.seed(SEED)
 torch.manual_seed(SEED)
 torch.cuda.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True
print("Predicting BERT large model...... ")
 bert_config = BertConfig(JIGSAW_BERT_LARGE_JSON_PATH)
 tokenizer = BertTokenizer.from_pretrained(BERT_LARGE_PATH, cache_dir=None,do_lower_case=True)
 X_test = convert_lines(test_df["comment_text"].fillna("DUMMY_VALUE"), MAX_SEQUENCE_LENGTH, tokenizer)
 test = torch.utils.data.TensorDataset(torch.tensor(X_test, dtype=torch.long))
gc.collect()
model = BertForSequenceClassification(bert_config, num_labels=1)
 model.load_state_dict(torch.load(JIGSAW_BERT_LARGE_MODEL_PATH))
model.to(device)
 for param in model.parameters() :
param.requires_grad = False
model.eval()
model_preds = np.zeros(( len(X_test)))
 test_loader = torch.utils.data.DataLoader(test, batch_size=INFER_BATCH_SIZE, shuffle=False)
 tk0 = tqdm(test_loader)
 for i,(x_batch,)in enumerate(tk0):
pred = model(x_batch.to(device), attention_mask=(x_batch > 0 ).to(device), labels=None)
 model_preds[i * INFER_BATCH_SIZE:(i + 1)* INFER_BATCH_SIZE] = pred[:, 0].detach().cpu().squeeze().numpy()
test_preds[:,0] = torch.sigmoid(torch.tensor(model_preds)).numpy().ravel()
del model
gc.collect()
print("Predicting BERT small model...... ")
 bert_config = BertConfig(JIGSAW_BERT_SMALL_JSON_PATH)
 tokenizer = BertTokenizer.from_pretrained(BERT_SMALL_PATH, cache_dir=None,do_lower_case=True)
 X_test = convert_lines(test_df["comment_text"].fillna("DUMMY_VALUE"), MAX_SEQUENCE_LENGTH, tokenizer)
 test = torch.utils.data.TensorDataset(torch.tensor(X_test, dtype=torch.long))
model = BertForSequenceClassification(bert_config, num_labels=1)
 model.load_state_dict(torch.load(JIGSAW_BERT_SMALL_MODEL_PATH))
model.to(device)
 for param in model.parameters() :
param.requires_grad = False
model.eval()
model_preds = np.zeros(( len(X_test)))
 test_loader = torch.utils.data.DataLoader(test, batch_size=INFER_BATCH_SIZE, shuffle=False)
 tk0 = tqdm(test_loader)
 for i,(x_batch,)in enumerate(tk0):
pred = model(x_batch.to(device), attention_mask=(x_batch > 0 ).to(device), labels=None)
 model_preds[i * INFER_BATCH_SIZE:(i + 1)* INFER_BATCH_SIZE] = pred[:, 0].detach().cpu().squeeze().numpy()
test_preds[:,1] = torch.sigmoid(torch.tensor(model_preds)).numpy().ravel()
del model
gc.collect()
bert_submission = pd.DataFrame.from_dict({
'id': test_df['id'],
'prediction': test_preds.mean(axis=1)})
 bert_submission.to_csv('bert_submission.csv', index=False)
<define_variables>
fill_na_num_0(features,'BsmtFullBath')
 fill_na_num_0(features,'BsmtHalfBath' )
EMBEDDING_PATHS = ['.. /input/pickled-word-embedding/crawl-300d-2M.pkl',
'.. /input/pickled-word-embedding/glove.840B.300d.pkl']
NUM_MODELS = 2
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4*LSTM_UNITS
EPOCHS = 3
MAX_LEN = 220
IDENTITY_COLUMNS = [
'transgender', 'female', 'homosexual_gay_or_lesbian', 'muslim', 'hindu',
'white', 'black', 'psychiatric_or_mental_illness', 'jewish'
]
AUX_COLUMNS = ['target', 'severe_toxicity','obscene','identity_attack','insult','threat']
TEXT_COLUMN = 'comment_text'
TARGET_COLUMN = 'target'<define_variables>
features.drop('GarageYrBlt', inplace = True, axis = 1)
 features.drop(['Utilities'], axis=1, inplace = True)
 fill_na_cat_none(features,'GarageType')
 fill_na_cat_none(features,'GarageFinish')
 fill_na_cat_none(features,'GarageQual')
 fill_na_cat_none(features,'GarageCond')
 fill_na_num_0(features,'GarageCars')
 fill_na_num_0(features,'GarageArea' )
SYMBOLS_TO_ISOLATE = '.,?!-;*"…:—() %
SYMBOLS_TO_REMOVE = '
🍕\r🐵\xa0\ue014\t\uf818\uf04a\xad😢🐶️\uf0e0😜😎👊\u200b\u200e😁عدويهصقأناخلىبمغر😍💖💵Е👎😀😂\u202a\u202c🔥😄🏻💥ᴍʏʀᴇɴᴅᴏᴀᴋʜᴜʟᴛᴄᴘʙғᴊᴡɢ😋👏שלוםבי😱‼\x81エンジ故障\u2009🚌ᴵ͞🌟😊😳😧🙀😐😕\u200f👍😮😃😘אעכח💩💯⛽🚄🏼ஜ😖ᴠ🚲‐😟😈💪🙏🎯🌹😇💔😡\x7f👌ἐὶήιὲκἀίῃἴξ🙄Ｈ😠\ufeff\u2028😉😤⛺🙂\u3000تحكسة👮💙فزط😏🍾🎉😞\u2008🏾😅😭👻😥😔😓🏽🎆🍻🍽🎶🌺🤔😪\x08‑🐰🐇🐱🙆😨🙃💕𝘊𝘦𝘳𝘢𝘵𝘰𝘤𝘺𝘴𝘪𝘧𝘮𝘣💗💚地獄谷улкнПоАН🐾🐕😆ה🔗🚽歌舞伎🙈😴🏿🤗🇺🇸мυтѕ⤵🏆🎃😩\u200a🌠🐟💫💰💎эпрд\x95🖐🙅⛲🍰🤐👆🙌\u2002💛🙁👀🙊🙉\u2004ˢᵒʳʸᴼᴷᴺʷᵗʰᵉᵘ\x13🚬🤓\ue602😵άοόςέὸתמדףנרךצט😒͝🆕👅👥👄🔄🔤👉👤👶👲🔛🎓\uf0b7\uf04c\x9f\x10成都😣⏺😌🤑🌏😯ех😲Ἰᾶὁ💞🚓🔔📚🏀👐\u202d💤🍇\ue613小土豆🏡❔⁉\u202f👠》कर्मा🇹🇼🌸蔡英文🌞🎲レクサス😛外国人关系Сб💋💀🎄💜🤢َِьыгя不是\x9c\x9d🗑\u2005💃📣👿༼つ༽😰ḷЗз▱ц￼🤣卖温哥华议会下降你失去所有的钱加拿大坏税骗子🐝ツ🎅\x85🍺آإشء🎵🌎͟ἔ油别克🤡🤥😬🤧й\u2003🚀🤴ʲшчИОРФДЯМюж😝🖑ὐύύ特殊作戦群щ💨圆明园קℐ🏈😺🌍⏏ệ🍔🐮🍁🍆🍑🌮🌯🤦\u200d𝓒𝓲𝓿𝓵안영하세요ЖљКћ🍀😫🤤ῦ我出生在了可以说普通话汉语好极🎼🕺🍸🥂🗽🎇🎊🆘🤠👩🖒🚪天一家⚲\u2006⚭⚆⬭⬯⏖新✀╌🇫🇷🇩🇪🇮🇬🇧😷🇨🇦ХШ🌐\x1f杀鸡给猴看ʁ𝗪𝗵𝗲𝗻𝘆𝗼𝘂𝗿𝗮𝗹𝗶𝘇𝗯𝘁𝗰𝘀𝘅𝗽𝘄𝗱📺ϖ\u2000үսᴦᎥһͺ\u2007հ\u2001ɩｙｅ൦ｌƽｈ𝐓𝐡𝐞𝐫𝐮𝐝𝐚𝐃𝐜𝐩𝐭𝐢𝐨𝐧Ƅᴨןᑯ໐ΤᏧ௦Іᴑ܁𝐬𝐰𝐲𝐛𝐦𝐯𝐑𝐙𝐣𝐇𝐂𝐘𝟎ԜТᗞ౦〔Ꭻ𝐳𝐔𝐱𝟔𝟓𝐅🐋ﬃ💘💓ё𝘥𝘯𝘶💐🌋🌄🌅𝙬𝙖𝙨𝙤𝙣𝙡𝙮𝙘𝙠𝙚𝙙𝙜𝙧𝙥𝙩𝙪𝙗𝙞𝙝𝙛👺🐷ℋ𝐀𝐥𝐪🚶𝙢Ἱ🤘ͦ💸ج패티Ｗ𝙇ᵻ👂👃ɜ🎫\uf0a7БУі🚢🚂ગુજરાતીῆ🏃𝓬𝓻𝓴𝓮𝓽𝓼☘﴾̯﴿₽\ue807𝑻𝒆𝒍𝒕𝒉𝒓𝒖𝒂𝒏𝒅𝒔𝒎𝒗𝒊👽😙\u200cЛ‒🎾👹⎌🏒⛸公寓养宠物吗🏄🐀🚑🤷操美𝒑𝒚𝒐𝑴🤙🐒欢迎来到阿拉斯ספ𝙫🐈𝒌𝙊𝙭𝙆𝙋𝙍𝘼𝙅ﷻ🦄巨收赢得白鬼愤怒要买额ẽ🚗🐳𝟏𝐟𝟖𝟑𝟕𝒄𝟗𝐠𝙄𝙃👇锟斤拷𝗢𝟳𝟱𝟬⦁マルハニチロ株式社⛷한국어ㄸㅓ니͜ʖ𝘿𝙔₵𝒩ℯ𝒾𝓁𝒶𝓉𝓇𝓊𝓃𝓈𝓅ℴ𝒻𝒽𝓀𝓌𝒸𝓎𝙏ζ𝙟𝘃𝗺𝟮𝟭𝟯𝟲👋🦊多伦🐽🎻🎹⛓🏹🍷🦆为和中友谊祝贺与其想象对法如直接问用自己猜本传教士没积唯认识基督徒曾经让相信耶稣复活死怪他但当们聊些政治题时候战胜因圣把全堂结婚孩恐惧且栗谓这样还♾🎸🤕🤒⛑🎁批判检讨🏝🦁🙋😶쥐스탱트뤼도석유가격인상이경제황을렵게만들지않록잘관리해야합다캐나에서대마초와화약금의품런성분갈때는반드시허된사용🔫👁凸ὰ💲🗯𝙈Ἄ𝒇𝒈𝒘𝒃𝑬𝑶𝕾𝖙𝖗𝖆𝖎𝖌𝖍𝖕𝖊𝖔𝖑𝖉𝖓𝖐𝖜𝖞𝖚𝖇𝕿𝖘𝖄𝖛𝖒𝖋𝖂𝕴𝖟𝖈𝕸👑🚿💡知彼百\uf005𝙀𝒛𝑲𝑳𝑾𝒋𝟒😦𝙒𝘾𝘽🏐𝘩𝘨ὼṑ𝑱𝑹𝑫𝑵𝑪🇰🇵👾ᓇᒧᔭᐃᐧᐦᑳᐨᓃᓂᑲᐸᑭᑎᓀᐣ🐄🎈🔨🐎🤞🐸💟🎰🌝🛳点击查版🍭𝑥𝑦𝑧ＮＧ👣\uf020っ🏉ф💭🎥Ξ🐴👨🤳🦍\x0b🍩𝑯𝒒😗𝟐🏂👳🍗🕉🐲چی𝑮𝗕𝗴🍒ꜥⲣⲏ🐑⏰鉄リ事件ї💊「」\uf203\uf09a\uf222\ue608\uf202\uf099\uf469\ue607\uf410\ue600燻製シ虚偽屁理屈Г𝑩𝑰𝒀𝑺🌤𝗳𝗜𝗙𝗦𝗧🍊ὺἈἡχῖΛ⤏🇳𝒙ψՁմեռայինրւդձ冬至ὀ𝒁🔹🤚🍎𝑷🐂💅𝘬𝘱𝘸𝘷𝘐𝘭𝘓𝘖𝘹𝘲𝘫کΒώ💢ΜΟΝΑΕ🇱♲𝝈↴💒⊘Ȼ🚴🖕🖤🥘📍👈➕🚫🎨🌑🐻𝐎𝐍𝐊𝑭🤖🎎😼🕷ｇｒｎｔｉｄｕｆｂｋ𝟰🇴🇭🇻🇲𝗞𝗭𝗘𝗤👼📉🍟🍦🌈🔭《🐊🐍\uf10aლڡ🐦\U0001f92f\U0001f92a🐡💳ἱ🙇𝗸𝗟𝗠𝗷🥜さようなら🔼'
ISOLATE_DICT = {ord(c):f' {c} ' for c in SYMBOLS_TO_ISOLATE}
REMOVE_DICT = {ord(c):f'' for c in SYMBOLS_TO_REMOVE}
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'
CONTRACTION_MAPPING = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "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", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have" }
def handle_punctuation(text):
text = text.translate(REMOVE_DICT)
 text = text.translate(ISOLATE_DICT)
 return text
def clean_contractions(text, mapping=CONTRACTION_MAPPING):

 specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text
def preprocess(x):
x = handle_punctuation(x)
 return x
def get_coefs(word, *arr):

 return word, np.asarray(arr,dtype='float32')
 def load_embeddings(path):
if path.split('.')[-1] in ['txt','vec']:
with open(path,'rb')as f:
return dict(get_coefs(*line.strip().split(' ')) for line in f)
 if path.split('.')[-1] =='pkl':
with open(path,'rb')as f:
return pickle.load(f)
 def build_matrix(word_index, path):

 embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+1, 300))
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
pass
return embedding_matrix<set_options>
new_numerical_features = features.dtypes[features.dtypes != "object"].index
new_categorical_features = features.dtypes[features.dtypes == "object"].index
new_num_df = features[new_numerical_features]
new_cat_df = features[new_categorical_features]
features[['BsmtQual','BsmtCond','BsmtExposure','TotalBsmtSF']].head()
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(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
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
def build_model(embedding_matrix, num_aux_targets):

 words = Input(shape=(MAX_LEN,))
x = Embedding(*embedding_matrix.shape, weights = [embedding_matrix], trainable=False )(words)
 x = SpatialDropout1D(0.1 )(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 hidden = concatenate([
GlobalMaxPooling1D()(x),
GlobalAveragePooling1D()(x)
 ])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 aux_result =Dense(num_aux_targets, activation='sigmoid' )(hidden)
 model = Model(inputs =words, outputs =[result, aux_result])
 model.compile(loss='binary_crossentropy', optimizer='adam')
 return model<prepare_x_and_y>
fill_na_cat_none(features,'BsmtExposure')
 fill_na_cat_none(features,'BsmtCond')
 fill_na_cat_none(features,'BsmtQual')
 fill_na_mode(features,'MSZoning')
 fill_na_mode(features,'SaleType')
 fill_na_mode(features,'KitchenQual')
 fill_na_mode(features,'Electrical')
 fill_na_mode(features,'Exterior1st')
 fill_na_mode(features,'Exterior2nd')
 fill_na_mode(features,'Functional')
 features[['FireplaceQu','Fireplaces']].head()
x_train = train_df[TEXT_COLUMN].astype(str)
 y_train = train_df[TARGET_COLUMN].values
y_aux_train = train_df[AUX_COLUMNS].values
x_test = test_df[TEXT_COLUMN].astype(str)
 for column in IDENTITY_COLUMNS + [TARGET_COLUMN]:
train_df[column] = np.where(train_df[column] >=0.5, True, False )<string_transform>
fill_na_cat_none(features,'FireplaceQu' )
tokenizer = text.Tokenizer(filters = CHARS_TO_REMOVE)
 tokenizer.fit_on_texts(list(x_train)+ list(x_test))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train,maxlen=MAX_LEN)
 x_test= sequence.pad_sequences(x_test, maxlen=MAX_LEN )<define_variables>
features['LandContour'] = features['LandContour'].replace(dict(Lvl=4, Bnk=3, HLS=2, Low=1))
features['LandSlope'] = features['LandSlope'].replace(dict(Gtl=3, Mod=2, Sev=1))
features['ExterQual'] =features['ExterQual'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['ExterCond'] =features['ExterCond'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['BsmtQual'] = features['BsmtQual'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['BsmtCond'] =features['BsmtCond'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['BsmtCond'] =features['BsmtCond'].replace('None',0)
 features['BsmtExposure'] =features['BsmtExposure'].replace(dict(Gd=4, Av=3, Mn=2, No=1))
features['BsmtExposure'] =features['BsmtExposure'].replace('None',0)
 features['BsmtFinType1'] = features['BsmtFinType1'].replace(dict(GLQ=6, ALQ=5, BLQ=4, Rec=3, LwQ=2, Unf=1))
features['BsmtFinType1'] =features['BsmtFinType1'].replace('None',0)
 features['BsmtFinType2'] = features['BsmtFinType2'].replace(dict(GLQ=6, ALQ=5, BLQ=4, Rec=3, LwQ=2, Unf=1))
features['BsmtFinType2'] = features['BsmtFinType2'].replace('None',0)
 features['HeatingQC'] = features['HeatingQC'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['CentralAir'] = features['CentralAir'].replace(dict(Y=1, N=0))
features['KitchenQual'] =features['KitchenQual'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['Functional'] = features['Functional'].replace(dict(Typ=8, Min1=7, Min2=6, Mod=5, Maj1=4, Maj2=3, Sev=2, Sal=1))
features['FireplaceQu'] = features['FireplaceQu'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['FireplaceQu'] = features['FireplaceQu'].replace('None', 0)
 features['GarageQual'] = features['GarageQual'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['GarageQual'] =features['GarageQual'].replace('None', 0)
 features['GarageCond'] = features['GarageCond'].replace(dict(Ex=5, Gd=4, TA=3, Fa=2, Po=1))
features['GarageCond'] = features['GarageCond'].replace('None', 0)
 features['LotShape'] = features['LotShape'].replace(dict(Reg=4, IR1=3, IR2=2, IR3=1))
features['GarageFinish'] = features['GarageFinish'].replace(dict(Fin=3, RFn=2, Unf=1))
features['GarageFinish'] = features['GarageFinish'].replace('None', 0)
 features['PavedDrive'] =features['PavedDrive'].replace(dict(Y=3, P=2, N=1))
features =features.astype({"Functional": int} )
sample_weights = np.ones(len(x_train), dtype=np.float32)
 sample_weights += train_df[IDENTITY_COLUMNS].sum(axis=1)
 sample_weights += train_df[TARGET_COLUMN]*(~train_df[IDENTITY_COLUMNS] ).sum(axis=1)
 sample_weights +=(~train_df[TARGET_COLUMN])*train_df[IDENTITY_COLUMNS].sum(axis=1)
 sample_weights/=sample_weights.mean()<train_model>
features['BsmtQual'] =features['BsmtQual'].replace('None',0)
 features =features.astype({"KitchenQual": int})
 features =features.astype({"Functional": int})
embedding_matrix = np.concatenate([build_matrix(tokenizer.word_index,f)for f in EMBEDDING_PATHS], axis =-1)
 print("Embedding matrix shape:", embedding_matrix.shape)
 del train_df, tokenizer
gc.collect()<train_model>
features['MSSubClass'] =features['MSSubClass'].astype('category')
 features['MoSold'] = features['MoSold'].astype('category')
 features['YrSold'] =features['YrSold'].astype('category')
 cat_cols = ['MSZoning','Street','MSSubClass', 'MoSold', 'YrSold', 'LotConfig', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Foundation', 'Heating', 'Electrical', 'GarageType', 'SaleType', 'SaleCondition']
dumies=pd.get_dummies(features[cat_cols],drop_first=True)
 dumies.head()
checkpoint_predictions = []
weights = []
NUM_MODELS = 1
for model_idx in range(NUM_MODELS):
model = build_model(embedding_matrix, y_aux_train.shape[-1])
 for global_epoch in range(EPOCHS):
model.fit(
 x_train,
[y_train, y_aux_train],
batch_size=BATCH_SIZE,
epochs=1,
verbose=1,
sample_weight=[sample_weights.values, np.ones_like(sample_weights)],
callbacks = [
LearningRateScheduler(lambda _: 1e-3*(0.55**global_epoch))
] 
)
 checkpoint_predictions.append(model.predict(x_test, batch_size=2048)[0].flatten())
 weights.append(2 ** global_epoch)
 del model
gc.collect()<save_to_csv>
for column in cat_cols:
features.drop([column],axis=1,inplace=True )
predictions = np.average(checkpoint_predictions, weights=weights, axis=0)
 lstm_submission = pd.DataFrame.from_dict({
'id': test_df.id,
'prediction': predictions
})
 lstm_submission.to_csv('submission.csv', index=False )<save_to_csv>
features=pd.concat([ features,dumies],axis=1)
 features.head()
submission = pd.DataFrame.from_dict({
'id': test_df['id'],
'prediction': lstm_submission['prediction'].rank(pct=True)*0.3 + bert_submission['prediction'].rank(pct=True)*0.7})
 submission.to_csv('submission.csv', index=False)
<set_options>
new_train = features.iloc[:1458, :]
new_test = features.iloc[1458:,:]
X_test = new_test
X_train = new_train
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)
%reload_ext autoreload
%autoreload 2
%matplotlib inline<install_modules>
X_standardize = pd.DataFrame(X_scaled, columns = X_train.columns)
 X_standardize.head()
%%time
! pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext".. /input/nvidiaapex/repository/NVIDIA-apex-39e153a<define_variables>
ridge = Ridge()
ridge.fit(X_train, y_log)
 y_sale = ridge.predict(X_test)
 y_predict_ridge = np.expm1(y_sale)
 submission = pd.DataFrame(y_predict_ridge)
 sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=submission[0]
sample.to_csv('Ridge.csv',index = False )
package_dir_a = ".. /input/ppbert/pytorch-pretrained-bert/pytorch-pretrained-BERT"
sys.path.insert(0, package_dir_a )<import_modules>
y_train = ridge.predict(X_train )
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import pandas as pd
from typing import *
from pathlib import Path
import torch
import torch.optim as optim
from fastai import *
from fastai.text import *
from fastai.vision import *
from fastai.callbacks import *
from sklearn.model_selection import train_test_split<set_options>
from sklearn.linear_model import Lasso,ElasticNet
from sklearn import metrics
tqdm.pandas(desc="my bar!")
 warnings.simplefilter('ignore')
 warnings.filterwarnings('ignore' )<define_variables>
lasso = Lasso(alpha=0.01)
 lasso.fit(X_train, y_log)
 y_lasso_pred = lasso.predict(X_test)
 lasso_predict = np.expm1(y_lasso_pred)
 submission = pd.DataFrame(lasso_predict)
 sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=submission[0]
sample.to_csv('Lasso.csv',index = False )
BERT_MODEL_PATH = '.. /input/bert-pretrained-models/uncased_l-12_h-768_a-12/uncased_L-12_H-768_A-12/'
WORK_DIR = '.. /working/'
convert_tf_checkpoint_to_pytorch.convert_tf_checkpoint_to_pytorch(BERT_MODEL_PATH + 'bert_model.ckpt',
BERT_MODEL_PATH + 'bert_config.json',
WORK_DIR + 'pytorch_model.bin')
 shutil.copyfile(BERT_MODEL_PATH + 'bert_config.json', WORK_DIR + 'bert_config.json' )<set_options>
y_lasso_train = lasso.predict(X_train )
warnings.filterwarnings(action='once')
 device = torch.device('cuda')
 MAX_SEQUENCE_LENGTH = 300
SEED = 620402
BATCH_SIZE = 16
BERT_MODEL_BIN = '.. /working'
bert_config = BertConfig(BERT_MODEL_PATH + 'bert_config.json')
 np.random.seed(SEED)
 torch.manual_seed(SEED)
 torch.cuda.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True<load_pretrained>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
ENet.fit(X_train, y_log)
 ENet_train_pred = ENet.predict(X_train)
 ENet_pred = np.expm1(ENet.predict(X_test))
sub = pd.DataFrame(ENet_pred)
 sub.head()
sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=sub[0]
sample.to_csv('Enet.csv',index = False)
bert_tokenizer = BertTokenizer.from_pretrained(BERT_MODEL_PATH, cache_dir=None, do_lower_case=True )<string_transform>
ridge.score(X_train,y_log )
class FastAiBertTokenizer(BaseTokenizer):

 def __init__(self, tokenizer: BertTokenizer, max_seq_len: int=300, **kwargs):
self._pretrained_tokenizer = tokenizer
self.max_seq_len = max_seq_len
def __call__(self, *args, **kwargs):
return self
def tokenizer(self, t:str)-> List[str]:

 return ["[CLS]"] + self._pretrained_tokenizer.tokenize(t)[:self.max_seq_len - 2] + ["[SEP]"]<choose_model_class>
ENet.score(X_train,y_log )
fastai_tokenizer = Tokenizer(
 tok_func = FastAiBertTokenizer(bert_tokenizer, max_seq_len = MAX_SEQUENCE_LENGTH),
pre_rules=[],
post_rules=[]
)<define_variables>
lasso.score(X_train,y_log )
fastai_bert_vocab = Vocab(list(bert_tokenizer.vocab.keys()))<load_from_csv>
print('Ridge MSE: ',metrics.mean_squared_error(y_log,y_train))
%%time
test_df = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv" )<define_variables>
print('Enet MSE: ',metrics.mean_squared_error(y_log,ENet_train_pred))
<categorify><EOS> 
print('Lasso MSE: ',metrics.mean_squared_error(y_log,y_lasso_train))