{"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-08-12T11:45:19.030751Z","iopub.execute_input":"2022-08-12T11:45:19.031738Z","iopub.status.idle":"2022-08-12T11:45:19.063832Z","shell.execute_reply.started":"2022-08-12T11:45:19.031619Z","shell.execute_reply":"2022-08-12T11:45:19.062492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.feature_extraction.text import TfidfTransformer\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:19.066328Z","iopub.execute_input":"2022-08-12T11:45:19.067655Z","iopub.status.idle":"2022-08-12T11:45:20.352141Z","shell.execute_reply.started":"2022-08-12T11:45:19.067603Z","shell.execute_reply":"2022-08-12T11:45:20.350658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"../input/nlp-getting-started/train.csv\")\ntest=pd.read_csv(\"../input/nlp-getting-started/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.353890Z","iopub.execute_input":"2022-08-12T11:45:20.354374Z","iopub.status.idle":"2022-08-12T11:45:20.435488Z","shell.execute_reply.started":"2022-08-12T11:45:20.354334Z","shell.execute_reply":"2022-08-12T11:45:20.434468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.438663Z","iopub.execute_input":"2022-08-12T11:45:20.439960Z","iopub.status.idle":"2022-08-12T11:45:20.468722Z","shell.execute_reply.started":"2022-08-12T11:45:20.439905Z","shell.execute_reply":"2022-08-12T11:45:20.467079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.471029Z","iopub.execute_input":"2022-08-12T11:45:20.471805Z","iopub.status.idle":"2022-08-12T11:45:20.486584Z","shell.execute_reply.started":"2022-08-12T11:45:20.471748Z","shell.execute_reply":"2022-08-12T11:45:20.485233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.488900Z","iopub.execute_input":"2022-08-12T11:45:20.489654Z","iopub.status.idle":"2022-08-12T11:45:20.526213Z","shell.execute_reply.started":"2022-08-12T11:45:20.489612Z","shell.execute_reply":"2022-08-12T11:45:20.524954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:50:41.422197Z","iopub.execute_input":"2022-08-12T11:50:41.422664Z","iopub.status.idle":"2022-08-12T11:50:41.439510Z","shell.execute_reply.started":"2022-08-12T11:50:41.422625Z","shell.execute_reply":"2022-08-12T11:50:41.438477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.527932Z","iopub.execute_input":"2022-08-12T11:45:20.529187Z","iopub.status.idle":"2022-08-12T11:45:20.551075Z","shell.execute_reply.started":"2022-08-12T11:45:20.529132Z","shell.execute_reply":"2022-08-12T11:45:20.549559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.552911Z","iopub.execute_input":"2022-08-12T11:45:20.553833Z","iopub.status.idle":"2022-08-12T11:45:20.575095Z","shell.execute_reply.started":"2022-08-12T11:45:20.553784Z","shell.execute_reply":"2022-08-12T11:45:20.573972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=CountVectorizer()\nword_count=count.fit_transform(train['text'])\nprint(word_count)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.576608Z","iopub.execute_input":"2022-08-12T11:45:20.577248Z","iopub.status.idle":"2022-08-12T11:45:20.791169Z","shell.execute_reply.started":"2022-08-12T11:45:20.577209Z","shell.execute_reply":"2022-08-12T11:45:20.789706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nimport nltk\nnltk.download('stopwords')\nfrom nltk.corpus import stopwords\nfrom nltk.stem.porter import PorterStemmer\ncorpus = []\nfor i in range(0,7613):\n    review = re.sub('[^a-zA-Z]','',train['text'][i])\n    review = review.lower()\n    review = review.split()\n    ps = PorterStemmer()\n    all_stopwords = stopwords.words('english')\n    review = [ps.stem(word) for word in review if not word in set(all_stopwords)]\n    review = ''.join(review)\n    corpus.append(review)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:20.795110Z","iopub.execute_input":"2022-08-12T11:45:20.795589Z","iopub.status.idle":"2022-08-12T11:45:23.742813Z","shell.execute_reply.started":"2022-08-12T11:45:20.795539Z","shell.execute_reply":"2022-08-12T11:45:23.741478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\ncv = CountVectorizer(max_features=7613)\nX=cv.fit_transform(corpus).toarray()\ny=train.iloc[:,[4]].values","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:23.744368Z","iopub.execute_input":"2022-08-12T11:45:23.744772Z","iopub.status.idle":"2022-08-12T11:45:23.841796Z","shell.execute_reply.started":"2022-08-12T11:45:23.744737Z","shell.execute_reply":"2022-08-12T11:45:23.840476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:54:06.673408Z","iopub.execute_input":"2022-08-12T11:54:06.673905Z","iopub.status.idle":"2022-08-12T11:54:06.682467Z","shell.execute_reply.started":"2022-08-12T11:54:06.673863Z","shell.execute_reply":"2022-08-12T11:54:06.681275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:54:14.163329Z","iopub.execute_input":"2022-08-12T11:54:14.164509Z","iopub.status.idle":"2022-08-12T11:54:14.171437Z","shell.execute_reply.started":"2022-08-12T11:54:14.164456Z","shell.execute_reply":"2022-08-12T11:54:14.170435Z"},"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(X,y,test_size = 0.20,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:23.843768Z","iopub.execute_input":"2022-08-12T11:45:23.844622Z","iopub.status.idle":"2022-08-12T11:45:24.327743Z","shell.execute_reply.started":"2022-08-12T11:45:23.844565Z","shell.execute_reply":"2022-08-12T11:45:24.326688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nclassifier = GaussianNB()\nclassifier.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:24.329538Z","iopub.execute_input":"2022-08-12T11:45:24.330360Z","iopub.status.idle":"2022-08-12T11:45:25.481349Z","shell.execute_reply.started":"2022-08-12T11:45:24.330307Z","shell.execute_reply":"2022-08-12T11:45:25.479939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:25.482945Z","iopub.execute_input":"2022-08-12T11:45:25.484051Z","iopub.status.idle":"2022-08-12T11:45:25.751656Z","shell.execute_reply.started":"2022-08-12T11:45:25.484003Z","shell.execute_reply":"2022-08-12T11:45:25.750628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:52:15.773336Z","iopub.execute_input":"2022-08-12T11:52:15.773833Z","iopub.status.idle":"2022-08-12T11:52:15.781362Z","shell.execute_reply.started":"2022-08-12T11:52:15.773793Z","shell.execute_reply":"2022-08-12T11:52:15.780219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:45:25.753334Z","iopub.execute_input":"2022-08-12T11:45:25.753755Z","iopub.status.idle":"2022-08-12T11:45:25.761664Z","shell.execute_reply.started":"2022-08-12T11:45:25.753718Z","shell.execute_reply":"2022-08-12T11:45:25.760333Z"},"trusted":true},"execution_count":null,"outputs":[]}]}