{"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 sys \nsys.path.append(\"../input/sentence-transformers/sentence-transformers-1.1.0\") \nimport sentence_transformers","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:18.949705Z","iopub.execute_input":"2022-07-17T09:21:18.950325Z","iopub.status.idle":"2022-07-17T09:21:25.611404Z","shell.execute_reply.started":"2022-07-17T09:21:18.950277Z","shell.execute_reply":"2022-07-17T09:21:25.610287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sentence_transformers import SentenceTransformer","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.613150Z","iopub.execute_input":"2022-07-17T09:21:25.613743Z","iopub.status.idle":"2022-07-17T09:21:25.619094Z","shell.execute_reply.started":"2022-07-17T09:21:25.613706Z","shell.execute_reply":"2022-07-17T09:21:25.617741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain=pd.read_csv('../input/feedback-prize-effectiveness/train.csv')\ntest=pd.read_csv('../input/feedback-prize-effectiveness/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.620579Z","iopub.execute_input":"2022-07-17T09:21:25.621030Z","iopub.status.idle":"2022-07-17T09:21:25.900483Z","shell.execute_reply.started":"2022-07-17T09:21:25.620990Z","shell.execute_reply":"2022-07-17T09:21:25.899541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.903764Z","iopub.execute_input":"2022-07-17T09:21:25.904444Z","iopub.status.idle":"2022-07-17T09:21:25.923181Z","shell.execute_reply.started":"2022-07-17T09:21:25.904402Z","shell.execute_reply":"2022-07-17T09:21:25.922213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effectiveness_map = {\"Ineffective\":0, \"Adequate\":1,\"Effective\":2}\ntrain[\"discourse_effectiveness\"] = train[\"discourse_effectiveness\"].map(effectiveness_map)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.924639Z","iopub.execute_input":"2022-07-17T09:21:25.924975Z","iopub.status.idle":"2022-07-17T09:21:25.940125Z","shell.execute_reply.started":"2022-07-17T09:21:25.924942Z","shell.execute_reply":"2022-07-17T09:21:25.939018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.941555Z","iopub.execute_input":"2022-07-17T09:21:25.942741Z","iopub.status.idle":"2022-07-17T09:21:25.956869Z","shell.execute_reply.started":"2022-07-17T09:21:25.942703Z","shell.execute_reply":"2022-07-17T09:21:25.956043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.958049Z","iopub.execute_input":"2022-07-17T09:21:25.958447Z","iopub.status.idle":"2022-07-17T09:21:25.969456Z","shell.execute_reply.started":"2022-07-17T09:21:25.958410Z","shell.execute_reply":"2022-07-17T09:21:25.968279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sentences_train=[]\nfrom sentence_transformers import SentenceTransformer\nfor i in train['discourse_text']:\n    sentences_train.append(i)\nmodel=SentenceTransformer('../input/allenai',device='cuda')\n#Sentences are encoded by calling model.encode()\nX = model.encode(sentences_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:21:25.971559Z","iopub.execute_input":"2022-07-17T09:21:25.972542Z","iopub.status.idle":"2022-07-17T09:23:04.671468Z","shell.execute_reply.started":"2022-07-17T09:21:25.972501Z","shell.execute_reply":"2022-07-17T09:23:04.670507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sentences_test=[]\nfrom sentence_transformers import SentenceTransformer\nfor i in test['discourse_text']:\n    sentences_test.append(i)\n\nmodel=SentenceTransformer('../input/allenai',device='cuda')\n#Sentences are encoded by calling model.encode()\ntest = model.encode(sentences_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:23:04.673076Z","iopub.execute_input":"2022-07-17T09:23:04.673690Z","iopub.status.idle":"2022-07-17T09:23:06.284186Z","shell.execute_reply.started":"2022-07-17T09:23:04.673654Z","shell.execute_reply":"2022-07-17T09:23:06.283149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:23:06.289085Z","iopub.execute_input":"2022-07-17T09:23:06.289779Z","iopub.status.idle":"2022-07-17T09:23:06.296508Z","shell.execute_reply.started":"2022-07-17T09:23:06.289739Z","shell.execute_reply":"2022-07-17T09:23:06.295587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:23:06.297914Z","iopub.execute_input":"2022-07-17T09:23:06.298794Z","iopub.status.idle":"2022-07-17T09:23:06.306223Z","shell.execute_reply.started":"2022-07-17T09:23:06.298757Z","shell.execute_reply":"2022-07-17T09:23:06.305234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=train['discourse_effectiveness']","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:23:06.308072Z","iopub.execute_input":"2022-07-17T09:23:06.308858Z","iopub.status.idle":"2022-07-17T09:23:06.314268Z","shell.execute_reply.started":"2022-07-17T09:23:06.308817Z","shell.execute_reply":"2022-07-17T09:23:06.313047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, stratify=y, random_state=1121218\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:23:06.315928Z","iopub.execute_input":"2022-07-17T09:23:06.316695Z","iopub.status.idle":"2022-07-17T09:23:06.364610Z","shell.execute_reply.started":"2022-07-17T09:23:06.316538Z","shell.execute_reply":"2022-07-17T09:23:06.363711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Another Approach","metadata":{}},{"cell_type":"markdown","source":"for root, dirs, files in os.walk('../input/feedback-prize-effectiveness/train'):\n    for file in files:\n        if file.endswith('.txt'):\n            for i in train['essay_id']:\n                if i==str(file[:-4]):\n                    with open(os.path.join(root, file), 'r') as f:\n                        text = f.read()\n                train['discourse_text']=text\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:52:44.654141Z","iopub.execute_input":"2022-07-16T20:52:44.654506Z","iopub.status.idle":"2022-07-16T21:05:40.187789Z","shell.execute_reply.started":"2022-07-16T20:52:44.654473Z","shell.execute_reply":"2022-07-16T21:05:40.186479Z"}}},{"cell_type":"markdown","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:05:40.188740Z","iopub.status.idle":"2022-07-16T21:05:40.189800Z","shell.execute_reply.started":"2022-07-16T21:05:40.189527Z","shell.execute_reply":"2022-07-16T21:05:40.189559Z"}}},{"cell_type":"markdown","source":"from sklearn.model_selection import RandomizedSearchCV\nimport lightgbm as lgb\nrs_params = {\n        'bagging_fraction': (0.5, 0.8),\n        'bagging_frequency': (5, 8),\n        'feature_fraction': (0.5, 0.8),\n        'max_depth': (10, 13),\n        'min_data_in_leaf': (90, 120),\n        'num_leaves': (1200, 1550)\n}\n\n# Initialize a RandomizedSearchCV object using 5-fold CV-\nrs_cv = RandomizedSearchCV(estimator=lgb.LGBMClassifier(objective='multiclass',metric='multi_logloss',num_class=3,device_type='gpu'), param_distributions=rs_params, cv = 5, n_iter=10,verbose=1)\n\n# Train on training data-\nrs_cv.fit(X_train, y_train,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T04:43:52.927594Z","iopub.execute_input":"2022-07-17T04:43:52.928255Z","iopub.status.idle":"2022-07-17T05:34:22.111104Z","shell.execute_reply.started":"2022-07-17T04:43:52.928216Z","shell.execute_reply":"2022-07-17T05:34:22.110193Z"}}},{"cell_type":"markdown","source":"import time\nprint(rs_cv.best_params_)\npredicted=rs_cv.predict_proba(X_test)\n# Calculate MAE\nfrom sklearn.metrics import log_loss\nloss = log_loss(y_test,predicted) \nprint(\"log loss:\" ,loss)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:44:43.018707Z","iopub.execute_input":"2022-07-17T05:44:43.019089Z","iopub.status.idle":"2022-07-17T05:44:43.509598Z","shell.execute_reply.started":"2022-07-17T05:44:43.019058Z","shell.execute_reply":"2022-07-17T05:44:43.508546Z"}}},{"cell_type":"markdown","source":"best_classifier_lgbm=rs_cv.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:44:54.016879Z","iopub.execute_input":"2022-07-17T05:44:54.017274Z","iopub.status.idle":"2022-07-17T05:44:54.022196Z","shell.execute_reply.started":"2022-07-17T05:44:54.017240Z","shell.execute_reply":"2022-07-17T05:44:54.021273Z"}}},{"cell_type":"markdown","source":"y_pred_test_lgbm = best_classifier_lgbm.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:45:03.684349Z","iopub.execute_input":"2022-07-17T05:45:03.685005Z","iopub.status.idle":"2022-07-17T05:45:03.690615Z","shell.execute_reply.started":"2022-07-17T05:45:03.684967Z","shell.execute_reply":"2022-07-17T05:45:03.689452Z"}}},{"cell_type":"markdown","source":"submission_lgbm = pd.read_csv(\"../input/feedback-prize-effectiveness/sample_submission.csv\")\nsubmission_lgbm.loc[:,\"Ineffective\"] = y_pred_test_lgbm [:,0]\nsubmission_lgbm.loc[:,\"Adequate\"] = y_pred_test_lgbm [:,1]\nsubmission_lgbm.loc[:,\"Effective\"] = y_pred_test_lgbm [:,2]","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:45:18.598072Z","iopub.execute_input":"2022-07-17T05:45:18.598452Z","iopub.status.idle":"2022-07-17T05:45:18.610125Z","shell.execute_reply.started":"2022-07-17T05:45:18.598421Z","shell.execute_reply":"2022-07-17T05:45:18.609105Z"}}},{"cell_type":"markdown","source":"submission_lgbm.to_csv('submission_lgbm.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:45:20.219798Z","iopub.execute_input":"2022-07-17T05:45:20.220183Z","iopub.status.idle":"2022-07-17T05:45:20.228885Z","shell.execute_reply.started":"2022-07-17T05:45:20.220150Z","shell.execute_reply":"2022-07-17T05:45:20.227906Z"}}},{"cell_type":"code","source":"from xgboost import XGBClassifier\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import RandomizedSearchCV\nimport time\n\n# A parameter grid for XGBoost\nparams = {\n    'n_estimators':[500],\n    'min_child_weight':[4,5], \n    'gamma':[i/10.0 for i in range(3,6)],  \n    'subsample':[i/10.0 for i in range(6,11)],\n    'colsample_bytree':[i/10.0 for i in range(6,11)], \n    'max_depth': [2,3,4,6,7],\n    'objective': ['multi:softproba'],\n    'booster': ['gbtree', 'gblinear'],\n    'eval_metric': ['mlogloss'],\n    'eta': [i/10.0 for i in range(3,6)],\n    'tree_method':['gpu_hist']\n}\n\nreg = XGBClassifier()\n\n# run randomized search\nn_iter_search = 10\nrandom_search = RandomizedSearchCV(reg, param_distributions=params,\n                                   n_iter=n_iter_search, cv=5, scoring='neg_log_loss')\n\nstart = time.time()\nrandom_search.fit(X_train, y_train)\nprint(\"RandomizedSearchCV took %.2f seconds for %d candidates\"\n      \" parameter settings.\" % ((time.time() - start), n_iter_search))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:27:53.432912Z","iopub.execute_input":"2022-07-17T09:27:53.433253Z","iopub.status.idle":"2022-07-17T10:32:56.326649Z","shell.execute_reply.started":"2022-07-17T09:27:53.433225Z","shell.execute_reply":"2022-07-17T10:32:56.325568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_regressor = random_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:32:56.328813Z","iopub.execute_input":"2022-07-17T10:32:56.329178Z","iopub.status.idle":"2022-07-17T10:32:56.334821Z","shell.execute_reply.started":"2022-07-17T10:32:56.329141Z","shell.execute_reply":"2022-07-17T10:32:56.333759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted=best_regressor.predict_proba(X_test)\n# Calculate MAE\nfrom sklearn.metrics import log_loss\nloss = log_loss(y_test,predicted) \nprint(\"log loss:\" ,loss)\nend = time.time()\nprint('Execution time is:')\nprint(end - start)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:32:56.336628Z","iopub.execute_input":"2022-07-17T10:32:56.337182Z","iopub.status.idle":"2022-07-17T10:32:56.511122Z","shell.execute_reply.started":"2022-07-17T10:32:56.337142Z","shell.execute_reply":"2022-07-17T10:32:56.510104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_test_xg = best_regressor.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:32:56.513514Z","iopub.execute_input":"2022-07-17T10:32:56.514104Z","iopub.status.idle":"2022-07-17T10:32:56.520104Z","shell.execute_reply.started":"2022-07-17T10:32:56.514067Z","shell.execute_reply":"2022-07-17T10:32:56.519043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_lgbm = pd.read_csv(\"../input/feedback-prize-effectiveness/sample_submission.csv\")\nsubmission_lgbm.loc[:,\"Ineffective\"] = y_pred_test_xg [:,0]\nsubmission_lgbm.loc[:,\"Adequate\"] = y_pred_test_xg [:,1]\nsubmission_lgbm.loc[:,\"Effective\"] = y_pred_test_xg [:,2]","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:32:56.521657Z","iopub.execute_input":"2022-07-17T10:32:56.522035Z","iopub.status.idle":"2022-07-17T10:32:56.536288Z","shell.execute_reply.started":"2022-07-17T10:32:56.522001Z","shell.execute_reply":"2022-07-17T10:32:56.535456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_lgbm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:32:56.537601Z","iopub.execute_input":"2022-07-17T10:32:56.537929Z","iopub.status.idle":"2022-07-17T10:32:56.545159Z","shell.execute_reply.started":"2022-07-17T10:32:56.537895Z","shell.execute_reply":"2022-07-17T10:32:56.543989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}