Kaggle competition: Tweet Sentiment Extraction
<SOS> metric: Jaccard Kaggle data source: tweet-sentiment-extraction<define_variables>
import pandas as pd
import numpy as np
import torch
import os
import sklearn
import json
col_names=datasets_no_missing.columns.values
col_types=datasets_no_missing.dtypes
object_cols=[]
numeric_cols=[]
for col_name, col_type in zip(col_names, col_types):
if col_type=='object':
object_cols.append(col_name)
 else:
numeric_cols.append(col_name)
 print("String değerler içeren özellikler:")
 print(object_cols)
 print("
Sayıal değerler içeren özellikler:")
 print(numeric_cols )<categorify>
!pip install '.. /input/required-packages/seqeval-0.0.12-py3-none-any.whl' -q
!pip install '.. /input/required-packages/simpletransformers-0.22.1-py3-none-any.whl' -q
label_encoder_col_names =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
<categorify>
INPUT_FILES_PATH = "/kaggle/input/tweet-sentiment-extraction/"
for col_name in label_encoder_col_names:
labelEncoder=LabelEncoder()
labelEncoder.fit(datasets_no_missing[col_name].values)
 datasets_no_missing[col_name]=labelEncoder.transform(datasets_no_missing[col_name].values )<feature_engineering>
df_train = pd.read_csv(INPUT_FILES_PATH + "train.csv")
 df_train = df_train.dropna()
df_train.reset_index(drop=True, inplace=True)
 df_test = pd.read_csv(INPUT_FILES_PATH + "test.csv")
 df_test = df_test.dropna()
df_test.reset_index(drop=True, inplace=True)
 sample_submission = pd.read_csv(INPUT_FILES_PATH + "sample_submission.csv")
datasets_no_missing['TotalSF'] = datasets_no_missing['TotalBsmtSF'] + datasets_no_missing['1stFlrSF'] + datasets_no_missing['2ndFlrSF']<feature_engineering>
use_cuda = torch.cuda.is_available()
use_cuda
skewness = skewness[abs(skewness)> 0.75]
print("Box Cox transform uygulanan özelliklerin sayısı:{}".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
datasets_no_missing[feat] = boxcox1p(datasets_no_missing[feat], lam )<categorify>
def getSelectedTextStartIndex(text, selected_text):
return text.lower().find(selected_text.lower())
 start_indices = []
for tupl in df_train.itertuples() :
start_indices.append(getSelectedTextStartIndex(tupl.text, tupl.selected_text))
datasets_no_missing_dummies=pd.get_dummies(datasets_no_missing)
 print(datasets_no_missing_dummies.shape )<normalization>
X_yellow=datasets_no_missing_dummies[:ntrain]
nfeature_name=datasets_no_missing_dummies.columns.values[:-1]
rank1D=Rank1D(features=nfeature_name, algorithm="shapiro")
 rank1D.fit(X_yellow[nfeature_name], y_train)
 rank1D.transform(X_yellow[nfeature_name])
 print("önem hesaplandı" )<split>
n=30
n_most_important=df.index.values[:n]<normalization>
def train_preprocessing(df_train):
out = []
for tupl in df_train.itertuples() :
question = tupl.sentiment
answer = tupl.selected_text
context = tupl.text
qid = tupl.Index
answer_start = start_indices[qid]
ans = [{"answer_start": answer_start, "text": answer.lower() }]
qas = [{"question": question, "id": qid, "is_impossible": False, "answers": ans}]
out.append({"context": context.lower() , "qas": qas})
 return out
def test_preprocessing(df_test):
out = []
for tupl in df_test.itertuples() :
qas = []
con = []
ans = []
question = tupl.sentiment
context = tupl.text
qid = tupl.Index
ans = [{"answer_start": 1000000, "text": "__None__"}]
qas = [{"question": question, "id": qid, "is_impossible": False, "answers": ans}]
out.append({"context": context.lower() , "qas": qas})
 return out
scaler = MinMaxScaler()
scaler.fit(datasets_no_missing_dummies[n_most_important])
 datasets_no_missing_dummies_scaled=scaler.transform(datasets_no_missing_dummies[n_most_important] )<define_variables>
BERT_PATH = ".. /input/required-packages/distilbert-base-uncased-distilled-squad"
start = time.time()
args={
'reprocess_input_data': True,
'overwrite_output_dir': True,
'learning_rate': 5e-5,
'num_train_epochs': 4,
'doc_stride': 64,
'fp16': False,
}
QA_model_pos = QuestionAnsweringModel('distilbert', BERT_PATH, args=args, use_cuda=use_cuda)
 QA_model_neg = QuestionAnsweringModel('distilbert', BERT_PATH, args=args, use_cuda=use_cuda)
 end = time.time()
print(end - start )
preprocessed_train = datasets_no_missing_dummies_scaled[:ntrain]
preprocessed_test = datasets_no_missing_dummies_scaled[ntrain:] 
<compute_train_metric>
train_data = train_preprocessing(df_train[df_train["sentiment"] == "positive"])
 QA_model_pos.train_model(train_data = train_data, output_dir=MODEL_POS, show_running_loss=True )
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(preprocessed_train)
 rmse= np.sqrt(-cross_val_score(model, preprocessed_train, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<choose_model_class>
train_data = train_preprocessing(df_train[df_train["sentiment"] == "negative"])
 QA_model_neg.train_model(train_data = train_data, output_dir=MODEL_POS, show_running_loss=True )
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )<choose_model_class>
predictions_pos = QA_model_pos.predict(test_preprocessing(df_test_pos))
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 )<compute_test_metric>
df_test_neg = df_test[df_test["sentiment"]=="negative"].copy()
df_test_neg.head()
score = rmsle_cv(model_lgb)
 print("LGMRboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
predictions_neg = QA_model_neg.predict(test_preprocessing(df_test_neg))
score = rmsle_cv(model_xgb)
 print("XGBRboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
prediction_test_pos = pd.DataFrame.from_dict(predictions_pos)
 prediction_test_pos.index = df_test_pos.index.copy()
prediction_test_neg = pd.DataFrame(predictions_neg)
 prediction_test_neg.index = df_test_neg.index.copy()
prediction_test_neu = df_test[df_test["sentiment"] == "neutral"].copy()
prediction_test_neu.rename(columns = {"text": "answer" }, inplace=True)
 submission_df = pd.concat([prediction_test_pos, prediction_test_neg, prediction_test_neu], axis=0)
 submission_df.sort_index(inplace=True )
<predict_on_test><EOS> 
sample_submission["selected_text"] = submission_df["answer"]
sample_submission.to_csv("submission.csv", index = False)
 print("File submitted successfully.")