Kaggle competition: Tweet Sentiment Extraction
train.isnull().sum()<drop_column>
df=pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')
 df.head()
train.drop(["PoolQC","PavedDrive",'Fence','MiscFeature','FireplaceQu','GarageFinish', 'Alley'], inplace = True, axis = 1)
 test.drop(["PoolQC","PavedDrive",'Fence','MiscFeature','FireplaceQu','GarageFinish' , 'Alley' ], inplace = True, axis = 1 )<categorify>
def missing_value_of_data(data):
total = data.isnull().sum().sort_values(ascending=False)
 precent=round(total/data.shape[0]*100,2)
 return pd.concat([total,precent],axis=1,keys=['Total','Percent'] )
train = pd.get_dummies(train)
 test = pd.get_dummies(test )<prepare_x_and_y>
missing_value_of_data(df )
y = train['SalePrice']
train = train.drop(["SalePrice"], axis = 1 )<drop_column>
df.dropna(inplace=True )
train.drop(missing_cols , inplace = True, axis = 1 )<data_type_conversions>
def count_values_in_columns(data,feature):
total=data.loc[:,feature].value_counts()
percentage = round(data.loc[:,feature].value_counts(normalize=True)*100,2)
 return pd.concat([total,percentage],axis=1,keys=['Total','Percentage'] )
mean = train.mean().astype(np.int32)
 train.fillna(mean , inplace = True)
 mean = test.mean().astype(np.int32)
 test.fillna(mean , inplace = True )<count_missing_values>
count_values_in_columns(df,'sentiment' )
train.isnull().sum()<count_missing_values>
def duplicated_values_data(data):
dup=[]
columns=data.columns
for i in columns:
dup.append(sum(data[i].duplicated()))
return pd.concat([pd.Series(columns),pd.Series(dup)],axis=1,keys=['Columns','Duplicate count'] )
train.isnull().sum()<feature_engineering>
duplicated_values_data(df )
train['LotFA'] = train['LotArea'] + train['LotFrontage']
train['LotFA2'] = train['LotArea'] - train['LotFrontage']
train['OverallRate'] = train['OverallQual'] + train['OverallCond']
train['OverallRate2'] = train['OverallQual'] - train['OverallCond']
train['yearAvg'] = train['YearBuilt'] + train['YearRemodAdd']
train['yearAvg2'] = train['YearBuilt'] - train['YearRemodAdd']
train['fsFeet'] = train['1stFlrSF'] + train['2ndFlrSF']
train['fsFeet2'] = train['1stFlrSF'] - train['2ndFlrSF']
train['bath'] = train['BsmtFullBath'] + train['BsmtHalfBath']
train['bath2'] = train['BsmtFullBath'] - train['BsmtHalfBath']
train['areaPerCar'] = train['GarageArea'] / train['GarageCars']
train['OpenEnclosedPorch'] = train['OpenPorchSF'] + train['EnclosedPorch']
train['OpenEnclosedPorch2'] = train['OpenPorchSF'] - train['EnclosedPorch']
train['yearSANEG'] =(train['YrSold']**2 - train['yearAvg']**2)**0.5
train['yearSAPOS'] =(train['YrSold']**2 + train['yearAvg']**2)**0.5
test['LotFA'] = test['LotArea'] + test['LotFrontage']
test['LotFA2'] = test['LotArea'] - test['LotFrontage']
test['OverallRate'] = test['OverallQual'] + test['OverallCond']
test['OverallRate2'] = test['OverallQual'] - test['OverallCond']
test['yearAvg'] = test['YearBuilt'] + test['YearRemodAdd']
test['yearAvg2'] = test['YearBuilt'] - test['YearRemodAdd']
test['fsFeet'] = test['1stFlrSF'] + test['2ndFlrSF']
test['fsFeet2'] = test['1stFlrSF'] - test['2ndFlrSF']
test['bath'] = test['BsmtFullBath'] + test['BsmtHalfBath']
test['bath2'] = test['BsmtFullBath'] - test['BsmtHalfBath']
test['areaPerCar'] = test['GarageArea'] / test['GarageCars']
test['OpenEnclosedPorch'] = test['OpenPorchSF'] + test['EnclosedPorch']
test['OpenEnclosedPorch2'] = test['OpenPorchSF'] - test['EnclosedPorch']
test['yearSANEG'] =(test['YrSold']**2 - test['yearAvg']**2)**0.5
test['yearSAPOS'] =(test['YrSold']**2 + test['yearAvg']**2)**0.5<train_model>
def find_url(string):
try:
text = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +',string)
 except:
text=[]
return "".join(text )
train.fillna(0 , inplace = True)
 test.fillna(0 , inplace = True )<count_missing_values>
df['url'] = df['text'].apply(lambda x : find_url(x))
train.isnull().sum()<count_missing_values>
def find_emoji(text):
emo_text=emoji.demojize(text)
 line=re.findall(r'\: (.*?)\:',emo_text)
 return line
train.isnull().sum()<choose_model_class>
sentence="I love ⚽ very much 😁"
find_emoji(sentence )
cluster = KMeans(n_clusters= 800, max_iter=300, tol=0.0001, verbose=0, random_state = 0, n_jobs=-1 )<train_model>
df['emoji'] = df['text'].apply(lambda x: find_emoji(x))
kmeans_train = cluster.fit(train)
 labels_train = kmeans_train.labels_
labels_train<predict_on_test>
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text )
kmeans_testing = cluster.predict(test)
 kmeans_testing<feature_engineering>
sentence="Its all about \U0001F600 face"
print(sentence)
 remove_emoji(sentence )
train['cluster'] = labels_train
test ['cluster'] = kmeans_testing<define_variables>
df['text']=df['text'].apply(lambda x: remove_emoji(x))
print("The number of traning examples(data points)= %i " % train.shape[0])
 print("The number of features we have = %i " % train.shape[1] )<drop_column>
def stop_word_fn(text):
stop_words=set(stopwords.words('english'))
word_tokens= word_tokenize(text)
 non_stop_word=[w for w in word_tokens if not w in stop_words ]
stop_words = [w for w in word_tokens if w in stop_words]
return stop_words
id = test['Id']
test.drop(['Id'], axis = 1, inplace = True)
 train.drop(['Id'], axis = 1, inplace = True)
<split>
example_sent = "This is a sample sentence, showing off the stop words filtration."
stop_word_fn(example_sent )
x_train, x_test, y_train, y_test = train_test_split(train.values , y.values, test_size=0.05, random_state=42 )<choose_model_class>
df['stop_words']=df['text'].apply(lambda x : stop_word_fn(x))
et = ExtraTreesRegressor(n_estimators=950 , max_features = 'auto', max_leaf_nodes=None, n_jobs= -1, random_state = 0, verbose = 0)
 gbr = GradientBoostingRegressor()
lasso = Lasso()
xgbr = XGBRegressor()
svr = SVR(kernel= 'rbf', gamma= 'auto', tol=0.001, C=100.0, max_iter=-1)
 rf = RandomForestRegressor(n_estimators=900, random_state=0)
 lr = LinearRegression(fit_intercept=True, normalize=True, copy_X=True, n_jobs=-1)
 knnR = KNeighborsRegressor(n_neighbors=20, n_jobs=-1)
 reg = StackingCVRegressor(regressors=[ lasso , xgbr , et],meta_regressor=lasso)
 reg.fit(x_train, y_train, groups = None)
<compute_test_metric>
stop = set(stopwords.words('english'))
warnings.filterwarnings(action="ignore")
 cufflinks.go_offline()
cufflinks.set_config_file(world_readable=True, theme='pearl')
reg.score(x_test,y_test )<save_to_csv>
train=pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 test=pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 train.sample(6 )
sub = pd.DataFrame({"Id": id ,"SalePrice":(reg.predict(test.values)).round(decimals=2)})
 sub.to_csv("stackcv_linearsvc.csv", index=False )<set_options>
train.dropna()
train.shape
%matplotlib inline 
<load_from_csv>
train['target'] = train['selected_text'].str.lower()
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )<define_variables>
train['target_url'] =train['target'].apply(lambda x : find_url(x))
data = [["Variable","Definition","Datatype","Key"],
["Passenger ID","Index for the observational unit","Int", "1-1309"],
["Pclass","boarding class of the observational unit","Int", "1-3"],
["Name","Name of observational unit, including the title","String","Multiple"],
["Sex","Gender of the observational unit","String", "male/female"],
["Age","Age of the observational unit","Double","0,17 - 76"],
["SibSp","number of siblings/spouse(s)of the observational unit aboard the Titanic", "Int", "0, 1, 2, 3, 4, 5, 8"],
["Parch","
["Ticket","Ticket number","Int","Multiple"],
["Fare", "Fare paid for Ticket", "float", "0,0000 - 512.329200"],
["Cabin","Cabin number, including the info where this cabin is located","Variant", "Multiple"],
["Embarked","Port of Embarkation","String","'Q', 'S', 'C'"]
]
display(HTML(
 '<table><tr>{}</tr></table>'.format(
 '</tr><tr>'.join(
 '<td>{}</td>'.format('</td><td>'.join(str(_)for _ in row)) for row in data)
)
))<count_missing_values>
def find_star(text):
try:
line=re.findall(r'[*]{2,5}',text)
 except:
line=[]
return len(line)
 train['star']=train['target'].apply(lambda x:find_star(x))
train.isnull().any()<count_missing_values>
def find_only_star(text):
try:
if len(text.split())==1:
line=re.findall(r'[*]{2,5}',text)
 return len(line)
 else:
return 0
except:
return 0
train['only_star']=train['target'].apply(lambda x:find_only_star(x))
test.isnull().any()<feature_engineering>
train['target']= np.where(train['only_star']==1,"abusive",train['target'] )
train.loc[train["Age"].isnull() ==True,"Age"]=np.mean(train["Age"] )<feature_engineering>
def remove_link(string):
try:
text = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +'," ",string)
 except:
text=''
return " ".join(text.split())
 def remove_punct(text):
try:
line = re.sub(r'[!"\$%&'() *+,\-.\/:;=
except:
line=''
return " ".join(line.split())
 train['target']=train['target'].apply(lambda x:remove_link(x))
train['target']=train['target'].apply(lambda x:remove_punct(x))
train['target_tweet_length']=train['target'].str.split().map(lambda x: len(x))
test.loc[test["Age"].isnull() ==True,"Age"]=np.mean(test["Age"] )<feature_engineering>
train['target_average_word_len']=train['target'].str.split().apply(lambda x : [len(i)for i in x] ).map(lambda x: np.mean(x))
train.loc[train["Fare"].isnull() ==True,"Fare"]=np.mean(train["Fare"] )<feature_engineering>
full_data=pd.concat([train,test])
 full_data['text']=full_data['text'].str.lower()
full_data.shape
full_data['text']=full_data['text'].apply(lambda x:remove_link(x))
full_data['text']=full_data['text'].apply(lambda x:remove_punct(x))
test.loc[test["Fare"].isnull() ==True,"Fare"]=np.mean(test["Fare"] )<train_model>
full_data.loc[full_data['text']=="",['text']]="nothing"
gmm=GaussianMixture(n_components=10,max_iter=500,random_state=0)
 age=train["Age"]
age=age.values
age=age.reshape(-1,1)
 gmm.fit(age )<compute_test_metric>
full_data['text_tweet_length']=full_data['text'].str.split().map(lambda x: len(x))
full_data['text_average_word_len']=full_data['text'].str.split().apply(lambda x : [len(i)for i in x] ).map(lambda x: np.mean(x))
log_likelihood=gmm.score_samples(age )<define_variables>
def corpus_sentiment_stop(data,feature,sentiment):
corpus=create_corpus(data,feature,sentiment)
 dic=defaultdict(int)
 for word in corpus:
if word in stop:
dic[word]+=1
top=sorted(dic.items() , key=lambda x:x[1],reverse=True)
 x,y=zip(*top)
 return x,y
detected_outliers_age=np.where(log_likelihood<-8)[0]<filter>
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.iloc[detected_outliers_age]<train_model>
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 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
gmm=GaussianMixture(n_components=80,max_iter=500,random_state=0)
 SibSp=train["SibSp"]
SibSp=SibSp.values
SibSp=SibSp.reshape(-1,1)
 gmm.fit(SibSp )<define_variables>
test = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 ct_test = test.shape[0]
input_ids_t = np.ones(( ct_test,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct_test,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct_test,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
detected_outliers_SibSp=np.where(log_likelihood<1.5)[0]<filter>
def scheduler(epoch):
return 3e-5 * 0.2**epoch
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(128, 2,padding='same' )(x1)
 x1 = tf.keras.layers.LeakyReLU()(x1)
 x1 = tf.keras.layers.Conv1D(64, 2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(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(128, 2, padding='same' )(x2)
 x2 = tf.keras.layers.LeakyReLU()(x2)
 x2 = tf.keras.layers.Conv1D(64, 2, padding='same' )(x2)
 x2 = tf.keras.layers.Dense(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='binary_crossentropy', optimizer=optimizer)
 return model
train.iloc[detected_outliers_SibSp]<train_model>
n_splits=5
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
DISPLAY=1
for i in range(5):
print('
print('
print('
K.clear_session()
model = build_model()
model.load_weights('.. /input/model4/v4-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
<define_variables><EOS> 
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)
 test['selected_text'] = all
submission=test[['textID','selected_text']]
submission.to_csv('submission.csv',index=False)
 submission.head(5 )