{"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\nprint(os.listdir(\"../input\"))\nimport gensim\nprint(os.listdir(\"../input/embeddings\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12de73882a08e2680dbfb54b033f70cf8439a384"},"cell_type":"code","source":"url = \"../input/embeddings/GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin\"\nembeddings =gensim.models.KeyedVectors.load_word2vec_format(url,binary=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"682ea4b6146607da11fb5ab268cef7bdd4889bb6"},"cell_type":"code","source":"from sklearn.metrics.pairwise import cosine_similarity\ncosine_similarity([embeddings['camera'],embeddings['quality']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"784e7ce812298a30f55d1e760cc11251d34b392f"},"cell_type":"code","source":"embeddings.most_similar('hyundai',topn=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"655333c4d87af3a3f70f3db63aa9a5a6aa32e323"},"cell_type":"code","source":"embeddings.doesnt_match(['rahul','sonia','gandhi','sachin'])  ## getting the odd man out\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7c86c24fba8490bc868a7def1ed7cdcbd418b51"},"cell_type":"code","source":"embeddings.most_similar(positive=['king','women'],negative=['man'],topn=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0de1a383bc47bf9619c646097cc6a995f469add"},"cell_type":"code","source":"url=\"https://raw.githubusercontent.com/skathirmani/datasets/master/imdb_sentiment.csv\"\nimdb=pd.read_csv(url)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9a5004373ba353e14f3c7f9d47c9b9fcd708a8f"},"cell_type":"code","source":"imdb.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd097a22f178db25a67bbd40dc484f510bad05bc"},"cell_type":"code","source":"import nltk\ndocs_vectors=pd.DataFrame()\nstopwords=nltk.corpus.stopwords.words('english')  ### do not do stemming\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:     ### looping through allthe words in a document\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()        ### calculating the mean(column sum)\n    docs_vectors=docs_vectors.append(temp_avg,ignore_index=True)\ndocs_vectors.shape   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3adc0b7dc87a7e902ef81a4577dcff3759512df"},"cell_type":"code","source":"docs_vectors ## vector representation of each word","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8538a4718338c216f170bf2e2bacdf3010d0c449"},"cell_type":"code","source":"pd.isnull(docs_vectors).sum().sum()  ##nearly 2 rows is completely missing\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bf1241344e7ebcd8f0f92014324d3c099a5d1e1"},"cell_type":"code","source":"docs_vectors['sentiment']=imdb['sentiment']\ndocs_vectors=docs_vectors.dropna()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24228a02a646d0759dac35032d1f1ffac7c861cb"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score,classification_report\nfrom sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier,GradientBoostingClassifier","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"062c0e684339c71202f38c68887570a6cc0d23fb"},"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)\ntrain_y=train['sentiment']\ntest_x=test.drop('sentiment',axis=1)\ntest_y=test['sentiment']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41366fbd90f6541e89e99c2652d37b7e7aa588b2"},"cell_type":"code","source":"ab_model = AdaBoostClassifier(n_estimators=300,random_state=100)\nab_model.fit(train_x,train_y)\nab_pred =ab_model.predict(test_x)\naccuracy_score(test_y,ab_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58f34798e0805f7ef9a09ce6d44bc9c651ab1ab5"},"cell_type":"code","source":"ab_pred[:5]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77209351c90bc9244619e70fe7d17a8df93c01bd"},"cell_type":"code","source":"gb_model = GradientBoostingClassifier(n_estimators=300,random_state=100)\ngb_model.fit(train_x,train_y)\ngb_pred =gb_model.predict(test_x)\naccuracy_score(test_y,gb_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e14442c693c6edfa7e542b17e46bec593775887b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58374a0ad08a21f27427bbd6779238785c095ec6"},"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}