{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\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":"2023-01-21T19:18:36.418022Z","iopub.execute_input":"2023-01-21T19:18:36.41837Z","iopub.status.idle":"2023-01-21T19:18:36.44257Z","shell.execute_reply.started":"2023-01-21T19:18:36.418286Z","shell.execute_reply":"2023-01-21T19:18:36.441578Z"},"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":"2023-01-21T19:18:36.444489Z","iopub.execute_input":"2023-01-21T19:18:36.445156Z","iopub.status.idle":"2023-01-21T19:18:38.793378Z","shell.execute_reply.started":"2023-01-21T19:18:36.445121Z","shell.execute_reply":"2023-01-21T19:18:38.792281Z"},"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":"2023-01-21T19:18:38.799191Z","iopub.execute_input":"2023-01-21T19:18:38.801537Z","iopub.status.idle":"2023-01-21T19:19:11.988201Z","shell.execute_reply.started":"2023-01-21T19:18:38.801498Z","shell.execute_reply":"2023-01-21T19:19:11.985889Z"},"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":"2023-01-21T19:19:11.990512Z","iopub.execute_input":"2023-01-21T19:19:11.99087Z","iopub.status.idle":"2023-01-21T19:19:12.597837Z","shell.execute_reply.started":"2023-01-21T19:19:11.990834Z","shell.execute_reply":"2023-01-21T19:19:12.596835Z"},"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":"2023-01-21T19:19:12.600453Z","iopub.execute_input":"2023-01-21T19:19:12.600792Z","iopub.status.idle":"2023-01-21T19:19:12.604885Z","shell.execute_reply.started":"2023-01-21T19:19:12.600764Z","shell.execute_reply":"2023-01-21T19:19:12.603762Z"},"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":"2023-01-21T19:19:12.60645Z","iopub.execute_input":"2023-01-21T19:19:12.606859Z","iopub.status.idle":"2023-01-21T19:19:12.933839Z","shell.execute_reply.started":"2023-01-21T19:19:12.606822Z","shell.execute_reply":"2023-01-21T19:19:12.932928Z"},"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":"2023-01-21T19:19:12.935387Z","iopub.execute_input":"2023-01-21T19:19:12.935989Z","iopub.status.idle":"2023-01-21T19:19:12.939832Z","shell.execute_reply.started":"2023-01-21T19:19:12.935952Z","shell.execute_reply":"2023-01-21T19:19:12.938858Z"},"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":"2023-01-21T19:19:12.941212Z","iopub.execute_input":"2023-01-21T19:19:12.941812Z","iopub.status.idle":"2023-01-21T19:19:13.546603Z","shell.execute_reply.started":"2023-01-21T19:19:12.941777Z","shell.execute_reply":"2023-01-21T19:19:13.545677Z"},"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":"2023-01-21T19:19:13.551031Z","iopub.execute_input":"2023-01-21T19:19:13.552843Z","iopub.status.idle":"2023-01-21T19:19:14.121981Z","shell.execute_reply.started":"2023-01-21T19:19:13.552813Z","shell.execute_reply":"2023-01-21T19:19:14.120733Z"},"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":"2023-01-21T19:19:14.124Z","iopub.execute_input":"2023-01-21T19:19:14.124436Z","iopub.status.idle":"2023-01-21T19:19:14.131919Z","shell.execute_reply.started":"2023-01-21T19:19:14.124397Z","shell.execute_reply":"2023-01-21T19:19:14.130791Z"},"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":"2023-01-21T19:19:14.13367Z","iopub.execute_input":"2023-01-21T19:19:14.134421Z","iopub.status.idle":"2023-01-21T19:19:14.817017Z","shell.execute_reply.started":"2023-01-21T19:19:14.134378Z","shell.execute_reply":"2023-01-21T19:19:14.816052Z"},"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":"2023-01-21T19:19:14.818456Z","iopub.execute_input":"2023-01-21T19:19:14.818913Z","iopub.status.idle":"2023-01-21T19:19:15.017875Z","shell.execute_reply.started":"2023-01-21T19:19:14.818873Z","shell.execute_reply":"2023-01-21T19:19:15.016888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"books.drop_duplicates(subset=['review_text'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-21T19:19:15.019226Z","iopub.execute_input":"2023-01-21T19:19:15.01968Z","iopub.status.idle":"2023-01-21T19:19:19.387915Z","shell.execute_reply.started":"2023-01-21T19:19:15.019643Z","shell.execute_reply":"2023-01-21T19:19:19.386876Z"},"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":"2023-01-21T19:19:19.389298Z","iopub.execute_input":"2023-01-21T19:19:19.389679Z","iopub.status.idle":"2023-01-21T19:20:15.163964Z","shell.execute_reply.started":"2023-01-21T19:19:19.389642Z","shell.execute_reply":"2023-01-21T19:20:15.16292Z"},"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":"2023-01-21T19:20:15.165698Z","iopub.execute_input":"2023-01-21T19:20:15.166155Z","iopub.status.idle":"2023-01-21T19:20:16.548288Z","shell.execute_reply.started":"2023-01-21T19:20:15.166116Z","shell.execute_reply":"2023-01-21T19:20:16.547317Z"},"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":"2023-01-21T19:20:16.54956Z","iopub.execute_input":"2023-01-21T19:20:16.549908Z","iopub.status.idle":"2023-01-21T19:20:18.113998Z","shell.execute_reply.started":"2023-01-21T19:20:16.549875Z","shell.execute_reply":"2023-01-21T19:20:18.113039Z"},"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":"2023-01-21T19:20:18.115315Z","iopub.execute_input":"2023-01-21T19:20:18.115976Z","iopub.status.idle":"2023-01-21T19:20:24.601711Z","shell.execute_reply.started":"2023-01-21T19:20:18.115938Z","shell.execute_reply":"2023-01-21T19:20:24.600625Z"},"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":"2023-01-21T19:20:24.603383Z","iopub.execute_input":"2023-01-21T19:20:24.604755Z","iopub.status.idle":"2023-01-21T19:20:24.615753Z","shell.execute_reply.started":"2023-01-21T19:20:24.604709Z","shell.execute_reply":"2023-01-21T19:20:24.614816Z"},"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":"2023-01-21T19:20:24.617308Z","iopub.execute_input":"2023-01-21T19:20:24.617655Z","iopub.status.idle":"2023-01-21T19:20:32.548597Z","shell.execute_reply.started":"2023-01-21T19:20:24.61762Z","shell.execute_reply":"2023-01-21T19:20:32.547031Z"},"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":"2023-01-21T19:20:32.54978Z","iopub.status.idle":"2023-01-21T19:20:32.550759Z","shell.execute_reply.started":"2023-01-21T19:20:32.550498Z","shell.execute_reply":"2023-01-21T19:20:32.550525Z"},"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":{"iopub.status.busy":"2023-01-21T19:20:32.552306Z","iopub.status.idle":"2023-01-21T19:20:32.553402Z","shell.execute_reply.started":"2023-01-21T19:20:32.553136Z","shell.execute_reply":"2023-01-21T19:20:32.553161Z"},"trusted":true},"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":"2023-01-21T19:20:32.554759Z","iopub.status.idle":"2023-01-21T19:20:32.556293Z","shell.execute_reply.started":"2023-01-21T19:20:32.556021Z","shell.execute_reply":"2023-01-21T19:20:32.556045Z"},"trusted":true},"execution_count":null,"outputs":[]}]}