Kaggle competition: University of Liverpool - Ion Switching
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))
Actual_1 = 'My life is totally awesome'
Predict_1 = 'awesome life'
Actual_2 = 'We are active kagglers'
Predict_2 = 'We are active kagglers'
print("Jaccard score for first set of scentences: {}".format(jaccard(Actual_1,Predict_1)))
 print("Jaccard score for second set of scentences: {}".format(jaccard(Actual_2,Predict_2)) )<set_options>
%matplotlib inline
stop=set(stopwords.words('english'))
warnings.filterwarnings("ignore")
 cufflinks.go_offline()
cufflinks.set_config_file(world_readable=True, theme='pearl')
<load_from_csv>
from cuml.neighbors import KNeighborsClassifier, NearestNeighbors
import cuml; cuml.__version__
train=pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 test=pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 train.sample(6 )<count_missing_values>
%%time
NAME='KNN'
NMINUS=3
NPLUS=3
weights=[4, 1, 0.5, 0.25, 1, 0.5, 0.25]
NFOLDS=5
RS=42
PATH=Path('/kaggle/input/is-eda-sine-50hz-exp')
 print(f'Loading data for model {NAME}
')
 train=feather.read_dataframe(PATH/'train.feather')
 test=feather.read_dataframe(PATH/'test.feather')
 with open(PATH/'folds_train.pickle', 'rb')as infile:
folds_train = pickle.load(infile)
 with open(PATH/'folds_val.pickle', 'rb')as infile:
folds_val = pickle.load(infile )
def miss_val(df):
total=df.isnull().sum()
return pd.concat([total],axis=1,keys=['Total'])
 print("Missing values for train dataset
")
 print(miss_val(train))
print("---------------------------------------------------------------------")
 print("Missing values for test dataset
")
 print(miss_val(test))<correct_missing_values>
cols=['signal_no_drift']
target=['open_channels']
for shift in range(1, NMINUS+1):
feature='signal_shift_-' + str(shift)
 cols.append(feature)
 for shift in range(1, NPLUS+1):
feature='signal_shift_+' + str(shift)
 cols.append(feature)
 print(f"The list of features included in the {NAME} model:
")
 print(cols )
train=train.dropna()
train.shape<feature_engineering>
train=train[cols+target+['model', 'batch', 'time']]
test=test[cols+['model', 'batch', 'segment', 'time']]
train['target']=train['selected_text'].str.lower()<feature_engineering>
classes=np.array(['class_'+str(i)for i in range(11)])
 oof=pd.DataFrame(data=np.zeros(( len(train), 11)) , index=train.index, columns=classes)
 oof_preds=np.zeros(len(train))
preds_proba=pd.DataFrame(data=np.zeros(( len(test), 11)) , index=test.index, columns=classes)
 f1_folds=[]
def find_link(string):
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +', string)
 return "".join(url)
 train['target_url']=train['target'].apply(lambda x: find_link(x))
df=pd.DataFrame(train.loc[train['target_url']!=""]['sentiment'].value_counts() ).reset_index()
df.rename(columns={"index": "sentiment", "sentiment": "url_count"} )<string_transform>
for c, w in zip(cols, weights):
train[c]=w*train[c]
test[c]=w*test[c]
def find_punct(text):
line = re.findall(r'[!"\$%&'() *+,\-.\/:;=
string="".join(line)
 return list(string )<feature_engineering>
%%time
KNN=100
batch = 1024
for fold_num in range(1, NFOLDS+1):
print('-'*50)
 print(f'Fold {fold_num}:')
 train_index=folds_train[fold_num]
val_index=folds_val[fold_num]
X_train, Y_train = train.iloc[train_index, :], train.loc[train_index, target]
X_val, Y_val = train.iloc[val_index, :], train.loc[val_index, target]
for m in models:
mask_model_train=(X_train['model']==m)
 mask_model_val=(X_val['model']==m)
 mask_model_test=(test['model']==m)
 X_mod=X_train.loc[mask_model_train, cols].values
Y_mod=Y_train[mask_model_train].values.reshape(-1,)
 X_val_mod=X_val.loc[mask_model_val, cols].values
Y_val_mod=Y_val[mask_model_val].values.reshape(-1,)
 X_test=test.loc[mask_model_test, cols].copy()
if m=='M4':
shift=1
else:
shift=0
clf = NearestNeighbors(n_neighbors=KNN)
 clf.fit(X_mod)
 distances, indices = clf.kneighbors(X_val_mod)
 ct = indices.shape[0]
pred = np.zeros(( ct,KNN),dtype=np.int8)
 Y_val_pred = np.zeros(( ct,len(np.unique(Y_mod))),dtype=np.float32)
 it = ct//batch + int(ct%batch!=0)
 for k in range(it):
a = batch*k; b = batch*(k+1); b = min(ct,b)
 pred[a:b,:] = Y_mod[ indices[a:b].astype(int)]
for j in np.unique(Y_mod):
Y_val_pred[a:b,j-shift] = np.sum(pred[a:b,]==j,axis=1)/KNN
distances, indices = clf.kneighbors(X_test.values)
 ct = indices.shape[0]
pred = np.zeros(( ct,KNN),dtype=np.int8)
 Y_test_pred = np.zeros(( ct,len(np.unique(Y_mod))),dtype=np.float32)
 it = ct//batch + int(ct%batch!=0)
 for k in range(it):
a = batch*k; b = batch*(k+1); b = min(ct,b)
 pred[a:b,:] = Y_mod[ indices[a:b].astype(int)]
for j in np.unique(Y_mod):
Y_test_pred[a:b,j-shift] = np.sum(pred[a:b,]==j,axis=1)/KNN
classes_mod=classes[np.unique(Y_mod)]
oof.loc[val_index[mask_model_val], classes_mod]=Y_val_pred
preds_proba.loc[mask_model_test, classes_mod]+=Y_test_pred
Y_val_pred=np.argmax(Y_val_pred, axis=1 ).astype(int ).reshape(-1,)+ int(shift)
 f1_model=f1_score(Y_val_mod, Y_val_pred, average='macro')
 print(f'Model {m}: done! Macro F1 score = {f1_model:.5f}')
 oof_preds[val_index]=np.argmax(oof.iloc[val_index, :].values, axis=1 ).astype(int ).reshape(-1,)
 Y_val_OC=train.loc[val_index, 'open_channels'].values.astype(np.uint8 ).reshape(-1,)
 f1_fold=f1_score(Y_val_OC, oof_preds[val_index], average='macro')
 f1_folds.append(f1_fold)
 print(f'
Fold {fold_num} is done! Macro F1 score = {f1_fold:.5f}')
 preds_proba/=NFOLDS
preds=np.argmax(preds_proba.values, axis=1 ).astype(int ).reshape(-1,)
 print('-'*50)
 print('Summary:')
 for m in models:
print(f"
Model {m}:")
 mask_model=train['model']==m
f1_model=f1_score(train.loc[mask_model, 'open_channels'].values.reshape(-1,),
oof_preds[mask_model], average='macro')
 print(classification_report(train.loc[mask_model, 'open_channels'].values.reshape(-1,),
oof_preds[mask_model], digits=5))
print(f'Macro F1 score for model {m} = {f1_model:.5f}')
 f1_av=np.array(f1_folds ).mean()
f1_std=np.std(f1_folds)
 print(f'Macro F1 score = {f1_av:.5f}(average across the folds); std = {f1_std:.5f}')
 f1=f1_score(train['open_channels'].values.reshape(-1,), oof_preds, average='macro')
 print(f'Macro F1 score = {f1:.5f}(out-of-folds)' )
train['target_punct']=train['target'].apply(lambda x:find_punct(x))
train['target_punct_len']=train['target'].apply(lambda x:len(find_punct(x)) )<create_dataframe>
for c, w in zip(cols, weights):
train[c]=train[c]/w
test[c]=test[c]/w
punc_df=pd.DataFrame(train,columns=['target_punct','sentiment'])
 punc_df=punc_df[punc_df['target_punct'].map(lambda d: len(d)) > 0]
punc_df=punc_df.explode('target_punct')
 positive_df=pd.DataFrame(punc_df.loc[punc_df['sentiment']=="positive"]['target_punct'].value_counts() ).reset_index().rename(columns={'index': 'punct','target_punct':'pos_punct'})
 negative_df=pd.DataFrame(punc_df.loc[punc_df['sentiment']=="negative"]['target_punct'].value_counts() ).reset_index().rename(columns={'index': 'punct','target_punct':'neg_punct'})
 neutral_df=pd.DataFrame(punc_df.loc[punc_df['sentiment']=="neutral"]['target_punct'].value_counts() ).reset_index().rename(columns={'index': 'punct','target_punct':'neut_punct'})
<count_values>
%%time
print(classification_report(train['open_channels'].values.reshape(-1,), oof_preds, digits=5))
train['star']=train['target'].apply(lambda x:find_star(x))
train.loc[train['star']!=0]['sentiment'].value_counts().to_frame()<feature_engineering>
%%time
PATH=Path('/kaggle/input/is-gmm-cv5-b7-repl-seq-folds/sub_GMM_110.csv')
 preds_GMM=pd.read_csv(PATH)
 mask_M1_GMM=(test['model']=='M1')&(preds_GMM['open_channels']>1)
 preds[mask_M1_GMM]=preds_GMM.loc[mask_M1_GMM, 'open_channels']
def find_only_star(text):
if len(text.split())==1:
line=re.findall(r'[*]{2,5}',text)
 return len(line)
 else:
return 0<count_values>
PATH=Path('/kaggle/input/liverpool-ion-switching/')
 sub=pd.read_csv(PATH/'sample_submission.csv' )
train['only_star']=train['target'].apply(lambda x:find_only_star(x))
train.loc[train['only_star']==1]['sentiment'].value_counts()<feature_engineering>
sub['open_channels']=preds
sub['open_channels'].value_counts().sort_index()
train['target']= np.where(train['only_star']==1,"abusive",train['target'] )<string_transform>
time_zone = pytz.timezone('America/Chicago')
 current_datetime = datetime.now(time_zone)
 ts=current_datetime.strftime("%m%d%H%M%S")
 sub_file_name='sub_'+NAME+'_'+ts+'.csv'
oof_file_name='oof_'+NAME+'.feather'
preds_file_name='preds_'+NAME+'.feather'
ts, sub_file_name, oof_file_name, preds_file_name
def remove_link(string):
text = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +'," ",string)
 return " ".join(text.split() )<string_transform>
%%time
oof.to_feather(oof_file_name)
 preds_proba.to_feather(preds_file_name)
 sub.to_csv(sub_file_name, index=False, float_format='%.4f' )
def remove_punct(text):
line = re.sub(r'[!"\$%&'() *+,\-.\/:;=
return " ".join(line.split() )<feature_engineering>
low={'M1':-4, 'M2':-4, 'M3':-5, 'M4':-5, 'M5':-5}
high={'M1':2, 'M2':0, 'M3':6, 'M4':10, 'M5':5}
train['target']=train['target'].apply(lambda x:remove_link(x))
train['target']=train['target'].apply(lambda x:remove_punct(x))<string_transform>
y_true=train['open_channels'].values
mask=np.equal(y_true, oof_preds)
 for m in models:
show_results(y_true[~mask], mod=m, lag=-1, df=train[~mask], s=0.7, mk_scale=5 )
def create_corpus(data,feature,sentiment):
corpus=[]
for x in data[data['sentiment']==sentiment][feature].str.split() :
for i in x:
corpus.append(i)
 return corpus<prepare_x_and_y>
high['M1']=4
def corpus_sentiment(data,feature,sentiment):
corpus=create_corpus(data,feature,sentiment)
 dic=defaultdict(int)
 for word in corpus:
if word not in stop:
dic[word]+=1
top=sorted(dic.items() , key=lambda x:x[1],reverse=True)
 x,y=zip(*top)
 return x,y<count_values>
batches_order=np.array([0, 1, 2, 6, 3, 7, 4, 9, 5, 8] )
def count_values(df,feature):
total=df.loc[:,feature].value_counts(dropna=False)
 percent=round(df.loc[:,feature].value_counts(dropna=False,normalize=True)*100,2)
 return pd.concat([total,percent],axis=1,keys=['Total','Percent'])
<count_values>
%%time
train['open_channels']=oof_preds
train['open_channels']=train['open_channels'].astype(np.uint8)
 signal_scatter_plots(train, col='signal_no_drift' )
<count_values><EOS> 
%%time
test['open_channels']=preds
test['open_channels']=test['open_channels'].astype(np.uint8)
 signal_scatter_plots(test, col='signal_no_drift' )