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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Assignment 3: Hello Vectors\n",
    "\n",
    "Welcome to this week's programming assignment of the specialization. In this assignment we will explore word vectors.\n",
    "In natural language processing, we represent each word as a vector consisting of numbers.\n",
    "The vector encodes the meaning of the word. These numbers (or weights) for each word are learned using various machine\n",
    "learning models, which we will explore in more detail later in this specialization. Rather than make you code the\n",
    "machine learning models from scratch, we will show you how to use them. In the real world, you can always load the\n",
    "trained word vectors, and you will almost never have to train them from scratch. In this assignment you will\n",
    "\n",
    "- Predict analogies between words.\n",
    "- Use PCA to reduce the dimensionality of the word embeddings and plot them in two dimensions.\n",
    "- Compare word embeddings by using a similarity measure (the cosine similarity).\n",
    "- Understand how these vector space models work.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Important Note on Submission to the AutoGrader\n",
    "\n",
    "Before submitting your assignment to the AutoGrader, please make sure you are not doing the following:\n",
    "\n",
    "1. You have not added any _extra_ `print` statement(s) in the assignment.\n",
    "2. You have not added any _extra_ code cell(s) in the assignment.\n",
    "3. You have not changed any of the function parameters.\n",
    "4. You are not using any global variables inside your graded exercises. Unless specifically instructed to do so, please refrain from it and use the local variables instead.\n",
    "5. You are not changing the assignment code where it is not required, like creating _extra_ variables.\n",
    "\n",
    "If you do any of the following, you will get something like, `Grader Error: Grader feedback not found` (or similarly unexpected) error upon submitting your assignment. Before asking for help/debugging the errors in your assignment, check for these first. If this is the case, and you don't remember the changes you have made, you can get a fresh copy of the assignment by following these [instructions](https://www.coursera.org/learn/classification-vector-spaces-in-nlp/supplement/YLuAg/h-ow-to-refresh-your-workspace)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Table of Contents\n",
    "\n",
    "- [1 - Predict the Countries from Capitals](#1)\n",
    "    - [1.1 Importing the Data](#1-1)\n",
    "    - [1.2 Cosine Similarity](#1-2)\n",
    "        - [Exercise 1 - cosine_similarity (UNQ_C1)](#ex-1)\n",
    "    - [1.3 Euclidean Distance](#1-3)\n",
    "        - [Exercise 2 - euclidean (UNQ_C2)](#ex-2)\n",
    "    - [1.4 Finding the Country of each Capital](#1-4)\n",
    "        - [Exercise 3 - get_country (UNQ_C3)](#ex-3)\n",
    "    - [1.5 Model Accuracy](#1-5)\n",
    "        - [Exercise 4 - get_accuracy (UNQ_C4)](#ex-4)\n",
    "- [2 - Plotting the vectors using PCA](#2)\n",
    "    - [Exercise 5 - compute_pca (UNQ_C5)](#ex-5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='1'></a>\n",
    "## 1 - Predict the Countries from Capitals\n",
    "\n",
    "During the presentation of the module, we have illustrated the word analogies\n",
    "by finding the capital of a country from the country. In this part of the assignment\n",
    "we have changed the problem a bit. You are asked to predict the **countries** \n",
    "that correspond to some **capitals**.\n",
    "You are playing trivia against some second grader who just took their geography test and knows all the capitals by heart.\n",
    "Thanks to NLP, you will be able to answer the questions properly. In other words, you will write a program that can give\n",
    "you the country by its capital. That way you are pretty sure you will win the trivia game. We will start by exploring the data set.\n",
    "\n",
    "<img src = './images/map.jpg' width=\"width\" height=\"height\" style=\"width:467px;height:300px;\"/>\n",
    "\n",
    "<a name='1-1'></a>\n",
    "###  1.1 Importing the Data\n",
    "\n",
    "As usual, you start by importing some essential Python libraries and the load dataset.\n",
    "The dataset will be loaded as a [Pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/dsintro.html),\n",
    "which is very a common method in data science. Because of the large size of the data,\n",
    "this may take a few minutes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run this cell to import packages.\n",
    "import pickle\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import w3_unittest\n",
    "\n",
    "from utils import get_vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city1</th>\n",
       "      <th>country1</th>\n",
       "      <th>city2</th>\n",
       "      <th>country2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Athens</td>\n",
       "      <td>Greece</td>\n",
       "      <td>Bangkok</td>\n",
       "      <td>Thailand</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Athens</td>\n",
       "      <td>Greece</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>China</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Athens</td>\n",
       "      <td>Greece</td>\n",
       "      <td>Berlin</td>\n",
       "      <td>Germany</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Athens</td>\n",
       "      <td>Greece</td>\n",
       "      <td>Bern</td>\n",
       "      <td>Switzerland</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Athens</td>\n",
       "      <td>Greece</td>\n",
       "      <td>Cairo</td>\n",
       "      <td>Egypt</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    city1 country1    city2     country2\n",
       "0  Athens   Greece  Bangkok     Thailand\n",
       "1  Athens   Greece  Beijing        China\n",
       "2  Athens   Greece   Berlin      Germany\n",
       "3  Athens   Greece     Bern  Switzerland\n",
       "4  Athens   Greece    Cairo        Egypt"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('./data/capitals.txt', delimiter=' ')\n",
    "data.columns = ['city1', 'country1', 'city2', 'country2']\n",
    "\n",
    "# print first five elements in the DataFrame\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "\n",
    "#### To Run This Code On Your Own Machine:\n",
    "Note that because the original google news word embedding dataset is about 3.64 gigabytes,\n",
    "the workspace is not able to handle the full file set.  So we've downloaded the full dataset,\n",
    "extracted a sample of the words that we're going to analyze in this assignment, and saved\n",
    "it in a pickle file called word_embeddings_capitals.p\n",
    "\n",
    "If you want to download the full dataset on your own and choose your own set of word embeddings,\n",
    "please see the instructions and some helper code.\n",
    "\n",
    "- Download the dataset from this [page](https://code.google.com/archive/p/word2vec/).\n",
    "- Search in the page for 'GoogleNews-vectors-negative300.bin.gz' and click the link to download.\n",
    "- You'll need to unzip the file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copy-paste the code below and run it on your local machine after downloading\n",
    "the dataset to the same directory as the notebook.\n",
    "\n",
    "```python\n",
    "import nltk\n",
    "from gensim.models import KeyedVectors\n",
    "\n",
    "\n",
    "embeddings = KeyedVectors.load_word2vec_format('./GoogleNews-vectors-negative300.bin', binary = True)\n",
    "f = open('capitals.txt', 'r').read()\n",
    "set_words = set(nltk.word_tokenize(f))\n",
    "select_words = words = ['king', 'queen', 'oil', 'gas', 'happy', 'sad', 'city', 'town', 'village', 'country', 'continent', 'petroleum', 'joyful']\n",
    "for w in select_words:\n",
    "    set_words.add(w)\n",
    "\n",
    "def get_word_embeddings(embeddings):\n",
    "\n",
    "    word_embeddings = {}\n",
    "    for word in embeddings.vocab:\n",
    "        if word in set_words:\n",
    "            word_embeddings[word] = embeddings[word]\n",
    "    return word_embeddings\n",
    "\n",
    "\n",
    "# Testing your function\n",
    "word_embeddings = get_word_embeddings(embeddings)\n",
    "print(len(word_embeddings))\n",
    "pickle.dump( word_embeddings, open( \"word_embeddings_subset.p\", \"wb\" ) )\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we will load the word embeddings as a [Python dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries).\n",
    "As stated, these have already been obtained through a machine learning algorithm. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "243"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_embeddings = pickle.load(open(\"./data/word_embeddings_subset.p\", \"rb\"))\n",
    "len(word_embeddings)  # there should be 243 words that will be used in this assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each of the word embedding is a 300-dimensional vector."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dimension: 300\n"
     ]
    }
   ],
   "source": [
    "print(\"dimension: {}\".format(word_embeddings['Spain'].shape[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Predict relationships among words\n",
    "\n",
    "Now you will write a function that will use the word embeddings to predict relationships among words.\n",
    "* The function will take as input three words.\n",
    "* The first two are related to each other.\n",
    "* It will predict a 4th word which is related to the third word in a similar manner as the two first words are related to each other.\n",
    "* As an example, \"Athens is to Greece as Bangkok is to ______\"?\n",
    "* You will write a program that is capable of finding the fourth word.\n",
    "* We will give you a hint to show you how to compute this.\n",
    "\n",
    "A similar analogy would be the following:\n",
    "\n",
    "<img src = './images/vectors.jpg' width=\"width\" height=\"height\" style=\"width:467px;height:200px;\"/>\n",
    "\n",
    "You will implement a function that can tell you the capital of a country.\n",
    "You should use the same methodology shown in the figure above. To do this,\n",
    "you'll first compute the cosine similarity metric or the Euclidean distance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='1-2'></a>\n",
    "### 1.2 Cosine Similarity\n",
    "\n",
    "The cosine similarity function is:\n",
    "\n",
    "$$\\cos (\\theta)=\\frac{\\mathbf{A} \\cdot \\mathbf{B}}{\\|\\mathbf{A}\\|\\|\\mathbf{B}\\|}=\\frac{\\sum_{i=1}^{n} A_{i} B_{i}}{\\sqrt{\\sum_{i=1}^{n} A_{i}^{2}} \\sqrt{\\sum_{i=1}^{n} B_{i}^{2}}}\\tag{1}$$\n",
    "\n",
    "$A$ and $B$ represent the word vectors and $A_i$ or $B_i$ represent index i of that vector. Note that if A and B are identical, you will get $cos(\\theta) = 1$.\n",
    "* Otherwise, if they are the total opposite, meaning, $A= -B$, then you would get $cos(\\theta) = -1$.\n",
    "* If you get $cos(\\theta) =0$, that means that they are orthogonal (or perpendicular).\n",
    "* Numbers between 0 and 1 indicate a similarity score.\n",
    "* Numbers between -1 and 0 indicate a dissimilarity score.\n",
    "\n",
    "<a name='ex-1'></a>\n",
    "### Exercise 1 - cosine_similarity\n",
    "Implement a function that takes in two word vectors and computes the cosine distance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>\n",
    "    <font size=\"3\" color=\"darkgreen\"><b>Hints</b></font>\n",
    "</summary>\n",
    "<p>\n",
    "<ul>\n",
    "    <li> Python's<a href=\"https://docs.scipy.org/doc/numpy/reference/\" > NumPy library </a> adds support for linear algebra operations (e.g., dot product, vector norm ...).</li>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html\" > numpy.dot </a>.</li>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html\">numpy.linalg.norm </a>.</li>\n",
    "</ul>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# UNQ_C1 GRADED FUNCTION: cosine_similarity\n",
    "\n",
    "def cosine_similarity(A, B):\n",
    "    '''\n",
    "    Input:\n",
    "        A: a numpy array which corresponds to a word vector\n",
    "        B: A numpy array which corresponds to a word vector\n",
    "    Output:\n",
    "        cos: numerical number representing the cosine similarity between A and B.\n",
    "    '''\n",
    "\n",
    "    ### START CODE HERE ###\n",
    "    dot = np.dot(A,B)    \n",
    "    norma = np.sqrt(np.dot(A,A))\n",
    "    normb = np.sqrt(np.dot(B,B))   \n",
    "    cos = dot/(norma*normb)\n",
    "\n",
    "    ### END CODE HERE ###\n",
    "    return cos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6510956"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# feel free to try different words\n",
    "king = word_embeddings['king']\n",
    "queen = word_embeddings['queen']\n",
    "\n",
    "cosine_similarity(king, queen)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Expected Output**:\n",
    "\n",
    "$\\approx$ 0.651095"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m All tests passed\n"
     ]
    }
   ],
   "source": [
    "# Test your function\n",
    "w3_unittest.test_cosine_similarity(cosine_similarity)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='1-3'></a>\n",
    "### 1.3 Euclidean Distance\n",
    "\n",
    "You will now implement a function that computes the similarity between two vectors using the Euclidean distance.\n",
    "Euclidean distance is defined as:\n",
    "\n",
    "$$ \\begin{aligned} d(\\mathbf{A}, \\mathbf{B})=d(\\mathbf{B}, \\mathbf{A}) &=\\sqrt{\\left(A_{1}-B_{1}\\right)^{2}+\\left(A_{2}-B_{2}\\right)^{2}+\\cdots+\\left(A_{n}-B_{n}\\right)^{2}} \\\\ &=\\sqrt{\\sum_{i=1}^{n}\\left(A_{i}-B_{i}\\right)^{2}} \\end{aligned}$$\n",
    "\n",
    "* $n$ is the number of elements in the vector\n",
    "* $A$ and $B$ are the corresponding word vectors. \n",
    "* The more similar the words, the more likely the Euclidean distance will be close to 0. \n",
    "\n",
    "<a name='ex-2'></a>\n",
    "### Exercise 2 - euclidean\n",
    "Implement a function that computes the Euclidean distance between two vectors."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>    \n",
    "<summary>\n",
    "    <font size=\"3\" color=\"darkgreen\"><b>Hints</b></font>\n",
    "</summary>\n",
    "<p>\n",
    "<ul>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html\" > numpy.linalg.norm </a>.</li>\n",
    "</ul>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# UNQ_C2 GRADED FUNCTION: euclidean\n",
    "\n",
    "def euclidean(A, B):\n",
    "    \"\"\"\n",
    "    Input:\n",
    "        A: a numpy array which corresponds to a word vector\n",
    "        B: A numpy array which corresponds to a word vector\n",
    "    Output:\n",
    "        d: numerical number representing the Euclidean distance between A and B.\n",
    "    \"\"\"\n",
    "\n",
    "    ### START CODE HERE ###\n",
    "\n",
    "    # euclidean distance    \n",
    "    d = np.sqrt(np.sum((A-B)**2))\n",
    "\n",
    "    ### END CODE HERE ###\n",
    "\n",
    "    return d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4796925"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test your function\n",
    "euclidean(king, queen)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Expected Output:**\n",
    "\n",
    "2.4796925"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m All tests passed\n"
     ]
    }
   ],
   "source": [
    "# Test your function\n",
    "w3_unittest.test_euclidean(euclidean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='1-4'></a>\n",
    "### 1.4 Finding the Country of each Capital\n",
    "\n",
    "Now, you  will use the previous functions to compute similarities between vectors,\n",
    "and use these to find the capital cities of countries. You will write a function that\n",
    "takes in three words, and the embeddings dictionary. Your task is to find the\n",
    "capital cities. For example, given the following words: \n",
    "\n",
    "- 1: Athens 2: Greece 3: Baghdad,\n",
    "\n",
    "your task is to predict the country 4: Iraq.\n",
    "\n",
    "<a name='ex-3'></a>\n",
    "### Exercise 3 - get_country\n",
    "**Instructions**: \n",
    "\n",
    "1. To predict the capital you might want to look at the *King - Man + Woman = Queen* example above, and implement that scheme into a mathematical function, using the word embeddings and a similarity function.\n",
    "\n",
    "2. Iterate over the embeddings dictionary and compute the cosine similarity score between your vector and the current word embedding.\n",
    "\n",
    "3. You should add a check to make sure that the word you return is not any of the words that you fed into your function. Return the one with the highest score."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# UNQ_C3 GRADED FUNCTION: get_country\n",
    "\n",
    "def get_country(city1, country1, city2, embeddings, cosine_similarity=cosine_similarity):\n",
    "    \"\"\"\n",
    "    Input:\n",
    "        city1: a string (the capital city of country1)\n",
    "        country1: a string (the country of capital1)\n",
    "        city2: a string (the capital city of country2)\n",
    "        embeddings: a dictionary where the keys are words and values are their emmbeddings\n",
    "    Output:\n",
    "        country: a tuple with the most likely country and its similarity score\n",
    "    \"\"\"\n",
    "    ### START CODE HERE ###\n",
    "\n",
    "    # store the city1, country 1, and city 2 in a set called group\n",
    "    group = set((city1, country1, city2))\n",
    "\n",
    "    # get embeddings of city 1\n",
    "    city1_emb = embeddings[city1]\n",
    "\n",
    "    # get embedding of country 1\n",
    "    country1_emb = embeddings[country1]\n",
    "\n",
    "    # get embedding of city 2\n",
    "    city2_emb = embeddings[city2]\n",
    "\n",
    "    # get embedding of country 2 (it's a combination of the embeddings of country 1, city 1 and city 2)\n",
    "    # Remember: King - Man + Woman = Queen\n",
    "    vec = country1_emb-city1_emb+city2_emb\n",
    "\n",
    "    # Initialize the similarity to -1 (it will be replaced by a similarities that are closer to +1)\n",
    "    similarity = -1\n",
    "\n",
    "    # initialize country to an empty string\n",
    "    country = ''\n",
    "\n",
    "    # loop through all words in the embeddings dictionary\n",
    "    for word in embeddings.keys():\n",
    "\n",
    "        # first check that the word is not already in the 'group'\n",
    "        if word not in group:\n",
    "\n",
    "            # get the word embedding\n",
    "            word_emb = embeddings[word]\n",
    "\n",
    "            # calculate cosine similarity between embedding of country 2 and the word in the embeddings dictionary\n",
    "            cur_similarity = cosine_similarity(vec,word_emb)\n",
    "\n",
    "            # if the cosine similarity is more similar than the previously best similarity...\n",
    "            if cur_similarity > similarity:\n",
    "\n",
    "                # update the similarity to the new, better similarity\n",
    "                similarity = cur_similarity\n",
    "\n",
    "                # store the country as a tuple, which contains the word and the similarity\n",
    "                country = (word,similarity)\n",
    "\n",
    "    ### END CODE HERE ###\n",
    "\n",
    "    return country"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Egypt', 0.7626822)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Testing your function, note to make it more robust you can return the 5 most similar words.\n",
    "get_country('Athens', 'Greece', 'Cairo', word_embeddings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Expected Output: (Approximately)**\n",
    "\n",
    "('Egypt', 0.7626821)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m All tests passed\n"
     ]
    }
   ],
   "source": [
    "# Test your function\n",
    "w3_unittest.test_get_country(get_country)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='1-5'></a>\n",
    "### 1.5 Model Accuracy\n",
    "\n",
    "Now you will test your new function on the dataset and check the accuracy of the model:\n",
    "\n",
    "$$\\text{Accuracy}=\\frac{\\text{Correct # of predictions}}{\\text{Total # of predictions}}$$\n",
    "\n",
    "<a name='ex-4'></a>\n",
    "### Exercise 4 - get_accuracy\n",
    "**Instructions**: Implement a program that can compute the accuracy on the dataset provided for you. You have to iterate over every row to get the corresponding words and feed them into you `get_country` function above. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>\n",
    "    <font size=\"3\" color=\"darkgreen\"><b>Hints</b></font>\n",
    "</summary>\n",
    "<p>\n",
    "<ul>\n",
    "    <li>Use <a href=\"https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html\" > pandas.DataFrame.iterrows </a>.</li>\n",
    "</ul>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# UNQ_C4 GRADED FUNCTION: get_accuracy\n",
    "\n",
    "def get_accuracy(word_embeddings, data, get_country=get_country):\n",
    "    '''\n",
    "    Input:\n",
    "        word_embeddings: a dictionary where the key is a word and the value is its embedding\n",
    "        data: a pandas DataFrame containing all the country and capital city pairs\n",
    "\n",
    "    '''\n",
    "\n",
    "    ### START CODE HERE ###\n",
    "    # initialize num correct to zero\n",
    "    num_correct = 0\n",
    "\n",
    "    # loop through the rows of the dataframe\n",
    "    for i, row in data.iterrows():\n",
    "\n",
    "        # get city1\n",
    "        city1 = row['city1']\n",
    "\n",
    "        # get country1\n",
    "        country1 = row['country1']\n",
    "\n",
    "        # get city2\n",
    "        city2 = row['city2']\n",
    "\n",
    "        # get country2\n",
    "        country2 = row['country2']\n",
    "\n",
    "        # use get_country to find the predicted country2\n",
    "        predicted_country2, _ = get_country(city1,country1,city2,word_embeddings)\n",
    "\n",
    "        # if the predicted country2 is the same as the actual country2...\n",
    "        if predicted_country2 == country2:\n",
    "            # increment the number of correct by 1\n",
    "            num_correct += 1\n",
    "\n",
    "    # get the number of rows in the data dataframe (length of dataframe)\n",
    "    m = len(data)\n",
    "\n",
    "    # calculate the accuracy by dividing the number correct by m\n",
    "    accuracy = num_correct /m\n",
    "\n",
    "    ### END CODE HERE ###\n",
    "    return accuracy\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE: The cell below takes about 30 SECONDS to run.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy is 0.92\n"
     ]
    }
   ],
   "source": [
    "accuracy = get_accuracy(word_embeddings, data)\n",
    "print(f\"Accuracy is {accuracy:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Expected Output:**\n",
    "\n",
    "$\\approx$ 0.92"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m All tests passed\n"
     ]
    }
   ],
   "source": [
    "# Test your function\n",
    "w3_unittest.test_get_accuracy(get_accuracy, data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='2'></a>\n",
    "## 2 - Plotting the vectors using PCA\n",
    "\n",
    "Now you will explore the distance between word vectors after reducing their dimension.\n",
    "The technique we will employ is known as\n",
    "[*principal component analysis* (PCA)](https://en.wikipedia.org/wiki/Principal_component_analysis).\n",
    "As we saw, we are working in a 300-dimensional space in this case.\n",
    "Although from a computational perspective we were able to perform a good job,\n",
    "it is impossible to visualize results in such high dimensional spaces.\n",
    "\n",
    "You can think of PCA as a method that projects our vectors in a space of reduced\n",
    "dimension, while keeping the maximum information about the original vectors in\n",
    "their reduced counterparts. In this case, by *maximum infomation* we mean that the\n",
    "Euclidean distance between the original vectors and their projected siblings is\n",
    "minimal. Hence vectors that were originally close in the embeddings dictionary,\n",
    "will produce lower dimensional vectors that are still close to each other.\n",
    "\n",
    "You will see that when you map out the words, similar words will be clustered\n",
    "next to each other. For example, the words 'sad', 'happy', 'joyful' all describe\n",
    "emotion and are supposed to be near each other when plotted.\n",
    "The words: 'oil', 'gas', and 'petroleum' all describe natural resources.\n",
    "Words like 'city', 'village', 'town' could be seen as synonyms and describe a\n",
    "similar thing.\n",
    "\n",
    "Before plotting the words, you need to first be able to reduce each word vector\n",
    "with PCA into 2 dimensions and then plot it. The steps to compute PCA are as follows:\n",
    "\n",
    "1. Mean normalize the data\n",
    "2. Compute the covariance matrix of your data ($\\Sigma$). \n",
    "3. Compute the eigenvectors and the eigenvalues of your covariance matrix\n",
    "4. Multiply the first K eigenvectors by your normalized data. The transformation should look something as follows:\n",
    "\n",
    "<img src = './images/word_embf.jpg' width=\"width\" height=\"height\" style=\"width:800px;height:200px;\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name='ex-5'></a>\n",
    "### Exercise 5 - compute_pca\n",
    "\n",
    "**Instructions**: \n",
    "\n",
    "Implement a program that takes in a data set where each row corresponds to a word vector. \n",
    "* The word vectors are of dimension 300. \n",
    "* Use PCA to change the 300 dimensions to `n_components` dimensions. \n",
    "* The new matrix should be of dimension `m, n_components`. \n",
    "\n",
    "* First de-mean the data\n",
    "* Get the eigenvalues using `linalg.eigh`.  Use 'eigh' rather than 'eig' since R is symmetric.  The performance gain when using eigh instead of eig is substantial.\n",
    "* Sort the eigenvectors and eigenvalues by decreasing order of the eigenvalues.\n",
    "* Get a subset of the eigenvectors (choose how many principle components you want to use using n_components).\n",
    "* Return the new transformation of the data by multiplying the eigenvectors with the original data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>\n",
    "    <font size=\"3\" color=\"darkgreen\"><b>Hints</b></font>\n",
    "</summary>\n",
    "<p>\n",
    "<ul>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html\" > numpy.mean(a,axis=None) </a> which takes one required parameter. You need to specify the optional argument axis for this exercise: If you set <code>axis = 0</code>, you take the mean for each column.  If you set <code>axis = 1</code>, you take the mean for each row.  Remember that each row is a word vector, and the number of columns are the number of dimensions in a word vector. </li>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html\" > numpy.cov(m, rowvar=True) </a> which takes one required parameter. You need to specify the optional argument rowvar for this exercise. This calculates the covariance matrix.  By default <code>rowvar</code> is <code>True</code>.  From the documentation: \"If rowvar is True (default), then each row represents a variable, with observations in the columns.\"  In our case, each row is a word vector observation, and each column is a feature (variable). </li>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigh.html\" > numpy.linalg.eigh(a, UPLO='L') </a> </li>\n",
    "    <li>Use <a href=\"https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html\" > numpy.argsort </a> sorts the values in an array from smallest to largest, then returns the indices from this sort. </li>\n",
    "    <li>In order to reverse the order of a list, you can use: <code>x[::-1]</code>.</li>\n",
    "    <li>To apply the sorted indices to eigenvalues, you can use this format <code>x[indices_sorted]</code>.</li>\n",
    "    <li>When applying the sorted indices to eigen vectors, note that each column represents an eigenvector.  In order to preserve the rows but sort on the columns, you can use this format <code>x[:,indices_sorted]</code></li>\n",
    "    <li>To transform the data using a subset of the most relevant principle components, take the matrix multiplication of the eigenvectors with the original data.  </li>\n",
    "    <li>The data is of shape <code>(n_observations, n_features)</code>.  </li>\n",
    "    <li>The subset of eigenvectors are in a matrix of shape <code>(n_features, n_components)</code>.</li>\n",
    "    <li>To multiply these together, take the transposes of both the eigenvectors <code>(n_components, n_features)</code> and the data (n_features, n_observations).</li>\n",
    "    <li>The product of these two has dimensions <code>(n_components,n_observations)</code>.  Take its transpose to get the shape <code>(n_observations, n_components)</code>.</li>\n",
    "</ul>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# UNQ_C5 GRADED FUNCTION: compute_pca\n",
    "\n",
    "\n",
    "def compute_pca(X, n_components=2):\n",
    "    \"\"\"\n",
    "    Input:\n",
    "        X: of dimension (m,n) where each row corresponds to a word vector\n",
    "        n_components: Number of components you want to keep.\n",
    "    Output:\n",
    "        X_reduced: data transformed in 2 dims/columns + regenerated original data\n",
    "    pass in: data as 2D NumPy array\n",
    "    \"\"\"\n",
    "\n",
    "    ### START CODE HERE ###\n",
    "    # mean center the data\n",
    "    X_demeaned = X - np.mean(X,axis=0)\n",
    "\n",
    "    # calculate the covariance matrix\n",
    "    covariance_matrix = np.cov(X_demeaned, rowvar=False)\n",
    "\n",
    "    # calculate eigenvectors & eigenvalues of the covariance matrix\n",
    "    eigen_vals, eigen_vecs = np.linalg.eigh(covariance_matrix, UPLO='L')\n",
    "\n",
    "    # sort eigenvalue in increasing order (get the indices from the sort)\n",
    "    idx_sorted = np.argsort(eigen_vals)\n",
    "    \n",
    "    # reverse the order so that it's from highest to lowest.\n",
    "    idx_sorted_decreasing = idx_sorted[::-1]\n",
    "\n",
    "    # sort the eigen values by idx_sorted_decreasing\n",
    "    eigen_vals_sorted = eigen_vals[idx_sorted_decreasing]\n",
    "\n",
    "    # sort eigenvectors using the idx_sorted_decreasing indices\n",
    "    eigen_vecs_sorted = eigen_vecs[:,idx_sorted_decreasing]\n",
    "\n",
    "    # select the first n eigenvectors (n is desired dimension\n",
    "    # of rescaled data array, or n_components)\n",
    "    eigen_vecs_subset = eigen_vecs_sorted[:,0:n_components]\n",
    "\n",
    "    # transform the data by multiplying the transpose of the eigenvectors with the transpose of the de-meaned data\n",
    "    # Then take the transpose of that product.\n",
    "    X_reduced = np.dot(eigen_vecs_subset.transpose(),X_demeaned.transpose()).transpose()\n",
    "\n",
    "    ### END CODE HERE ###\n",
    "\n",
    "    return X_reduced\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your original matrix was (3, 10) and it became:\n",
      "[[ 0.43437323  0.49820384]\n",
      " [ 0.42077249 -0.50351448]\n",
      " [-0.85514571  0.00531064]]\n"
     ]
    }
   ],
   "source": [
    "# Testing your function\n",
    "np.random.seed(1)\n",
    "X = np.random.rand(3, 10)\n",
    "X_reduced = compute_pca(X, n_components=2)\n",
    "print(\"Your original matrix was \" + str(X.shape) + \" and it became:\")\n",
    "print(X_reduced)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92m All tests passed\n"
     ]
    }
   ],
   "source": [
    "# Test your function\n",
    "w3_unittest.test_compute_pca(compute_pca)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Expected Output:**\n",
    "\n",
    "Your original matrix was: (3,10) and it became:\n",
    "\n",
    "<table>\n",
    "    <tr>\n",
    "        <td>\n",
    "           0.43437323\n",
    "            </td>\n",
    "                <td>\n",
    "            0.49820384\n",
    "            </td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>\n",
    "            0.42077249\n",
    "            </td>\n",
    "                <td>\n",
    "           -0.50351448\n",
    "            </td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>\n",
    "            -0.85514571\n",
    "            </td>\n",
    "                <td>\n",
    "           0.00531064\n",
    "            </td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "Now you will use your pca function to plot a few words we have chosen for you.\n",
    "You will see that similar words tend to be clustered near each other.\n",
    "Sometimes, even antonyms tend to be clustered near each other. Antonyms\n",
    "describe the same thing but just tend to be on the other end of the scale\n",
    "They are usually found in the same location of a sentence,\n",
    "have the same parts of speech, and thus when\n",
    "learning the word vectors, you end up getting similar weights. In the next week\n",
    "we will go over how you learn them, but for now let's just enjoy using them.\n",
    "\n",
    "**Instructions:** Run the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You have 11 words each of 300 dimensions thus X.shape is: (11, 300)\n"
     ]
    }
   ],
   "source": [
    "words = ['oil', 'gas', 'happy', 'sad', 'city', 'town',\n",
    "         'village', 'country', 'continent', 'petroleum', 'joyful']\n",
    "\n",
    "# given a list of words and the embeddings, it returns a matrix with all the embeddings\n",
    "X = get_vectors(word_embeddings, words)\n",
    "\n",
    "print('You have 11 words each of 300 dimensions thus X.shape is:', X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZwAAAD8CAYAAABDwhLXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAnt0lEQVR4nO3deXRV1fn/8fdjQIhCjQICQSQOlDJlkDAUGlAQUEtlLiIog4q0VTt8m29x0VVRcWml3zq0VItVUKGCBEjR2uKA/ATFSiIJo1S0UUkQIghCCRXh+f2RmzRgQgK5Ofcm+bzWuosz7HP2cy5ZebL32Wcfc3dERERq2hmRDkBEROoHJRwREQmEEo6IiARCCUdERAKhhCMiIoFQwhERkUDU+4RjZglmtinScYiI1HX1PuGIiEgwGkQ6gHAxs7OB54ELgBjgXqAD8D0gFngLuNXd3cy6AU+FDn05AuGKiNQ7Fs0zDTRv3twTEhKqVPbzzz/niy++oF27dgAcPXoUd6dBg+Kc+q9//Ytzzz2XuLg4tmzZQtu2bWnatCk7duxg//79dO7cuaYuQ0QkMNnZ2Z+5e4tIx1GeqG7hJCQkkJWVVaWy//znPxk0aBADBgxgyJAhpKWlsWTJEh588EEOHTpE48aNufnmm5k6dSqJiYls27YNgA0bNnD99ddXuR4RkWhmZh9FOoaKRHXCORXf/OY3effdd3nppZf45S9/yYABA5g9ezZZWVm0bduWGTNmcPjw4UiHKSJSb9WZQQMFBQWcddZZjB8/nvT0dN59910AmjdvzsGDB8nIyAAgLi6OuLg41qxZA8CCBQsiFrOISH1SZ1o4GzduJD09nTPOOIOGDRvy2GOPkZmZSZcuXWjVqhXdu3cvLTt37lwmT56MmTFo0KAIRi0iUn9E9aCB1NRU170VEZGqM7Nsd0+NdBzlqTN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XdHf/S9DxVOZk8ZZdcXc3s4rGtrdz93wzuxhYaWYb3f2DcMdaT7wAPOfu/zGzWylunfWPcEx1wbsU/5weNLNrgEygfWRDAjNrAiwBfuLuX0Q6nrqi3iQcd7+ymqfIB8r+VXtBaFuNOFm8ZrbLzFq7+85QU353BefID/37oZmtovivtSASTlW+q5IyO8ysAXAOsCeA2MpTabzuXja2P1F8Hy1aBfqzWh1lf5m7+0tm9gcza+7uEZsk08waUpxsFrj70nKK1JrvN9qoS63q1gHtzewiMzuT4hvdgY78KmM5MCG0PAH4WgvNzM41s0ah5eZAH2BLQPFV5bsqew2jgJUeuiMbAZXGe0If/bUU9+1Hq+XAjaHRVL2A/WW6YKOKmbUquXdnZj0o/p0UqT88CMXyJLDV3X9bQbFa8/1GnUiPWoiGDzCc4n7Y/wC7gBWh7fHAS2XKXUPxqJUPKO6Ki1S8zYDXgPeBV4HzQttTgT+FlnsDGykecbURuCngGL/2XQH3ANeGlhsDi4HtwDvAxRH+Gags3vuBzaHv83XgWxGM9TlgJ3Ak9HN7EzAVmBrab8Ds0LVspIKRl1ES621lvte3gd4R/jn4DuDABiAn9LkmWr/f2vbR1DYiIhIIdamJiEgglHBERCQQSjgiIhIIJRwREQmEEo6IiARCCUdERAKhhCMiIoH4/1img1fgibz1AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# We have done the plotting for you. Just run this cell.\n",
    "result = compute_pca(X, 2)\n",
    "plt.scatter(result[:, 0], result[:, 1])\n",
    "for i, word in enumerate(words):\n",
    "    plt.annotate(word, xy=(result[i, 0] - 0.05, result[i, 1] + 0.1))\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**What do you notice?**\n",
    "\n",
    "The word vectors for gas, oil and petroleum appear related to each other,\n",
    "because their vectors are close to each other.  Similarly, sad, joyful\n",
    "and happy all express emotions, and are also near each other."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "encoding": "# -*- coding: utf-8 -*-"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}