{"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-02-01T13:15:45.292929Z","iopub.execute_input":"2023-02-01T13:15:45.294308Z","iopub.status.idle":"2023-02-01T13:15:45.331609Z","shell.execute_reply.started":"2023-02-01T13:15:45.294138Z","shell.execute_reply":"2023-02-01T13:15:45.330280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as  pd\nimport numpy as np\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:15:45.335469Z","iopub.execute_input":"2023-02-01T13:15:45.336041Z","iopub.status.idle":"2023-02-01T13:15:45.341833Z","shell.execute_reply.started":"2023-02-01T13:15:45.335999Z","shell.execute_reply":"2023-02-01T13:15:45.340780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntest=pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv\")\nsample=pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:15:45.343430Z","iopub.execute_input":"2023-02-01T13:15:45.344685Z","iopub.status.idle":"2023-02-01T13:16:34.718258Z","shell.execute_reply.started":"2023-02-01T13:15:45.344626Z","shell.execute_reply":"2023-02-01T13:16:34.716834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.721563Z","iopub.execute_input":"2023-02-01T13:16:34.721962Z","iopub.status.idle":"2023-02-01T13:16:34.748681Z","shell.execute_reply.started":"2023-02-01T13:16:34.721909Z","shell.execute_reply":"2023-02-01T13:16:34.747745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.753615Z","iopub.execute_input":"2023-02-01T13:16:34.754051Z","iopub.status.idle":"2023-02-01T13:16:34.771450Z","shell.execute_reply.started":"2023-02-01T13:16:34.754011Z","shell.execute_reply":"2023-02-01T13:16:34.770223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.772758Z","iopub.execute_input":"2023-02-01T13:16:34.773638Z","iopub.status.idle":"2023-02-01T13:16:34.788764Z","shell.execute_reply.started":"2023-02-01T13:16:34.773604Z","shell.execute_reply":"2023-02-01T13:16:34.787652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['user_id','book_id','review_id','date_added','date_updated','read_at','started_at','n_votes','n_comments'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.790615Z","iopub.execute_input":"2023-02-01T13:16:34.791220Z","iopub.status.idle":"2023-02-01T13:16:34.836102Z","shell.execute_reply.started":"2023-02-01T13:16:34.791186Z","shell.execute_reply":"2023-02-01T13:16:34.835098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(['user_id','book_id','review_id','date_added','date_updated','read_at','started_at','n_votes','n_comments'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.837535Z","iopub.execute_input":"2023-02-01T13:16:34.837906Z","iopub.status.idle":"2023-02-01T13:16:34.857121Z","shell.execute_reply.started":"2023-02-01T13:16:34.837875Z","shell.execute_reply":"2023-02-01T13:16:34.855781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['review_text'] = train['review_text'].map(lambda x: x.lower())\n\ntest['review_text'] = test['review_text'].map(lambda x: x.lower())\n","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:34.858604Z","iopub.execute_input":"2023-02-01T13:16:34.859028Z","iopub.status.idle":"2023-02-01T13:16:37.776513Z","shell.execute_reply.started":"2023-02-01T13:16:34.858922Z","shell.execute_reply":"2023-02-01T13:16:37.775253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:37.778248Z","iopub.execute_input":"2023-02-01T13:16:37.778610Z","iopub.status.idle":"2023-02-01T13:16:37.792347Z","shell.execute_reply.started":"2023-02-01T13:16:37.778580Z","shell.execute_reply":"2023-02-01T13:16:37.790775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:37.794058Z","iopub.execute_input":"2023-02-01T13:16:37.794712Z","iopub.status.idle":"2023-02-01T13:16:37.809486Z","shell.execute_reply.started":"2023-02-01T13:16:37.794677Z","shell.execute_reply":"2023-02-01T13:16:37.808106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,classification_report\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import svm\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow as tf\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:37.811487Z","iopub.execute_input":"2023-02-01T13:16:37.811906Z","iopub.status.idle":"2023-02-01T13:16:45.687616Z","shell.execute_reply.started":"2023-02-01T13:16:37.811864Z","shell.execute_reply":"2023-02-01T13:16:45.686101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.review_text\nY = train.rating\nX_train, X_test, Y_train, Y_test  = train_test_split(X, Y ,test_size = 0.2, shuffle = True , random_state = 25)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:45.689438Z","iopub.execute_input":"2023-02-01T13:16:45.690680Z","iopub.status.idle":"2023-02-01T13:16:45.956288Z","shell.execute_reply.started":"2023-02-01T13:16:45.690629Z","shell.execute_reply":"2023-02-01T13:16:45.955206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.preprocessing import sequence\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.callbacks import EarlyStopping\nfrom keras import backend as K\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import regularizers\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:45.960534Z","iopub.execute_input":"2023-02-01T13:16:45.960904Z","iopub.status.idle":"2023-02-01T13:16:45.972829Z","shell.execute_reply.started":"2023-02-01T13:16:45.960875Z","shell.execute_reply":"2023-02-01T13:16:45.971609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_words = 1500\nmax_len = 200\ntok = Tokenizer(num_words=max_words)\ntok.fit_on_texts(X_train)\nsequences = tok.texts_to_sequences(X_train)\nsequences_matrix = sequence.pad_sequences(sequences,maxlen=max_len)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:16:45.974119Z","iopub.execute_input":"2023-02-01T13:16:45.974474Z","iopub.status.idle":"2023-02-01T13:20:23.246441Z","shell.execute_reply.started":"2023-02-01T13:16:45.974446Z","shell.execute_reply":"2023-02-01T13:20:23.245141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def RNN():\n    inputs = Input(name='inputs',shape=[max_len])\n    layer = Embedding(max_words,50,input_length=max_len)(inputs)\n    layer = LSTM(64)(layer)\n    layer = Dense(128,name='FC1',activation='relu', kernel_initializer='ones')(layer)\n    layer = Activation('relu')(layer)\n    layer = Dropout(0.5)(layer)\n    layer = Dense(256,name='FC2')(layer)\n    layer = Activation('relu')(layer)\n    layer = Dropout(0.5)(layer)\n    layer = Dense(128,name='FC3')(layer)\n    layer = Activation('relu')(layer)\n    layer = Dropout(0.5)(layer)\n    layer = Dense(1,name='out_layer')(layer)\n    layer = Activation('sigmoid')(layer)\n    model = Model(inputs=inputs,outputs=layer)\n    return model\nmodel = RNN()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:20:23.248077Z","iopub.execute_input":"2023-02-01T13:20:23.248768Z","iopub.status.idle":"2023-02-01T13:20:23.742548Z","shell.execute_reply.started":"2023-02-01T13:20:23.248722Z","shell.execute_reply":"2023-02-01T13:20:23.741575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RNN()\nmodel.summary()\nmodel.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:20:23.743919Z","iopub.execute_input":"2023-02-01T13:20:23.744239Z","iopub.status.idle":"2023-02-01T13:20:24.055848Z","shell.execute_reply.started":"2023-02-01T13:20:23.744212Z","shell.execute_reply":"2023-02-01T13:20:24.054702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(sequences_matrix,Y_train,batch_size=0,epochs=0,\n          validation_split=0.2,callbacks=[EarlyStopping(monitor='val_loss',min_delta=0.01)])","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:36:50.630108Z","iopub.execute_input":"2023-02-01T13:36:50.630519Z","iopub.status.idle":"2023-02-01T13:36:51.197439Z","shell.execute_reply.started":"2023-02-01T13:36:50.630486Z","shell.execute_reply":"2023-02-01T13:36:51.196285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sequences = tok.texts_to_sequences(X_test)\ntest_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:36:53.739774Z","iopub.execute_input":"2023-02-01T13:36:53.740216Z","iopub.status.idle":"2023-02-01T13:37:17.660733Z","shell.execute_reply.started":"2023-02-01T13:36:53.740175Z","shell.execute_reply":"2023-02-01T13:37:17.659164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_total = test.review_text","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:37:17.687288Z","iopub.execute_input":"2023-02-01T13:37:17.688216Z","iopub.status.idle":"2023-02-01T13:37:17.696296Z","shell.execute_reply.started":"2023-02-01T13:37:17.688181Z","shell.execute_reply":"2023-02-01T13:37:17.695300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok.fit_on_texts(X_total)\nsequences = tok.texts_to_sequences(X_total)\nsequences_matrix = sequence.pad_sequences(sequences,maxlen=max_len)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:37:17.697525Z","iopub.execute_input":"2023-02-01T13:37:17.698540Z","iopub.status.idle":"2023-02-01T13:39:39.933562Z","shell.execute_reply.started":"2023-02-01T13:37:17.698507Z","shell.execute_reply":"2023-02-01T13:39:39.932501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_finally = tok.texts_to_sequences(X_total)\ntest_sequences_matrix = sequence.pad_sequences(test_finally,maxlen=max_len)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:39:39.934791Z","iopub.execute_input":"2023-02-01T13:39:39.935750Z","iopub.status.idle":"2023-02-01T13:40:42.041802Z","shell.execute_reply.started":"2023-02-01T13:39:39.935715Z","shell.execute_reply":"2023-02-01T13:40:42.040353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_sequences_matrix)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T13:52:52.667164Z","iopub.execute_input":"2023-02-01T13:52:52.667675Z","iopub.status.idle":"2023-02-01T14:05:01.815421Z","shell.execute_reply.started":"2023-02-01T13:52:52.667631Z","shell.execute_reply":"2023-02-01T14:05:01.814049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:21:23.152779Z","iopub.execute_input":"2023-02-01T14:21:23.153263Z","iopub.status.idle":"2023-02-01T14:21:23.160442Z","shell.execute_reply.started":"2023-02-01T14:21:23.153231Z","shell.execute_reply":"2023-02-01T14:21:23.159467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t=prediction.flatten()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:27:00.132248Z","iopub.execute_input":"2023-02-01T14:27:00.132807Z","iopub.status.idle":"2023-02-01T14:27:00.139219Z","shell.execute_reply.started":"2023-02-01T14:27:00.132758Z","shell.execute_reply":"2023-02-01T14:27:00.138081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r=pd.Series(t)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:27:19.541873Z","iopub.execute_input":"2023-02-01T14:27:19.542628Z","iopub.status.idle":"2023-02-01T14:27:19.549622Z","shell.execute_reply.started":"2023-02-01T14:27:19.542588Z","shell.execute_reply":"2023-02-01T14:27:19.548217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:27:23.700931Z","iopub.execute_input":"2023-02-01T14:27:23.701423Z","iopub.status.idle":"2023-02-01T14:27:23.713151Z","shell.execute_reply.started":"2023-02-01T14:27:23.701389Z","shell.execute_reply":"2023-02-01T14:27:23.711899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample= pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:28:55.050920Z","iopub.execute_input":"2023-02-01T14:28:55.051358Z","iopub.status.idle":"2023-02-01T14:28:55.497514Z","shell.execute_reply.started":"2023-02-01T14:28:55.051327Z","shell.execute_reply":"2023-02-01T14:28:55.496562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:28:56.671089Z","iopub.execute_input":"2023-02-01T14:28:56.672027Z","iopub.status.idle":"2023-02-01T14:28:56.686148Z","shell.execute_reply.started":"2023-02-01T14:28:56.671979Z","shell.execute_reply":"2023-02-01T14:28:56.684878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:28:59.381187Z","iopub.execute_input":"2023-02-01T14:28:59.382124Z","iopub.status.idle":"2023-02-01T14:28:59.394461Z","shell.execute_reply.started":"2023-02-01T14:28:59.382086Z","shell.execute_reply":"2023-02-01T14:28:59.393218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.DataFrame({'review_id':test.index,'rating':r,})\nsample.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:29:26.086289Z","iopub.execute_input":"2023-02-01T14:29:26.086714Z","iopub.status.idle":"2023-02-01T14:29:26.741494Z","shell.execute_reply.started":"2023-02-01T14:29:26.086683Z","shell.execute_reply":"2023-02-01T14:29:26.740524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-02-01T14:29:36.191934Z","iopub.execute_input":"2023-02-01T14:29:36.192746Z","iopub.status.idle":"2023-02-01T14:29:36.207463Z","shell.execute_reply.started":"2023-02-01T14:29:36.192702Z","shell.execute_reply":"2023-02-01T14:29:36.205753Z"},"trusted":true},"execution_count":null,"outputs":[]}]}