{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nimport gensim\nprint(os.listdir(\"../input/embeddings/GoogleNews-vectors-negative300/\"))\npath = \"../input/embeddings/GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin\"\nembeddings = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True)\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from sklearn.metrics.pairwise import cosine_similarity\ncosine_similarity([embeddings['camera'], embeddings['photo']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eaafb82c3e7fc3efb91b91f4831d3330df75ac00"},"cell_type":"code","source":"embeddings.most_similar('camera', topn=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dbb54f733770d383badbda9d34214130c14163b6"},"cell_type":"code","source":"embeddings.doesnt_match(['rahul','gandhi','sonia','sachin'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ffecf9c6cb7c8bae30a46f25424ef6a70e8c3f87"},"cell_type":"code","source":"embeddings.most_similar(positive=['king','woman'],negative=['man'],topn=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f9e1b5fdd02db9089447877d12cfd87be0f9521"},"cell_type":"code","source":"url = 'https://raw.githubusercontent.com/skathirmani/datasets/master/imdb_sentiment.csv'\nimdb = pd.read_csv(url)\nimdb.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84cc385adc3ddd329f0e6035a8234de94c354fe0"},"cell_type":"code","source":"import nltk\nstopwords = nltk.corpus.stopwords.words('english')\ndocs_vectors = pd.DataFrame()\nfor doc in imdb['review'].str.lower().str.replace('[^a-z ]',''):\n    words = nltk.word_tokenize(doc)\n    words_clean = [word for word in words if word not in stopwords]\n    temp = pd.DataFrame()\n    for word in words_clean:\n        try:\n            word_vec = pd.Series(embeddings[word])\n            temp = temp.append(word_vec, ignore_index=True)\n        except:\n            pass\n    temp_avg = temp.mean()\n    docs_vectors = docs_vectors.append(temp_avg, ignore_index=True)\ndocs_vectors.shape    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"249e8c467389b92fcc6d132715b80344b1149171"},"cell_type":"code","source":"pd.isnull(docs_vectors).sum().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6534ac707f073e1093e6aab801e6f966738dd952"},"cell_type":"code","source":"docs_vectors['sentiment'] = imdb['sentiment']\ndocs_vectors = docs_vectors.dropna()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40c1269cd32cf77730a6541cbeb24871b9963dde"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19ada65fa66b5651f7a693dae085d68da6f42f42"},"cell_type":"code","source":"train, test = train_test_split(docs_vectors, test_size=0.2, random_state=100)\ntrain_x = train.drop('sentiment',axis=1)\ntest_x = test.drop('sentiment',axis=1)\n\ntrain_y = train['sentiment']\ntest_y = test['sentiment']\nmodel = AdaBoostClassifier(n_estimators=300, random_state = 100)\nmodel.fit(train_x,train_y)\ntest_pred = model.predict(test_x)\naccuracy_score(test_y,test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cd234b6766c272b75a963151a4d5546f8d4f7ec"},"cell_type":"code","source":"model_rf = RandomForestClassifier(n_estimators=300, random_state = 100)\nmodel_rf.fit(train_x,train_y)\ntest_pred = model_rf.predict(test_x)\naccuracy_score(test_y,test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"777f8a31ac47ad8b4df23f90a533b8a69f31358d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bbff55c83b543007815f9684263c57fb917eb91"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3eff24dd355263a3d0e8ed1f859b1bd810475093"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}