Kaggle competition: Tweet Sentiment Extraction
final_test=(0.25*vote_pred+0.35*stacking_pred+ 0.2*y_lasso_test + lgb_pred*0.2)
 print(f'Root Mean Square Error test= {score(final_test)}')
<predict_on_test>
train_df = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 test_df = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 train_df.head()
vote_test = vote_mod.predict(X_test)
 final1=np.expm1(vote_test)
 stack_test = stregr.predict(X_test)
 final2=np.expm1(stack_test)
 lasso_test = lasso_mod.predict(X_test)
 final3=np.expm1(lasso_test)
<save_to_csv>
print(train_df[train_df['text'].isnull() ])
 print(train_df[train_df['selected_text'].isnull() ] )
final=(0.2*final1+0.6*final2+0.2*final3)
 final_submission = pd.DataFrame({
"Id": b["Id"],
"SalePrice": final
})
 final_submission.to_csv("final_submission.csv", index=False)
 final_submission.head()<set_options>
train_df.dropna(inplace = True)
 train_df = train_df.reset_index(drop = True )
sns.set_style('whitegrid')
 warnings.simplefilter(action='ignore')
<load_from_csv>
train_df['N_text_words'] = train_df['text'].apply(lambda tweet : len(tweet.split()))
train_df['N_selected_text_words'] = train_df['selected_text'].apply(lambda tweet : len(tweet.split()))
train_df['N_words_difference'] = train_df['N_text_words'] - train_df['N_selected_text_words']
a = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 b = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<set_options>
print("There are {0} unique sentiments having values {1}".format(train_df['sentiment'].nunique() , train_df['sentiment'].unique()))
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None )<train_model>
n_neutral = train_df['sentiment'].loc[train_df['sentiment'] == 'neutral'].count()
n_positive = train_df['sentiment'].loc[train_df['sentiment'] == 'positive'].count()
n_negative = train_df['sentiment'].loc[train_df['sentiment'] == 'negative'].count()
print('The shape of our training set: %s houses and %s features'%(a.shape[0],a.shape[1]))
print('The shape of our training set: %s houses and %s features'%(b.shape[0],b.shape[1]))
print('The testing set has 1 feature less than the training set, which is SalePrice, the target to predict ' )<prepare_x_and_y>
print(f"Neutral tweets : {n_neutral}")
 print(f"Positive tweets : {n_positive}")
 print(f"Negative tweets : {n_negative}" )
na = a.shape[0]
nb = b.shape[0]
y_train = a['SalePrice'].to_frame()
c1 = pd.concat(( a, b), sort=False ).reset_index(drop=True)
 c1.drop(['SalePrice'], axis=1, inplace=True)
 c1.drop(['Id'], axis=1, inplace=True)
 print(f"Total size is {c1.shape}" )<compute_test_metric>
df_neutral = train_df.loc[train_df['sentiment'] == 'neutral']
df_positive = train_df.loc[train_df['sentiment'] == 'positive']
df_negative = train_df.loc[train_df['sentiment'] == 'negative']
msv1(c1, 20, color=sns.color_palette('Reds',15))<drop_column>
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
c=c1.dropna(thresh=len(c1)*0.8, axis=1)
 print(f"We dropped {c1.shape[1]-c.shape[1]} features in the combined set" )<data_type_conversions>
train_df['Jaccard_score'] = jaccard_score
NA=c[allna.index.to_list() ]<data_type_conversions>
def clean_text(text):

 text = str(text ).lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 text = re.sub('\w*\d\w*', '', text)
 return text
c['MasVnrArea']=c.MasVnrArea.fillna(0)
 c['LotFrontage']=c.LotFrontage.fillna(c.LotFrontage.median())
 c['GarageYrBlt']=c["GarageYrBlt"].fillna(1980 )<categorify>
train_df['text_cleaned'] = train_df['text'].apply(lambda x:clean_text(x))
train_df['selected_text_cleaned'] = train_df['selected_text'].apply(lambda x:clean_text(x))
ffill_cols = ['Electrical', 'SaleType', 'KitchenQual', 'Exterior1st',
'Exterior2nd', 'Functional', 'Utilities', 'MSZoning']
def filling_NA(data, columns, METHOD='ffill'):
fill_cols = columns
for col in data[fill_cols]:
data[col]= data[col].fillna(method=METHOD)
 return data<compute_test_metric>
STOPWORDS = stopwords.words('english')
 def remove_stopwords(text):
return [word for word in text.split() if word not in STOPWORDS]
msv2(c )<concatenate>
train_df['text_cleaned'] = train_df['text_cleaned'].apply(lambda x : remove_stopwords(x))
train_df['selected_text_cleaned'] = train_df['selected_text_cleaned'].apply(lambda x : remove_stopwords(x))
d=filling_NA(c, ffill_cols )<data_type_conversions>
def get_all_words(df_col):
all_words_text = []
for row in df_col:
for word in row:
all_words_text.append(word)
 return all_words_text
all_words_text = get_all_words(train_df['text_cleaned'])
 all_words_selected_text = get_all_words(train_df['selected_text_cleaned'] )
fill_cols = ['Electrical', 'SaleType', 'KitchenQual', 'Exterior1st',
'Exterior2nd', 'Functional', 'Utilities', 'MSZoning']
for col in c[fill_cols]:
c[col] = c[col].fillna(method='ffill' )<data_type_conversions>
def most_common_words(all_words):
return nltk.FreqDist(all_words )
NAcols=c.columns
for col in NAcols:
if c[col].dtype == "object":
c[col] = c[col].fillna("None" )<data_type_conversions>
all_words_neutral = get_all_words(train_df[train_df['sentiment'] == 'neutral']['text_cleaned'])
 all_words_positive = get_all_words(train_df[train_df['sentiment'] == 'positive']['text_cleaned'])
 all_words_negative = get_all_words(train_df[train_df['sentiment'] == 'negative']['text_cleaned'] )
for col in NAcols:
if c[col].dtype != "object":
c[col]= c[col].fillna(0 )<feature_engineering>
c['TotalArea'] = c['TotalBsmtSF'] + c['1stFlrSF'] + c['2ndFlrSF'] + c['GrLivArea'] +c['GarageArea']
c['Bathrooms'] = c['FullBath'] + c['HalfBath']*0.5
c['Year average']=(c['YearRemodAdd']+c['YearBuilt'])/2<data_type_conversions>
c['MSSubClass'] = c['MSSubClass'].apply(str)
 c['YrSold'] = c['YrSold'].astype(str )<categorify>
cb=pd.get_dummies(c)
 print(f"the shape of the original dataset {c.shape}")
 print(f"the shape of the encoded dataset {cb.shape}")
 print(f"We have {cb.shape[1]- c.shape[1]} new encoded features" )<split>
print('TF version',tf.__version__ )
Train = cb[:na]
Test = cb[na:]<sort_values>
MAX_LEN = 96
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 train.head()
a['GrLivArea'].sort_values(ascending=False ).head(2 )<sort_values>
ct = train.shape[0]
input_ids = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids = np.zeros(( ct,MAX_LEN),dtype='int32')
 start_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 end_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(train.shape[0]):
text1 = " "+" ".join(train.loc[k,'text'].split())
 text2 = " ".join(train.loc[k,'selected_text'].split())
 idx = text1.find(text2)
 chars = np.zeros(( len(text1)))
 chars[idx:idx+len(text2)]=1
if text1[idx-1]==' ': chars[idx-1] = 1
enc = tokenizer.encode(text1)
 offsets = []; idx=0
for t in enc.ids:
w = tokenizer.decode([t])
 offsets.append(( idx,idx+len(w)))
 idx += len(w)
 toks = []
for i,(a,b)in enumerate(offsets):
sm = np.sum(chars[a:b])
 if sm>0: toks.append(i)
 s_tok = sentiment_id[train.loc[k,'sentiment']]
input_ids[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask[k,:len(enc.ids)+5] = 1
if len(toks)>0:
start_tokens[k,toks[0]+1] = 1
end_tokens[k,toks[-1]+1] = 1
a['TotalBsmtSF'].sort_values(ascending=False ).head(1 )<sort_values>
test = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 ct = test.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test.shape[0]):
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k,:len(enc.ids)+5] = 1
a['MasVnrArea'].sort_values(ascending=False ).head(1 )<sort_values>
def build_model() :
ids = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 att = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 config = RobertaConfig.from_pretrained(PATH+'config-roberta-base.json')
 bert_model = TFRobertaModel.from_pretrained(PATH+'pretrained-roberta-base.h5',config=config)
 x = bert_model(ids,attention_mask=att,token_type_ids=tok)
 x1 = tf.keras.layers.Dropout(0.1 )(x[0])
 x1 = tf.keras.layers.Conv1D(1,1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.1 )(x[0])
 x2 = tf.keras.layers.Conv1D(1,1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer)
 return model
a['1stFlrSF'].sort_values(ascending=False ).head(1 )<sort_values>
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 if(len(a)==0)&(len(b)==0): return 0.5
c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
a['TotRmsAbvGrd'].sort_values(ascending=False ).head(1 )<drop_column>
train=Train[(Train['GrLivArea'] < 4600)&(Train['MasVnrArea'] < 1500)]
print(f'We removed {Train.shape[0]- train.shape[0]} outliers' )<prepare_x_and_y>
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
DISPLAY=1
n_splits = 5
for i in range(5):
print('
print('
print('
K.clear_session()
model = build_model()
model.load_weights('.. /input/tf-roberta-weights/v0-roberta-%i.h5'%i)
 print('Predicting Test...')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=DISPLAY)
 preds_start += preds[0]/n_splits
preds_end += preds[1]/n_splits
target=a[['SalePrice']]
target.loc[1298]<filter>
all = []
for k in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[k,])
 b = np.argmax(preds_end[k,])
 if a>b:
st = test.loc[k,'text']
else:
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 all.append(st )
<drop_column><EOS> 
test['selected_text'] = all
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )