{"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,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-04-15T17:41:14.324014Z","iopub.execute_input":"2023-04-15T17:41:14.324487Z","iopub.status.idle":"2023-04-15T17:41:17.432849Z","shell.execute_reply.started":"2023-04-15T17:41:14.324380Z","shell.execute_reply":"2023-04-15T17:41:17.431773Z"},"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-04-15T17:41:17.434936Z","iopub.execute_input":"2023-04-15T17:41:17.435373Z","iopub.status.idle":"2023-04-15T17:41:18.357745Z","shell.execute_reply.started":"2023-04-15T17:41:17.435337Z","shell.execute_reply":"2023-04-15T17:41:18.356771Z"},"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-04-15T17:41:18.359036Z","iopub.execute_input":"2023-04-15T17:41:18.359756Z","iopub.status.idle":"2023-04-15T17:41:43.719427Z","shell.execute_reply.started":"2023-04-15T17:41:18.359722Z","shell.execute_reply":"2023-04-15T17:41:43.718441Z"},"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-04-15T17:41:43.721881Z","iopub.execute_input":"2023-04-15T17:41:43.722279Z","iopub.status.idle":"2023-04-15T17:41:43.740762Z","shell.execute_reply.started":"2023-04-15T17:41:43.722243Z","shell.execute_reply":"2023-04-15T17:41:43.739868Z"},"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-04-15T17:41:43.742058Z","iopub.execute_input":"2023-04-15T17:41:43.742448Z","iopub.status.idle":"2023-04-15T17:41:51.598889Z","shell.execute_reply.started":"2023-04-15T17:41:43.742403Z","shell.execute_reply":"2023-04-15T17:41:51.597850Z"},"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-04-15T17:41:51.600192Z","iopub.execute_input":"2023-04-15T17:41:51.601093Z","iopub.status.idle":"2023-04-15T17:41:51.819257Z","shell.execute_reply.started":"2023-04-15T17:41:51.601057Z","shell.execute_reply":"2023-04-15T17:41:51.818386Z"},"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-04-15T17:41:51.821044Z","iopub.execute_input":"2023-04-15T17:41:51.822441Z","iopub.status.idle":"2023-04-15T17:41:51.831692Z","shell.execute_reply.started":"2023-04-15T17:41:51.822406Z","shell.execute_reply":"2023-04-15T17:41:51.830676Z"},"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-04-15T17:41:51.833099Z","iopub.execute_input":"2023-04-15T17:41:51.833742Z","iopub.status.idle":"2023-04-15T17:41:51.842333Z","shell.execute_reply.started":"2023-04-15T17:41:51.833702Z","shell.execute_reply":"2023-04-15T17:41:51.841282Z"},"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-04-15T17:41:51.844255Z","iopub.execute_input":"2023-04-15T17:41:51.844713Z","iopub.status.idle":"2023-04-15T17:48:41.128388Z","shell.execute_reply.started":"2023-04-15T17:41:51.844671Z","shell.execute_reply":"2023-04-15T17:48:41.127298Z"},"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-04-15T17:48:41.132584Z","iopub.execute_input":"2023-04-15T17:48:41.132863Z","iopub.status.idle":"2023-04-15T17:48:45.455043Z","shell.execute_reply.started":"2023-04-15T17:48:41.132838Z","shell.execute_reply":"2023-04-15T17:48:45.453850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:48:45.456383Z","iopub.execute_input":"2023-04-15T17:48:45.456834Z","iopub.status.idle":"2023-04-15T17:48:45.469602Z","shell.execute_reply.started":"2023-04-15T17:48:45.456795Z","shell.execute_reply":"2023-04-15T17:48:45.468490Z"},"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-04-15T17:48:45.471352Z","iopub.execute_input":"2023-04-15T17:48:45.471696Z","iopub.status.idle":"2023-04-15T17:49:03.436878Z","shell.execute_reply.started":"2023-04-15T17:48:45.471663Z","shell.execute_reply":"2023-04-15T17:49:03.435867Z"},"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-04-15T17:49:03.438640Z","iopub.execute_input":"2023-04-15T17:49:03.439532Z","iopub.status.idle":"2023-04-15T17:49:03.718507Z","shell.execute_reply.started":"2023-04-15T17:49:03.439494Z","shell.execute_reply":"2023-04-15T17:49:03.717597Z"},"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-04-15T17:49:03.720043Z","iopub.execute_input":"2023-04-15T17:49:03.720427Z","iopub.status.idle":"2023-04-15T17:49:03.772648Z","shell.execute_reply.started":"2023-04-15T17:49:03.720392Z","shell.execute_reply":"2023-04-15T17:49:03.771731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:49:03.774179Z","iopub.execute_input":"2023-04-15T17:49:03.774575Z","iopub.status.idle":"2023-04-15T17:49:03.781819Z","shell.execute_reply.started":"2023-04-15T17:49:03.774539Z","shell.execute_reply":"2023-04-15T17:49:03.780748Z"},"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-04-15T17:49:03.783179Z","iopub.execute_input":"2023-04-15T17:49:03.783810Z","iopub.status.idle":"2023-04-15T17:51:05.402300Z","shell.execute_reply.started":"2023-04-15T17:49:03.783772Z","shell.execute_reply":"2023-04-15T17:51:05.401336Z"},"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-04-15T17:51:05.403624Z","iopub.execute_input":"2023-04-15T17:51:05.404073Z","iopub.status.idle":"2023-04-15T17:51:10.288065Z","shell.execute_reply.started":"2023-04-15T17:51:05.404038Z","shell.execute_reply":"2023-04-15T17:51:10.287013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:10.292616Z","iopub.execute_input":"2023-04-15T17:51:10.295253Z","iopub.status.idle":"2023-04-15T17:51:10.306816Z","shell.execute_reply.started":"2023-04-15T17:51:10.295198Z","shell.execute_reply":"2023-04-15T17:51:10.305863Z"},"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-04-15T17:51:10.308487Z","iopub.execute_input":"2023-04-15T17:51:10.309127Z","iopub.status.idle":"2023-04-15T17:51:10.316694Z","shell.execute_reply.started":"2023-04-15T17:51:10.309093Z","shell.execute_reply":"2023-04-15T17:51:10.315836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:10.318170Z","iopub.execute_input":"2023-04-15T17:51:10.318578Z","iopub.status.idle":"2023-04-15T17:51:10.334376Z","shell.execute_reply.started":"2023-04-15T17:51:10.318545Z","shell.execute_reply":"2023-04-15T17:51:10.333319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:10.336057Z","iopub.execute_input":"2023-04-15T17:51:10.336454Z","iopub.status.idle":"2023-04-15T17:51:10.346905Z","shell.execute_reply.started":"2023-04-15T17:51:10.336419Z","shell.execute_reply":"2023-04-15T17:51:10.346002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:10.348020Z","iopub.execute_input":"2023-04-15T17:51:10.348356Z","iopub.status.idle":"2023-04-15T17:51:10.358500Z","shell.execute_reply.started":"2023-04-15T17:51:10.348322Z","shell.execute_reply":"2023-04-15T17:51:10.357451Z"},"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-04-15T17:51:10.360141Z","iopub.execute_input":"2023-04-15T17:51:10.360808Z","iopub.status.idle":"2023-04-15T17:51:10.405904Z","shell.execute_reply.started":"2023-04-15T17:51:10.360775Z","shell.execute_reply":"2023-04-15T17:51:10.404824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:10.407669Z","iopub.execute_input":"2023-04-15T17:51:10.408050Z","iopub.status.idle":"2023-04-15T17:51:10.415885Z","shell.execute_reply.started":"2023-04-15T17:51:10.408014Z","shell.execute_reply":"2023-04-15T17:51:10.414885Z"},"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-04-15T17:51:10.417427Z","iopub.execute_input":"2023-04-15T17:51:10.418458Z","iopub.status.idle":"2023-04-15T17:51:10.426676Z","shell.execute_reply.started":"2023-04-15T17:51:10.418425Z","shell.execute_reply":"2023-04-15T17:51:10.425656Z"},"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-04-15T17:51:10.428377Z","iopub.execute_input":"2023-04-15T17:51:10.428876Z","iopub.status.idle":"2023-04-15T17:51:10.517084Z","shell.execute_reply.started":"2023-04-15T17:51:10.428842Z","shell.execute_reply":"2023-04-15T17:51:10.516103Z"},"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-04-15T17:51:10.518713Z","iopub.execute_input":"2023-04-15T17:51:10.519812Z","iopub.status.idle":"2023-04-15T17:51:10.528354Z","shell.execute_reply.started":"2023-04-15T17:51:10.519775Z","shell.execute_reply":"2023-04-15T17:51:10.527398Z"},"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-04-15T17:51:10.534958Z","iopub.execute_input":"2023-04-15T17:51:10.535626Z","iopub.status.idle":"2023-04-15T17:51:13.481407Z","shell.execute_reply.started":"2023-04-15T17:51:10.535590Z","shell.execute_reply":"2023-04-15T17:51:13.480455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:51:13.482995Z","iopub.execute_input":"2023-04-15T17:51:13.483340Z","iopub.status.idle":"2023-04-15T17:51:13.489964Z","shell.execute_reply.started":"2023-04-15T17:51:13.483305Z","shell.execute_reply":"2023-04-15T17:51:13.488891Z"},"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-04-15T17:51:13.491360Z","iopub.execute_input":"2023-04-15T17:51:13.492340Z","iopub.status.idle":"2023-04-15T17:51:13.509591Z","shell.execute_reply.started":"2023-04-15T17:51:13.492307Z","shell.execute_reply":"2023-04-15T17:51:13.508539Z"},"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-04-15T17:51:13.511199Z","iopub.execute_input":"2023-04-15T17:51:13.511564Z","iopub.status.idle":"2023-04-15T17:59:20.229926Z","shell.execute_reply.started":"2023-04-15T17:51:13.511530Z","shell.execute_reply":"2023-04-15T17:59:20.228805Z"},"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-04-15T17:59:20.231582Z","iopub.execute_input":"2023-04-15T17:59:20.231948Z","iopub.status.idle":"2023-04-15T17:59:27.995746Z","shell.execute_reply.started":"2023-04-15T17:59:20.231912Z","shell.execute_reply":"2023-04-15T17:59:27.994866Z"},"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-04-15T17:59:27.998053Z","iopub.execute_input":"2023-04-15T17:59:27.999044Z","iopub.status.idle":"2023-04-15T17:59:28.006628Z","shell.execute_reply.started":"2023-04-15T17:59:27.999007Z","shell.execute_reply":"2023-04-15T17:59:28.005777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:59:28.007726Z","iopub.execute_input":"2023-04-15T17:59:28.008366Z","iopub.status.idle":"2023-04-15T17:59:28.332697Z","shell.execute_reply.started":"2023-04-15T17:59:28.008329Z","shell.execute_reply":"2023-04-15T17:59:28.331787Z"},"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-04-15T17:59:28.334266Z","iopub.execute_input":"2023-04-15T17:59:28.334646Z","iopub.status.idle":"2023-04-15T17:59:41.209621Z","shell.execute_reply.started":"2023-04-15T17:59:28.334610Z","shell.execute_reply":"2023-04-15T17:59:41.208638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTest.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T17:59:41.211238Z","iopub.execute_input":"2023-04-15T17:59:41.211639Z","iopub.status.idle":"2023-04-15T17:59:41.223055Z","shell.execute_reply.started":"2023-04-15T17:59:41.211600Z","shell.execute_reply":"2023-04-15T17:59:41.221885Z"},"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-04-15T17:59:41.224657Z","iopub.execute_input":"2023-04-15T17:59:41.225854Z","iopub.status.idle":"2023-04-15T18:03:23.361651Z","shell.execute_reply.started":"2023-04-15T17:59:41.225820Z","shell.execute_reply":"2023-04-15T18:03:23.359289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTest.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:03:23.363474Z","iopub.execute_input":"2023-04-15T18:03:23.363832Z","iopub.status.idle":"2023-04-15T18:03:23.377186Z","shell.execute_reply.started":"2023-04-15T18:03:23.363797Z","shell.execute_reply":"2023-04-15T18:03:23.376084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unseen_text = dfTest['pre_text'].tolist()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:03:23.378691Z","iopub.execute_input":"2023-04-15T18:03:23.379284Z","iopub.status.idle":"2023-04-15T18:03:23.405271Z","shell.execute_reply.started":"2023-04-15T18:03:23.379248Z","shell.execute_reply":"2023-04-15T18:03:23.404115Z"},"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-04-15T18:03:23.406805Z","iopub.execute_input":"2023-04-15T18:03:23.407425Z","iopub.status.idle":"2023-04-15T18:04:14.820740Z","shell.execute_reply.started":"2023-04-15T18:03:23.407382Z","shell.execute_reply":"2023-04-15T18:04:14.819682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:04:14.822166Z","iopub.execute_input":"2023-04-15T18:04:14.822897Z","iopub.status.idle":"2023-04-15T18:04:14.830012Z","shell.execute_reply.started":"2023-04-15T18:04:14.822847Z","shell.execute_reply":"2023-04-15T18:04:14.828737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Predictions:\", predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:04:14.832193Z","iopub.execute_input":"2023-04-15T18:04:14.832572Z","iopub.status.idle":"2023-04-15T18:04:14.841350Z","shell.execute_reply.started":"2023-04-15T18:04:14.832538Z","shell.execute_reply":"2023-04-15T18:04:14.840289Z"},"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-04-15T18:04:14.843024Z","iopub.execute_input":"2023-04-15T18:04:14.843450Z","iopub.status.idle":"2023-04-15T18:04:14.860267Z","shell.execute_reply.started":"2023-04-15T18:04:14.843417Z","shell.execute_reply":"2023-04-15T18:04:14.859178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-04-15T18:04:14.863041Z","iopub.execute_input":"2023-04-15T18:04:14.863327Z","iopub.status.idle":"2023-04-15T18:04:14.877967Z","shell.execute_reply.started":"2023-04-15T18:04:14.863302Z","shell.execute_reply":"2023-04-15T18:04:14.877062Z"},"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-04-15T18:04:14.880181Z","iopub.execute_input":"2023-04-15T18:04:14.881125Z","iopub.status.idle":"2023-04-15T18:04:15.432772Z","shell.execute_reply.started":"2023-04-15T18:04:14.881091Z","shell.execute_reply":"2023-04-15T18:04:15.431771Z"},"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":{"iopub.status.busy":"2023-04-15T18:04:15.434273Z","iopub.execute_input":"2023-04-15T18:04:15.434723Z","iopub.status.idle":"2023-04-15T18:04:15.439263Z","shell.execute_reply.started":"2023-04-15T18:04:15.434684Z","shell.execute_reply":"2023-04-15T18:04:15.438332Z"},"trusted":true},"execution_count":null,"outputs":[]}]}