{"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":"markdown","source":"# Predict/Classification of user's rating given review text","metadata":{}},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport plotly.express as px\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nimport re\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom plotly.offline import init_notebook_mode\ninit_notebook_mode(connected=True)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":false,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-04T14:11:12.000508Z","iopub.execute_input":"2023-02-04T14:11:12.000917Z","iopub.status.idle":"2023-02-04T14:11:14.670783Z","shell.execute_reply.started":"2023-02-04T14:11:12.000858Z","shell.execute_reply":"2023-02-04T14:11:14.669752Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import logging\nfrom numpy import random\nimport gensim\nimport nltk\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\nfrom sklearn.metrics import accuracy_score, confusion_matrix\nfrom nltk.corpus import stopwords\nfrom tqdm import tqdm\nimport re\nfrom bs4 import BeautifulSoup\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:14.672672Z","iopub.execute_input":"2023-02-04T14:11:14.675146Z","iopub.status.idle":"2023-02-04T14:11:15.607746Z","shell.execute_reply.started":"2023-02-04T14:11:14.675104Z","shell.execute_reply":"2023-02-04T14:11:15.606784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load The Dataset","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv',\n                usecols=['review_text', 'rating'])","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:15.609071Z","iopub.execute_input":"2023-02-04T14:11:15.609770Z","iopub.status.idle":"2023-02-04T14:11:36.277440Z","shell.execute_reply.started":"2023-02-04T14:11:15.609732Z","shell.execute_reply":"2023-02-04T14:11:36.276465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Quick Lookup","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:36.280021Z","iopub.execute_input":"2023-02-04T14:11:36.280495Z","iopub.status.idle":"2023-02-04T14:11:36.298599Z","shell.execute_reply.started":"2023-02-04T14:11:36.280456Z","shell.execute_reply":"2023-02-04T14:11:36.297529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['review_text'].apply(lambda x: len(x.split(' '))).sum())","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:36.300254Z","iopub.execute_input":"2023-02-04T14:11:36.300919Z","iopub.status.idle":"2023-02-04T14:11:43.505558Z","shell.execute_reply.started":"2023-02-04T14:11:36.300858Z","shell.execute_reply":"2023-02-04T14:11:43.504391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,4))\ndf.rating.value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:43.507776Z","iopub.execute_input":"2023-02-04T14:11:43.508996Z","iopub.status.idle":"2023-02-04T14:11:43.734823Z","shell.execute_reply.started":"2023-02-04T14:11:43.508954Z","shell.execute_reply":"2023-02-04T14:11:43.733898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The rating label is imbalanced. <br/>\nHow can I handle this? hmm... <br/>\n - Using weighted RMSE loss?\n - Using scikit-learn's `LogisticRegression` class with weighting?","metadata":{}},{"cell_type":"markdown","source":"## Text Preprocessing","metadata":{}},{"cell_type":"code","source":"stopwords = stopwords.words('english')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:43.736376Z","iopub.execute_input":"2023-02-04T14:11:43.736742Z","iopub.status.idle":"2023-02-04T14:11:43.747041Z","shell.execute_reply.started":"2023-02-04T14:11:43.736705Z","shell.execute_reply":"2023-02-04T14:11:43.746369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_desc(text_col):\n    preprocessed_descs = []\n    url_pattern = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')\n    tag_pattern = re.compile('@(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')\n    for sentence in tqdm(text_col.values):\n        if sentence is np.nan:\n            preprocessed_descs.append(sent.lower().strip())\n            continue\n        else:\n            sent = url_pattern.sub('', sentence)\n            sent = tag_pattern.sub('', sent)\n            sent = sent.replace('\\\\r', ' ')\n            sent = sent.replace('\\\\\"', ' ')\n            sent = sent.replace('\\\\n', ' ')\n            sent = re.sub('[^A-Za-z0-9]+', ' ', sent)\n            sent = ' '.join(e for e in sent.split() if e not in stopwords)\n            preprocessed_descs.append(sent.lower().strip())\n    return preprocessed_descs","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:43.749547Z","iopub.execute_input":"2023-02-04T14:11:43.751092Z","iopub.status.idle":"2023-02-04T14:11:43.758496Z","shell.execute_reply.started":"2023-02-04T14:11:43.751055Z","shell.execute_reply":"2023-02-04T14:11:43.757447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.assign(pre_text = preprocess_desc(df['review_text']))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:11:43.760083Z","iopub.execute_input":"2023-02-04T14:11:43.760555Z","iopub.status.idle":"2023-02-04T14:18:39.156677Z","shell.execute_reply.started":"2023-02-04T14:11:43.760521Z","shell.execute_reply":"2023-02-04T14:18:39.155752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['pre_text'].apply(lambda x: len(x.split(' '))).sum())","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:18:39.160746Z","iopub.execute_input":"2023-02-04T14:18:39.161609Z","iopub.status.idle":"2023-02-04T14:18:43.476495Z","shell.execute_reply.started":"2023-02-04T14:18:39.161568Z","shell.execute_reply":"2023-02-04T14:18:43.475336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:18:43.478166Z","iopub.execute_input":"2023-02-04T14:18:43.478542Z","iopub.status.idle":"2023-02-04T14:18:43.489525Z","shell.execute_reply.started":"2023-02-04T14:18:43.478505Z","shell.execute_reply":"2023-02-04T14:18:43.488309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tokenizing","metadata":{}},{"cell_type":"code","source":"!pip install tensorflow-gpu\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:25:31.069624Z","iopub.execute_input":"2023-02-04T14:25:31.070028Z","iopub.status.idle":"2023-02-04T14:25:43.559732Z","shell.execute_reply.started":"2023-02-04T14:25:31.069995Z","shell.execute_reply":"2023-02-04T14:25:43.558551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews = df['pre_text'].tolist()\n#ratings = df['rating'].tolist()\nratings = [rating-1 for rating in df['rating'].tolist()]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:33:23.785393Z","iopub.execute_input":"2023-02-04T16:33:23.785958Z","iopub.status.idle":"2023-02-04T16:33:23.905852Z","shell.execute_reply.started":"2023-02-04T16:33:23.785913Z","shell.execute_reply":"2023-02-04T16:33:23.904869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocessing\n# Convert the ratings to one-hot encoding\n#labels = tf.keras.utils.to_categorical(ratings, num_classes=6)\nlabels = tf.keras.utils.to_categorical(ratings, num_classes=5)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:33:25.423395Z","iopub.execute_input":"2023-02-04T16:33:25.423776Z","iopub.status.idle":"2023-02-04T16:33:25.470534Z","shell.execute_reply.started":"2023-02-04T16:33:25.423743Z","shell.execute_reply":"2023-02-04T16:33:25.469624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:33:28.613102Z","iopub.execute_input":"2023-02-04T16:33:28.613703Z","iopub.status.idle":"2023-02-04T16:33:28.621833Z","shell.execute_reply.started":"2023-02-04T16:33:28.613657Z","shell.execute_reply":"2023-02-04T16:33:28.620472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tokenize the text data\ntokenizer = Tokenizer()\ntokenizer.fit_on_texts(reviews)\nsequence = tokenizer.texts_to_sequences(reviews)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:19:03.175561Z","iopub.execute_input":"2023-02-04T14:19:03.176196Z","iopub.status.idle":"2023-02-04T14:21:11.599583Z","shell.execute_reply.started":"2023-02-04T14:19:03.176160Z","shell.execute_reply":"2023-02-04T14:21:11.598568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pad the sequences to a fixed length\nsequence = pad_sequences(sequence, maxlen=100)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:21:11.600928Z","iopub.execute_input":"2023-02-04T14:21:11.601315Z","iopub.status.idle":"2023-02-04T14:21:16.385511Z","shell.execute_reply.started":"2023-02-04T14:21:11.601276Z","shell.execute_reply":"2023-02-04T14:21:16.384487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:30:23.799143Z","iopub.execute_input":"2023-02-04T16:30:23.799499Z","iopub.status.idle":"2023-02-04T16:30:23.806673Z","shell.execute_reply.started":"2023-02-04T16:30:23.799467Z","shell.execute_reply":"2023-02-04T16:30:23.805458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting","metadata":{}},{"cell_type":"code","source":"# Splitting the data\nsplit = 0.8\nsplit_index = int(len(sequence) * split)\n# Train split\ntrain_data = sequence[:split_index]\ntrain_labels = labels[:split_index]\n# Test split\ntest_data = sequence[split_index:]\ntest_labels = labels[split_index:]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:26.035532Z","iopub.execute_input":"2023-02-04T17:05:26.035939Z","iopub.status.idle":"2023-02-04T17:05:26.041734Z","shell.execute_reply.started":"2023-02-04T17:05:26.035902Z","shell.execute_reply":"2023-02-04T17:05:26.040625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:28.013772Z","iopub.execute_input":"2023-02-04T17:05:28.014353Z","iopub.status.idle":"2023-02-04T17:05:28.020394Z","shell.execute_reply.started":"2023-02-04T17:05:28.014317Z","shell.execute_reply":"2023-02-04T17:05:28.019413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:29.550359Z","iopub.execute_input":"2023-02-04T17:05:29.550711Z","iopub.status.idle":"2023-02-04T17:05:29.557484Z","shell.execute_reply.started":"2023-02-04T17:05:29.550678Z","shell.execute_reply":"2023-02-04T17:05:29.556254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:31.328461Z","iopub.execute_input":"2023-02-04T17:05:31.328832Z","iopub.status.idle":"2023-02-04T17:05:31.336431Z","shell.execute_reply.started":"2023-02-04T17:05:31.328798Z","shell.execute_reply":"2023-02-04T17:05:31.335182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define Weighted RMSE loss","metadata":{}},{"cell_type":"code","source":"# Calculate weight for each class\nnum_classes = 5\nweights = np.zeros(num_classes)\nfor c in range(num_classes):\n    class_data = train_labels[train_labels[:, c] == 1]\n    weights[c] = 1.0 / (np.sum(class_data) / float(train_labels.shape[0]))\nweights = weights / np.sum(weights)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:48:16.509015Z","iopub.execute_input":"2023-02-04T16:48:16.510020Z","iopub.status.idle":"2023-02-04T16:48:16.558822Z","shell.execute_reply.started":"2023-02-04T16:48:16.509984Z","shell.execute_reply":"2023-02-04T16:48:16.557809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:48:17.821716Z","iopub.execute_input":"2023-02-04T16:48:17.822390Z","iopub.status.idle":"2023-02-04T16:48:17.828367Z","shell.execute_reply.started":"2023-02-04T16:48:17.822354Z","shell.execute_reply":"2023-02-04T16:48:17.827460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def weighted_rmse(y_true, y_pred, weights):\n    # Calculate the mean squared error for each sample\n    sample_mse = tf.math.square(y_true - y_pred)\n    \n    # Apply the weights to the mean squared error\n    weighted_mse = tf.math.multiply(sample_mse, weights)\n    \n    # Calculate the mean of the weighted mean squared error\n    mean_weighted_mse = tf.reduce_mean(weighted_mse)\n    \n    # Return the root mean squared error\n    return tf.math.sqrt(mean_weighted_mse)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:48:19.513233Z","iopub.execute_input":"2023-02-04T16:48:19.513628Z","iopub.status.idle":"2023-02-04T16:48:19.519111Z","shell.execute_reply.started":"2023-02-04T16:48:19.513592Z","shell.execute_reply":"2023-02-04T16:48:19.518139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building The Model","metadata":{}},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:31:52.805334Z","iopub.execute_input":"2023-02-04T16:31:52.805699Z","iopub.status.idle":"2023-02-04T16:31:52.811328Z","shell.execute_reply.started":"2023-02-04T16:31:52.805669Z","shell.execute_reply":"2023-02-04T16:31:52.810112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callback for early stopping\nearly_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', \n    patience=2, \n    mode='min',\n    restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:37.310467Z","iopub.execute_input":"2023-02-04T17:05:37.311057Z","iopub.status.idle":"2023-02-04T17:05:37.317293Z","shell.execute_reply.started":"2023-02-04T17:05:37.311019Z","shell.execute_reply":"2023-02-04T17:05:37.316115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building the model\nmodel = keras.Sequential([\n    keras.layers.Embedding(input_dim=len(tokenizer.word_index) + 1,\n                           output_dim=100,\n                           input_length=100),\n    keras.layers.Flatten(),\n    keras.layers.Dense(128, activation='relu'),\n    keras.layers.Dense(64, activation='relu'),\n    keras.layers.Dense(5, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:39.222599Z","iopub.execute_input":"2023-02-04T17:05:39.223012Z","iopub.status.idle":"2023-02-04T17:05:39.268730Z","shell.execute_reply.started":"2023-02-04T17:05:39.222975Z","shell.execute_reply":"2023-02-04T17:05:39.267748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:41.827178Z","iopub.execute_input":"2023-02-04T17:05:41.827533Z","iopub.status.idle":"2023-02-04T17:05:41.834254Z","shell.execute_reply.started":"2023-02-04T17:05:41.827503Z","shell.execute_reply":"2023-02-04T17:05:41.833143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\n'''\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n'''\n\nmodel.compile(optimizer='adam', \n              loss=lambda y_true, y_pred: weighted_rmse(y_true, y_pred, weights), \n              metrics=['accuracy'])\n\n'''\nmodel.compile(optimizer='adam', \n              loss='categorical_crossentropy', \n              metrics=['accuracy'], \n              sample_weight_mode=\"temporal\")\n'''","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:05:44.814805Z","iopub.execute_input":"2023-02-04T17:05:44.815488Z","iopub.status.idle":"2023-02-04T17:05:44.827197Z","shell.execute_reply.started":"2023-02-04T17:05:44.815452Z","shell.execute_reply":"2023-02-04T17:05:44.826199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model\n\nhistory = model.fit(train_data, train_labels, \n                    epochs=100, batch_size=32, \n                    validation_split=0.2, \n                    callbacks=[early_stopping])\n\n'''\nhistory = model.fit(train_data, train_labels, \n                    epochs=10, batch_size=32, \n                    validation_split=0.2,\n                    callbacks=[early_stopping])\n'''","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:12:55.846218Z","iopub.execute_input":"2023-02-04T17:12:55.846571Z","iopub.status.idle":"2023-02-04T17:21:31.104797Z","shell.execute_reply.started":"2023-02-04T17:12:55.846541Z","shell.execute_reply":"2023-02-04T17:21:31.103826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\ntest_loss, test_acc = model.evaluate(test_data, test_labels)\nprint('Test accuracy: ', test_acc)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:57:11.447580Z","iopub.execute_input":"2023-02-04T16:57:11.447958Z","iopub.status.idle":"2023-02-04T16:57:21.731648Z","shell.execute_reply.started":"2023-02-04T16:57:11.447927Z","shell.execute_reply":"2023-02-04T16:57:21.730570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot History","metadata":{}},{"cell_type":"code","source":"def plot_history(history):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n    \n    epochs_range = range(len(acc))\n    \n    plt.figure(figsize=(8, 8))\n    plt.subplot(2, 1, 1)\n    plt.plot(epochs_range, acc, label='Training Accuracy')\n    plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n    plt.legend(loc='lower right')\n    plt.title('Training and Validation Accuracy')\n    \n    plt.subplot(2, 1, 2)\n    plt.plot(epochs_range, loss, label='Training Loss')\n    plt.plot(epochs_range, val_loss, label='Validation Loss')\n    plt.legend(loc='upper right')\n    plt.title('Training and Validation Loss')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:57:49.564038Z","iopub.execute_input":"2023-02-04T16:57:49.564389Z","iopub.status.idle":"2023-02-04T16:57:49.572790Z","shell.execute_reply.started":"2023-02-04T16:57:49.564360Z","shell.execute_reply":"2023-02-04T16:57:49.571768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:57:52.479447Z","iopub.execute_input":"2023-02-04T16:57:52.479817Z","iopub.status.idle":"2023-02-04T16:57:52.836698Z","shell.execute_reply.started":"2023-02-04T16:57:52.479785Z","shell.execute_reply":"2023-02-04T16:57:52.835765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predicts on unseen data","metadata":{}},{"cell_type":"code","source":"dfTest = pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv',\n                usecols=['review_id', 'review_text'])","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:58:04.129666Z","iopub.execute_input":"2023-02-04T16:58:04.130042Z","iopub.status.idle":"2023-02-04T16:58:08.484100Z","shell.execute_reply.started":"2023-02-04T16:58:04.130009Z","shell.execute_reply":"2023-02-04T16:58:08.483103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTest.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:58:20.204219Z","iopub.execute_input":"2023-02-04T16:58:20.204582Z","iopub.status.idle":"2023-02-04T16:58:20.216999Z","shell.execute_reply.started":"2023-02-04T16:58:20.204549Z","shell.execute_reply":"2023-02-04T16:58:20.215928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTest = dfTest.assign(pre_text = preprocess_desc(dfTest['review_text']))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T16:58:22.618713Z","iopub.execute_input":"2023-02-04T16:58:22.619399Z","iopub.status.idle":"2023-02-04T17:02:08.160420Z","shell.execute_reply.started":"2023-02-04T16:58:22.619360Z","shell.execute_reply":"2023-02-04T17:02:08.159441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTest.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:08.162467Z","iopub.execute_input":"2023-02-04T17:02:08.163086Z","iopub.status.idle":"2023-02-04T17:02:08.173715Z","shell.execute_reply.started":"2023-02-04T17:02:08.163047Z","shell.execute_reply":"2023-02-04T17:02:08.172705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unseen_text = dfTest['pre_text'].tolist()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:08.175184Z","iopub.execute_input":"2023-02-04T17:02:08.175538Z","iopub.status.idle":"2023-02-04T17:02:08.194449Z","shell.execute_reply.started":"2023-02-04T17:02:08.175501Z","shell.execute_reply":"2023-02-04T17:02:08.193543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predictions on unseen data\nunseen_sequence = tokenizer.texts_to_sequences(unseen_text)\nunseen_sequence = pad_sequences(unseen_sequence, maxlen=100)\npredictions = model.predict(unseen_sequence)\npredictions = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:08.197017Z","iopub.execute_input":"2023-02-04T17:02:08.197335Z","iopub.status.idle":"2023-02-04T17:02:51.748282Z","shell.execute_reply.started":"2023-02-04T17:02:08.197302Z","shell.execute_reply":"2023-02-04T17:02:51.744821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:51.753154Z","iopub.execute_input":"2023-02-04T17:02:51.753487Z","iopub.status.idle":"2023-02-04T17:02:51.768447Z","shell.execute_reply.started":"2023-02-04T17:02:51.753457Z","shell.execute_reply":"2023-02-04T17:02:51.767421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Predictions:\", predictions)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:51.770129Z","iopub.execute_input":"2023-02-04T17:02:51.770567Z","iopub.status.idle":"2023-02-04T17:02:51.778354Z","shell.execute_reply.started":"2023-02-04T17:02:51.770529Z","shell.execute_reply":"2023-02-04T17:02:51.777259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create submission dataframe\nsubmission = pd.DataFrame({'review_id': dfTest['review_id'],\n                           'rating': predictions})","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:51.779932Z","iopub.execute_input":"2023-02-04T17:02:51.780594Z","iopub.status.idle":"2023-02-04T17:02:51.796004Z","shell.execute_reply.started":"2023-02-04T17:02:51.780556Z","shell.execute_reply":"2023-02-04T17:02:51.794896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:51.797477Z","iopub.execute_input":"2023-02-04T17:02:51.797961Z","iopub.status.idle":"2023-02-04T17:02:51.811280Z","shell.execute_reply.started":"2023-02-04T17:02:51.797921Z","shell.execute_reply":"2023-02-04T17:02:51.810096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Export submission file for submit to competition\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T17:02:51.813111Z","iopub.execute_input":"2023-02-04T17:02:51.813962Z","iopub.status.idle":"2023-02-04T17:02:52.379387Z","shell.execute_reply.started":"2023-02-04T17:02:51.813924Z","shell.execute_reply":"2023-02-04T17:02:52.377766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## My note o.O\n- While doing this I just keep forgeting that there are imbalance data, ... but nvm :'>\n    - 0.22921 | My dumb feedforward NN do their best :'<\n- Now I use weigted RMSE loss (Yep, I do care about data imbalance now(?))\n    - 0.21617 | My dumb dumb still dumb :p\n\n![Nyan Cat](https://i0.wp.com/www.printmag.com/wp-content/uploads/2021/02/4cbe8d_f1ed2800a49649848102c68fc5a66e53mv2.gif?resize=476%2C280&ssl=1)","metadata":{}},{"cell_type":"code","source":"# Plz sending love to my dumb dumb neural network ★♥︎☀︎","metadata":{},"execution_count":null,"outputs":[]}]}