{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import libraries\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport string\nimport re\n\nfrom sklearn.model_selection import train_test_split\n\nfrom imblearn.combine import SMOTETomek\nfrom imblearn.under_sampling import TomekLinks\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.metrics import confusion_matrix, recall_score, f1_score, accuracy_score, precision_score, log_loss","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:24:32.533679Z","iopub.execute_input":"2022-07-12T11:24:32.534050Z","iopub.status.idle":"2022-07-12T11:24:32.545861Z","shell.execute_reply.started":"2022-07-12T11:24:32.534020Z","shell.execute_reply":"2022-07-12T11:24:32.544517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract data from csv\ntraining_file = pd.read_csv('/kaggle/input/feedback-prize-effectiveness/train.csv')\ntesting_file = pd.read_csv('/kaggle/input/feedback-prize-effectiveness/test.csv')\nsubmission = pd.read_csv(\"../input/feedback-prize-effectiveness/sample_submission.csv\")\ntraining_file.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:24:33.840406Z","iopub.execute_input":"2022-07-12T11:24:33.841239Z","iopub.status.idle":"2022-07-12T11:24:34.009909Z","shell.execute_reply.started":"2022-07-12T11:24:33.841191Z","shell.execute_reply":"2022-07-12T11:24:34.008679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effectiveness_map = {\"Ineffective\":0, \"Adequate\":1,\"Effective\":2}\ntraining_file[\"discourse_effectiveness\"] = training_file[\"discourse_effectiveness\"].map(effectiveness_map)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:24:34.985618Z","iopub.execute_input":"2022-07-12T11:24:34.986699Z","iopub.status.idle":"2022-07-12T11:24:35.001874Z","shell.execute_reply.started":"2022-07-12T11:24:34.986639Z","shell.execute_reply":"2022-07-12T11:24:35.000543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lowercasing(text): \n    text = \"\".join(word.lower() for word in text)\n    return text\n\ndef punctuation_es(text):\n    punctuation_words = string.punctuation + '¿¡·' \n    text = \"\".join(word for word in text if word not in punctuation_words)\n    return text\n\ndef numbers_cleanner(text):\n    text = re.sub('\\d', '', text)\n    return text\n\ndef pipeline(text):\n    text = lowercasing(text)\n    text = numbers_cleanner(text)\n    text = punctuation_es(text)\n    return text","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:24:49.304088Z","iopub.execute_input":"2022-07-12T11:24:49.304584Z","iopub.status.idle":"2022-07-12T11:24:49.313942Z","shell.execute_reply.started":"2022-07-12T11:24:49.304552Z","shell.execute_reply":"2022-07-12T11:24:49.312412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract text and categories from training file\nX = training_file['discourse_text']\ny = training_file['discourse_effectiveness']\n\n# Preprocess and vectorize text (X)\ntfidf = TfidfVectorizer()\n\nfor i in range(len(X)):\n    X[i] = pipeline(X[i])\n\n# Vectorizer\nX = tfidf.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:24:51.909797Z","iopub.execute_input":"2022-07-12T11:24:51.910157Z","iopub.status.idle":"2022-07-12T11:25:16.518859Z","shell.execute_reply.started":"2022-07-12T11:24:51.910129Z","shell.execute_reply":"2022-07-12T11:25:16.517658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Balance dataset\nresample = SMOTETomek(tomek=TomekLinks(sampling_strategy='majority'))\nX, y = resample.fit_resample(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:25:52.409740Z","iopub.execute_input":"2022-07-12T11:25:52.410139Z","iopub.status.idle":"2022-07-12T11:30:33.765617Z","shell.execute_reply.started":"2022-07-12T11:25:52.410110Z","shell.execute_reply":"2022-07-12T11:30:33.764303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divive train & eval data\nX_train,X_test, Y_train,Y_test = train_test_split(X,y,test_size=0.2, random_state=25)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T11:30:33.768279Z","iopub.execute_input":"2022-07-12T11:30:33.768750Z","iopub.status.idle":"2022-07-12T11:30:33.796380Z","shell.execute_reply.started":"2022-07-12T11:30:33.768696Z","shell.execute_reply":"2022-07-12T11:30:33.794950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\nimport lightgbm as lgb\nlgb=lgb.LGBMClassifier(objective='multiclass',metric='multi_logloss',num_class=3,device_type='gpu')\nlgb.fit(X=X_train, y=Y_train)\npredicted=lgb.predict(X_test)\nprint(accuracy_score(Y_test, predicted))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:11:42.402834Z","iopub.execute_input":"2022-07-12T12:11:42.403280Z","iopub.status.idle":"2022-07-12T12:12:43.724099Z","shell.execute_reply.started":"2022-07-12T12:11:42.403250Z","shell.execute_reply":"2022-07-12T12:12:43.722751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Eval precision model\npred_eval = lgb.predict(X_test)\n\nprint(\"-- Eval precision:\", precision_score(Y_test, pred_eval, average='weighted'))\nprint(\"-- Eval recall:\", recall_score(Y_test, pred_eval, average='weighted'))\nprint(\"-- Eval f1:\", f1_score(Y_test, pred_eval, average='weighted'))\nprint(\"-- Eval accuracy:\", accuracy_score(Y_test, pred_eval))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:13:00.755730Z","iopub.execute_input":"2022-07-12T12:13:00.756156Z","iopub.status.idle":"2022-07-12T12:13:01.146060Z","shell.execute_reply.started":"2022-07-12T12:13:00.756126Z","shell.execute_reply":"2022-07-12T12:13:01.143887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrix\ncm_bow = confusion_matrix(Y_test, pred_eval)\n\nclass_label = y.unique()\ndf_cm = pd.DataFrame(cm_bow, index = class_label, columns = class_label)\n\nsns.heatmap(df_cm, annot = True, fmt = 'd')\nplt.title('Confusion matrix')\nplt.xlabel('PRED')\nplt.ylabel('REAL')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:13:08.134648Z","iopub.execute_input":"2022-07-12T12:13:08.135801Z","iopub.status.idle":"2022-07-12T12:13:08.461804Z","shell.execute_reply.started":"2022-07-12T12:13:08.135754Z","shell.execute_reply":"2022-07-12T12:13:08.460501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W = testing_file['discourse_text']\n\n# Vectorizer\nW = tfidf.transform(W)\n\npred_test = lgb.predict_proba(W)\npred_test","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:13:18.523703Z","iopub.execute_input":"2022-07-12T12:13:18.524077Z","iopub.status.idle":"2022-07-12T12:13:18.540945Z","shell.execute_reply.started":"2022-07-12T12:13:18.524049Z","shell.execute_reply":"2022-07-12T12:13:18.539756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.loc[:,\"Ineffective\"] = pred_test[:,0]\nsubmission.loc[:,\"Adequate\"] = pred_test[:,1]\nsubmission.loc[:,\"Effective\"] = pred_test[:,2]\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:13:28.522255Z","iopub.execute_input":"2022-07-12T12:13:28.523320Z","iopub.status.idle":"2022-07-12T12:13:28.544106Z","shell.execute_reply.started":"2022-07-12T12:13:28.523284Z","shell.execute_reply":"2022-07-12T12:13:28.542658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T12:13:36.006222Z","iopub.execute_input":"2022-07-12T12:13:36.006665Z","iopub.status.idle":"2022-07-12T12:13:36.016207Z","shell.execute_reply.started":"2022-07-12T12:13:36.006619Z","shell.execute_reply":"2022-07-12T12:13:36.014994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}