{"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":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-21T07:20:23.776083Z","iopub.execute_input":"2022-12-21T07:20:23.777157Z","iopub.status.idle":"2022-12-21T07:20:23.801377Z","shell.execute_reply.started":"2022-12-21T07:20:23.776944Z","shell.execute_reply":"2022-12-21T07:20:23.800406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing\n## Import libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime\nfrom tqdm import tqdm\ntqdm.pandas()\nfrom pandas_profiling import ProfileReport\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:28:44.477269Z","iopub.execute_input":"2022-12-21T11:28:44.477753Z","iopub.status.idle":"2022-12-21T11:28:44.490082Z","shell.execute_reply.started":"2022-12-21T11:28:44.477713Z","shell.execute_reply":"2022-12-21T11:28:44.488845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_train.csv\") \ntest = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:28:44.688732Z","iopub.execute_input":"2022-12-21T11:28:44.689589Z","iopub.status.idle":"2022-12-21T11:29:00.700071Z","shell.execute_reply.started":"2022-12-21T11:28:44.689542Z","shell.execute_reply":"2022-12-21T11:29:00.698987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Train verilerindeki satır sayısı: {train.shape[0]}')\nprint(f'Train verilerindeki sütun sayısı: {train.shape[1]}')\nprint(f'Train verilerindeki değer sayısı: {train.count().sum()}')\nprint(f'Train verilerinde eksik değerlere sahip satır sayısı: {sum(train.isna().sum())}')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:00.702118Z","iopub.execute_input":"2022-12-21T11:29:00.702426Z","iopub.status.idle":"2022-12-21T11:29:01.316114Z","shell.execute_reply.started":"2022-12-21T11:29:00.702396Z","shell.execute_reply":"2022-12-21T11:29:01.315062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_profile = ProfileReport(train, title=\"Train\")\n#train_profile.to_notebook_iframe()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:36:57.976719Z","iopub.execute_input":"2022-12-21T11:36:57.97717Z","iopub.status.idle":"2022-12-21T11:36:57.996698Z","shell.execute_reply.started":"2022-12-21T11:36:57.977076Z","shell.execute_reply":"2022-12-21T11:36:57.995733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Test verilerindeki satır sayısı: {test.shape[0]}')\nprint(f'Test verilerindeki sütun sayısı: {test.shape[1]}')\nprint(f'Test verilerindeki değer sayısı: {test.count().sum()}')\nprint(f'Test verilerinde eksik değerlere sahip satır sayısı: {sum(test.isna().sum())}')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:01.325124Z","iopub.execute_input":"2022-12-21T11:29:01.326255Z","iopub.status.idle":"2022-12-21T11:29:01.653285Z","shell.execute_reply.started":"2022-12-21T11:29:01.326216Z","shell.execute_reply":"2022-12-21T11:29:01.652312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pandas Profile","metadata":{}},{"cell_type":"code","source":"#test_profile = ProfileReport(test, title=\"test\")\n#test_profile.to_notebook_iframe()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:02.068631Z","iopub.execute_input":"2022-12-21T11:29:02.069022Z","iopub.status.idle":"2022-12-21T11:29:02.074072Z","shell.execute_reply.started":"2022-12-21T11:29:02.068986Z","shell.execute_reply":"2022-12-21T11:29:02.072884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing the review texts\n# İnceleme metinlerinin ön işlenmesi","metadata":{}},{"cell_type":"code","source":"import nltk\nfrom nltk.stem import WordNetLemmatizer\nfrom nltk.corpus import stopwords","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:03.309397Z","iopub.execute_input":"2022-12-21T11:29:03.310119Z","iopub.status.idle":"2022-12-21T11:29:03.31506Z","shell.execute_reply.started":"2022-12-21T11:29:03.31008Z","shell.execute_reply":"2022-12-21T11:29:03.313967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"books = pd.concat([train.drop('rating', axis=1), test], axis=0)\nbooks_rates = train['rating']\n\nbooks.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:03.827612Z","iopub.execute_input":"2022-12-21T11:29:03.827998Z","iopub.status.idle":"2022-12-21T11:29:04.404423Z","shell.execute_reply.started":"2022-12-21T11:29:03.827963Z","shell.execute_reply":"2022-12-21T11:29:04.403399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.set_index(\"review_id\", inplace=True)\ntest.set_index(\"review_id\", inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:05.715583Z","iopub.execute_input":"2022-12-21T11:29:05.715969Z","iopub.status.idle":"2022-12-21T11:29:05.722629Z","shell.execute_reply.started":"2022-12-21T11:29:05.715936Z","shell.execute_reply":"2022-12-21T11:29:05.721553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Gerekli olmayan bazı sütunların çıkarılması\n#### Droping some non-essential columns\n","metadata":{}},{"cell_type":"code","source":"books.drop(columns=['date_updated', 'read_at', 'started_at'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:06.768123Z","iopub.execute_input":"2022-12-21T11:29:06.768724Z","iopub.status.idle":"2022-12-21T11:29:07.411412Z","shell.execute_reply.started":"2022-12-21T11:29:06.768683Z","shell.execute_reply":"2022-12-21T11:29:07.41036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Removing rows with negative n_votes or negative n_comments \n#### Negatif n_votes veya negatif n_comments içeren satırları kaldırma","metadata":{}},{"cell_type":"code","source":"books = books[(books['n_votes'] >= 0) & (books['n_comments'] >= 0)]","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:08.175305Z","iopub.execute_input":"2022-12-21T11:29:08.175708Z","iopub.status.idle":"2022-12-21T11:29:08.349378Z","shell.execute_reply.started":"2022-12-21T11:29:08.175674Z","shell.execute_reply":"2022-12-21T11:29:08.348147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"books.drop_duplicates(subset=['review_text'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:08.774595Z","iopub.execute_input":"2022-12-21T11:29:08.775513Z","iopub.status.idle":"2022-12-21T11:29:12.608884Z","shell.execute_reply.started":"2022-12-21T11:29:08.775442Z","shell.execute_reply":"2022-12-21T11:29:12.607844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unapp_texts = books['review_text'].apply(lambda txt: np.mean(list(map(len, txt.split())))) > 25\nunapp_texts.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:29:12.610814Z","iopub.execute_input":"2022-12-21T11:29:12.611695Z","iopub.status.idle":"2022-12-21T11:30:05.89941Z","shell.execute_reply.started":"2022-12-21T11:29:12.61165Z","shell.execute_reply":"2022-12-21T11:30:05.89843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"books.drop(books[unapp_texts].index, axis=0, inplace=True)\nspoil_statement = 'spoiler alert'","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:30:05.90137Z","iopub.execute_input":"2022-12-21T11:30:05.902033Z","iopub.status.idle":"2022-12-21T11:30:07.106121Z","shell.execute_reply.started":"2022-12-21T11:30:05.901991Z","shell.execute_reply":"2022-12-21T11:30:07.105093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"books['spoil'] = [True if spoil_statement in txt else False for txt in books['review_text']]\nbooks[books['spoil'] == True]","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:30:07.107573Z","iopub.execute_input":"2022-12-21T11:30:07.10824Z","iopub.status.idle":"2022-12-21T11:30:08.689488Z","shell.execute_reply.started":"2022-12-21T11:30:07.1082Z","shell.execute_reply":"2022-12-21T11:30:08.688452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building \n# Modelleme","metadata":{}},{"cell_type":"code","source":"import torch\nimport numpy as np\nimport pandas as pd\nfrom sklearn import model_selection, metrics\nfrom transformers import AutoModelForSequenceClassification, AutoTokenizer, TrainingArguments, Trainer","metadata":{"execution":{"iopub.status.busy":"2022-12-21T07:20:23.80321Z","iopub.execute_input":"2022-12-21T07:20:23.803855Z","iopub.status.idle":"2022-12-21T07:20:31.420251Z","shell.execute_reply.started":"2022-12-21T07:20:23.803819Z","shell.execute_reply":"2022-12-21T07:20:31.419038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ClassificationDataset:\n    def __init__(self, data, tokenizer):\n        self.data = data\n        self.tokenizer = tokenizer\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, item):\n        text = str(self.data[\"review_text\"].values[item])\n        target = int(self.data[\"rating\"].values[item])\n        inputs = self.tokenizer(\n            text,\n            max_length=512, \n            padding=\"max_length\", \n            truncation=True\n        )\n\n        ids = inputs[\"input_ids\"]\n        mask = inputs[\"attention_mask\"]\n\n        return {\n            \"input_ids\": torch.tensor(ids, dtype=torch.long),\n            \"attention_mask\": torch.tensor(mask, dtype=torch.long),\n            \"labels\": torch.tensor(target, dtype=torch.long),\n        }","metadata":{"execution":{"iopub.status.busy":"2022-12-21T07:20:31.425505Z","iopub.execute_input":"2022-12-21T07:20:31.428567Z","iopub.status.idle":"2022-12-21T07:20:31.439945Z","shell.execute_reply.started":"2022-12-21T07:20:31.428523Z","shell.execute_reply":"2022-12-21T07:20:31.438886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_test.csv\")\ntokenizer = AutoTokenizer.from_pretrained(\"abhishek/autotrain-goodreads-review-rating-1828462866\", use_fast=True)\nmodel = AutoModelForSequenceClassification.from_pretrained(\"abhishek/autotrain-goodreads-review-rating-1828462866\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T07:20:31.445763Z","iopub.execute_input":"2022-12-21T07:20:31.448343Z","iopub.status.idle":"2022-12-21T07:21:20.511704Z","shell.execute_reply.started":"2022-12-21T07:20:31.448307Z","shell.execute_reply":"2022-12-21T07:21:20.510599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:, \"rating\"] = 0\ndataset = ClassificationDataset(df, tokenizer)\ntrainer = Trainer(model)\npreds = trainer.predict(dataset).predictions","metadata":{"execution":{"iopub.status.busy":"2022-12-21T07:21:20.513353Z","iopub.execute_input":"2022-12-21T07:21:20.513719Z","iopub.status.idle":"2022-12-21T11:17:46.308681Z","shell.execute_reply.started":"2022-12-21T07:21:20.513683Z","shell.execute_reply":"2022-12-21T11:17:46.307706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_sample_submission.csv\")\npreds = np.argmax(preds, axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.rating = preds\nsample_submission.to_csv(\"submission2.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T11:17:46.310082Z","iopub.execute_input":"2022-12-21T11:17:46.311107Z","iopub.status.idle":"2022-12-21T11:17:47.475255Z","shell.execute_reply.started":"2022-12-21T11:17:46.31103Z","shell.execute_reply":"2022-12-21T11:17:47.474272Z"},"trusted":true},"execution_count":null,"outputs":[]}]}