{"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":"2022-07-21T07:07:16.52187Z","iopub.execute_input":"2022-07-21T07:07:16.522262Z","iopub.status.idle":"2022-07-21T07:07:16.667726Z","shell.execute_reply.started":"2022-07-21T07:07:16.522134Z","shell.execute_reply":"2022-07-21T07:07:16.666664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets,layers,models\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom tensorflow.keras.preprocessing.text import Tokenizer","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:07:17.317731Z","iopub.execute_input":"2022-07-21T07:07:17.318282Z","iopub.status.idle":"2022-07-21T07:07:22.573324Z","shell.execute_reply.started":"2022-07-21T07:07:17.318235Z","shell.execute_reply":"2022-07-21T07:07:22.572518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:09:45.510648Z","iopub.execute_input":"2022-07-21T07:09:45.511489Z","iopub.status.idle":"2022-07-21T07:09:56.747376Z","shell.execute_reply.started":"2022-07-21T07:09:45.511446Z","shell.execute_reply":"2022-07-21T07:09:56.746512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(columns=['user_id', 'book_id' ,'date_added', 'date_updated', 'read_at', 'started_at'], axis=0)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:09:56.749092Z","iopub.execute_input":"2022-07-21T07:09:56.749366Z","iopub.status.idle":"2022-07-21T07:09:56.96029Z","shell.execute_reply.started":"2022-07-21T07:09:56.749329Z","shell.execute_reply":"2022-07-21T07:09:56.959564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['rating']\nX = train.drop('rating', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:09:56.961544Z","iopub.execute_input":"2022-07-21T07:09:56.961799Z","iopub.status.idle":"2022-07-21T07:09:57.009213Z","shell.execute_reply.started":"2022-07-21T07:09:56.961762Z","shell.execute_reply":"2022-07-21T07:09:57.008284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nX['review_id'] = le.fit_transform(X['review_id'])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:09:57.01171Z","iopub.execute_input":"2022-07-21T07:09:57.012053Z","iopub.status.idle":"2022-07-21T07:09:58.941304Z","shell.execute_reply.started":"2022-07-21T07:09:57.012013Z","shell.execute_reply":"2022-07-21T07:09:58.940057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" X['review_text'] = [str (item) for item in X['review_text']]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:09:58.943027Z","iopub.execute_input":"2022-07-21T07:09:58.943306Z","iopub.status.idle":"2022-07-21T07:09:59.427298Z","shell.execute_reply.started":"2022-07-21T07:09:58.943267Z","shell.execute_reply":"2022-07-21T07:09:59.42648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = Tokenizer(num_words=10000, oov_token='<UNK>')\ntokenizer.fit_on_texts(X['review_text'])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:10:13.436622Z","iopub.execute_input":"2022-07-21T07:10:13.436921Z","iopub.status.idle":"2022-07-21T07:12:15.606545Z","shell.execute_reply.started":"2022-07-21T07:10:13.436888Z","shell.execute_reply":"2022-07-21T07:12:15.605639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_sequences(tokenizer, review):\n    sequences = tokenizer.texts_to_sequences(review)\n    padded_sequences = pad_sequences(sequences, truncating='post', maxlen=1500, padding='post')\n    return padded_sequences","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:12:15.608555Z","iopub.execute_input":"2022-07-21T07:12:15.608847Z","iopub.status.idle":"2022-07-21T07:12:15.615279Z","shell.execute_reply.started":"2022-07-21T07:12:15.608807Z","shell.execute_reply":"2022-07-21T07:12:15.613221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X['review_text'] = get_sequences(tokenizer, X['review_text'])\nX['review_text'][0]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:12:15.616436Z","iopub.execute_input":"2022-07-21T07:12:15.617171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model = tf.keras.models.Sequential([\n    tf.keras.layers.Embedding(50000, 16, input_length=35),\n    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(20, return_sequences=True)),\n    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(20)),\n    tf.keras.layers.Dense(6, activation='softmax')\n    ])\n    \nmodel.compile(\n    loss='sparse_categorical_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)\n\nmodel.fit(\n    X, y,\n    epochs=7,\n    callbacks=[\n        tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=2)\n    ])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:07:43.631396Z","iopub.execute_input":"2022-07-21T07:07:43.631692Z","iopub.status.idle":"2022-07-21T07:07:54.879208Z","shell.execute_reply.started":"2022-07-21T07:07:43.631656Z","shell.execute_reply":"2022-07-21T07:07:54.878162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test['review_text']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T07:07:54.880918Z","iopub.execute_input":"2022-07-21T07:07:54.881272Z","iopub.status.idle":"2022-07-21T07:07:54.886151Z","shell.execute_reply.started":"2022-07-21T07:07:54.881228Z","shell.execute_reply":"2022-07-21T07:07:54.88538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrfr = RandomForestRegressor()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfr.fit(X['review_text'],y)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rfr.predict(X_test)\ny_pred","metadata":{},"execution_count":null,"outputs":[]}]}