{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\nimport tensorflow as tf\nimport keras\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom tqdm.autonotebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.274187Z","iopub.execute_input":"2021-06-21T02:03:16.274650Z","iopub.status.idle":"2021-06-21T02:03:16.282021Z","shell.execute_reply.started":"2021-06-21T02:03:16.274572Z","shell.execute_reply":"2021-06-21T02:03:16.280768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/commonlitreadabilityprize/train.csv\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.284300Z","iopub.execute_input":"2021-06-21T02:03:16.285079Z","iopub.status.idle":"2021-06-21T02:03:16.338332Z","shell.execute_reply.started":"2021-06-21T02:03:16.285021Z","shell.execute_reply":"2021-06-21T02:03:16.336880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"../input/commonlitreadabilityprize/test.csv\")\ndf_test","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.340833Z","iopub.execute_input":"2021-06-21T02:03:16.341293Z","iopub.status.idle":"2021-06-21T02:03:16.365569Z","shell.execute_reply.started":"2021-06-21T02:03:16.341247Z","shell.execute_reply":"2021-06-21T02:03:16.364273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_text(txt):\n    return re.sub('[^A-Za-z0-9]+', ' ', str(txt).lower()).strip()\n\ndef text_cleaning(text):\n    text = ''.join([k for k in text if k not in string.punctuation])\n    text = re.sub('[^A-Za-z0-9]+', ' ', str(text).lower()).strip()\n    text = re.sub(' +', ' ', text)\n    return text","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.367624Z","iopub.execute_input":"2021-06-21T02:03:16.368256Z","iopub.status.idle":"2021-06-21T02:03:16.376178Z","shell.execute_reply.started":"2021-06-21T02:03:16.368180Z","shell.execute_reply":"2021-06-21T02:03:16.374691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sentences = df_train[\"excerpt\"].tolist()\ntrain_sentences[1]","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.378530Z","iopub.execute_input":"2021-06-21T02:03:16.379400Z","iopub.status.idle":"2021-06-21T02:03:16.393509Z","shell.execute_reply.started":"2021-06-21T02:03:16.379355Z","shell.execute_reply":"2021-06-21T02:03:16.391651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sentences = df_test[\"excerpt\"].tolist()\ntest_sentences[1]","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.395804Z","iopub.execute_input":"2021-06-21T02:03:16.396293Z","iopub.status.idle":"2021-06-21T02:03:16.406063Z","shell.execute_reply.started":"2021-06-21T02:03:16.396238Z","shell.execute_reply":"2021-06-21T02:03:16.404527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = df_train[\"target\"].tolist()","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.409945Z","iopub.execute_input":"2021-06-21T02:03:16.410669Z","iopub.status.idle":"2021-06-21T02:03:16.417966Z","shell.execute_reply.started":"2021-06-21T02:03:16.410620Z","shell.execute_reply":"2021-06-21T02:03:16.416322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = Tokenizer(oov_token=\"<OOV>\")\ntokenizer.fit_on_texts(train_sentences) #sentences is list\nword_index = tokenizer.word_index\nprint(len(word_index))","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.420646Z","iopub.execute_input":"2021-06-21T02:03:16.421195Z","iopub.status.idle":"2021-06-21T02:03:16.928063Z","shell.execute_reply.started":"2021-06-21T02:03:16.421149Z","shell.execute_reply":"2021-06-21T02:03:16.926713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\nprint(reverse_word_index[9])\n\ndef decode_sentence(text):\n    return ' '.join([reverse_word_index.get(i, '?') for i in text])\n\ndef rmse_score(y_true,y_pred):\n    return np.sqrt(mean_squared_error(y_true,y_pred))","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.930044Z","iopub.execute_input":"2021-06-21T02:03:16.930528Z","iopub.status.idle":"2021-06-21T02:03:16.946712Z","shell.execute_reply.started":"2021-06-21T02:03:16.930481Z","shell.execute_reply":"2021-06-21T02:03:16.945303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_size = len(word_index)\nembedding_dim = 16\nmax_length = 100\ntrunc_type='post'\npadding_type='post'\noov_tok = \"<OOV>\"\ntraining_size = 20000","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.948636Z","iopub.execute_input":"2021-06-21T02:03:16.949107Z","iopub.status.idle":"2021-06-21T02:03:16.958358Z","shell.execute_reply.started":"2021-06-21T02:03:16.949062Z","shell.execute_reply":"2021-06-21T02:03:16.957101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequences = tokenizer.texts_to_sequences(train_sentences)\ntrain_padded = pad_sequences(train_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)\n\ntest_sequences = tokenizer.texts_to_sequences(test_sentences)\ntest_padded = pad_sequences(test_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:16.961256Z","iopub.execute_input":"2021-06-21T02:03:16.961802Z","iopub.status.idle":"2021-06-21T02:03:17.369432Z","shell.execute_reply.started":"2021-06-21T02:03:16.961754Z","shell.execute_reply":"2021-06-21T02:03:17.368074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(decode_sentence(train_padded[0]))\nprint(train_sentences[0])\nprint(train_labels[0])","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.371761Z","iopub.execute_input":"2021-06-21T02:03:17.372240Z","iopub.status.idle":"2021-06-21T02:03:17.380862Z","shell.execute_reply.started":"2021-06-21T02:03:17.372194Z","shell.execute_reply":"2021-06-21T02:03:17.379458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Need this block to get it to work with TensorFlow 2.x\nimport numpy as np\ntrain_padded = np.asarray(train_padded)\ntrain_labels = np.asarray(train_labels)\ntest_padded = np.asarray(test_padded)\n#test_labels = np.array(test_labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.382941Z","iopub.execute_input":"2021-06-21T02:03:17.383954Z","iopub.status.idle":"2021-06-21T02:03:17.392608Z","shell.execute_reply.started":"2021-06-21T02:03:17.383906Z","shell.execute_reply":"2021-06-21T02:03:17.391171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn import preprocessing\n#train_padded = preprocessing.StandardScaler().fit(train_padded).transform(train_padded)\n#test_padded = preprocessing.StandardScaler().fit(test_padded).transform(test_padded)\n#X_train[0:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.394296Z","iopub.execute_input":"2021-06-21T02:03:17.395244Z","iopub.status.idle":"2021-06-21T02:03:17.402042Z","shell.execute_reply.started":"2021-06-21T02:03:17.395198Z","shell.execute_reply":"2021-06-21T02:03:17.400568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn import preprocessing\n#from sklearn import utils\n\n#lab_enc = preprocessing.LabelEncoder()\n#encoded = lab_enc.fit_transform(train_labels)\n#train_labels = lab_enc.fit_transform(train_labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.403374Z","iopub.execute_input":"2021-06-21T02:03:17.405110Z","iopub.status.idle":"2021-06-21T02:03:17.411991Z","shell.execute_reply.started":"2021-06-21T02:03:17.405063Z","shell.execute_reply":"2021-06-21T02:03:17.410476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(train_padded,train_labels,test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.413939Z","iopub.execute_input":"2021-06-21T02:03:17.414715Z","iopub.status.idle":"2021-06-21T02:03:17.425970Z","shell.execute_reply.started":"2021-06-21T02:03:17.414652Z","shell.execute_reply":"2021-06-21T02:03:17.424718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape,y_train.shape)\nprint(X_test.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.427495Z","iopub.execute_input":"2021-06-21T02:03:17.428236Z","iopub.status.idle":"2021-06-21T02:03:17.438793Z","shell.execute_reply.started":"2021-06-21T02:03:17.428187Z","shell.execute_reply":"2021-06-21T02:03:17.437223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn                        import metrics, svm\nfrom sklearn.linear_model           import LinearRegression\nfrom sklearn.linear_model           import LogisticRegression\nfrom sklearn.tree                   import DecisionTreeClassifier\nfrom sklearn.neighbors              import KNeighborsClassifier\nfrom sklearn.discriminant_analysis  import LinearDiscriminantAnalysis\nfrom sklearn.naive_bayes            import GaussianNB\nfrom sklearn.svm                    import SVC","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.442942Z","iopub.execute_input":"2021-06-21T02:03:17.443349Z","iopub.status.idle":"2021-06-21T02:03:17.449663Z","shell.execute_reply.started":"2021-06-21T02:03:17.443308Z","shell.execute_reply":"2021-06-21T02:03:17.448104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn import linear_model\nfrom sklearn import svm\n\nclassifiers = [\n    svm.SVR(),\n    linear_model.SGDRegressor(),\n    linear_model.BayesianRidge(),\n    linear_model.LassoLars(),\n    linear_model.ARDRegression(),\n    linear_model.PassiveAggressiveRegressor(),\n    linear_model.TheilSenRegressor(),\n    linear_model.LinearRegression()]\n\ntrainingData   = train_padded\ntrainingScores  = train_labels\npredictionData  = test_padded\n\nfor item in classifiers:\n    print(item)\n    clf = item\n    clf.fit(trainingData, trainingScores)\n    print(clf.predict(predictionData),'\\n')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:03:17.451969Z","iopub.execute_input":"2021-06-21T02:03:17.452643Z","iopub.status.idle":"2021-06-21T02:05:07.691034Z","shell.execute_reply.started":"2021-06-21T02:03:17.452566Z","shell.execute_reply":"2021-06-21T02:05:07.689688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = svm.SVR()\nclf.fit(X_train, y_train)\n\n#print(clf.predict(X_test))\npredict = clf.predict(test_padded)\npredict","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:05:07.696876Z","iopub.execute_input":"2021-06-21T02:05:07.700089Z","iopub.status.idle":"2021-06-21T02:05:08.253508Z","shell.execute_reply.started":"2021-06-21T02:05:07.700027Z","shell.execute_reply":"2021-06-21T02:05:08.252343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict = model.predict(test_padded)\n#predict","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:05:08.257197Z","iopub.execute_input":"2021-06-21T02:05:08.257537Z","iopub.status.idle":"2021-06-21T02:05:08.265052Z","shell.execute_reply.started":"2021-06-21T02:05:08.257494Z","shell.execute_reply":"2021-06-21T02:05:08.263863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_sample = pd.read_csv(\"../input/commonlitreadabilityprize/sample_submission.csv\")\nsub_sample['target'] = predict\nsub_sample.to_csv('submission.csv', index=False)\npd.set_option(\"max_colwidth\",400)\nsub_sample","metadata":{"execution":{"iopub.status.busy":"2021-06-21T02:05:08.266718Z","iopub.execute_input":"2021-06-21T02:05:08.267202Z","iopub.status.idle":"2021-06-21T02:05:08.293633Z","shell.execute_reply.started":"2021-06-21T02:05:08.267155Z","shell.execute_reply":"2021-06-21T02:05:08.292280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}