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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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:32:19.372004Z",
     "start_time": "2024-12-01T08:32:18.179932Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from collections import Counter\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f47b9b42a2c1db6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:13.881724Z",
     "start_time": "2024-12-01T08:32:19.390239Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2762354, 7)\n"
     ]
    },
    {
     "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>application_number</th>\n",
       "      <th>title</th>\n",
       "      <th>abstract</th>\n",
       "      <th>main_ipcr_label</th>\n",
       "      <th>filing_date</th>\n",
       "      <th>examiner_id</th>\n",
       "      <th>inventor_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12745329</td>\n",
       "      <td>PROCESS AND PRODUCT</td>\n",
       "      <td>Process for producing a chocolate or chocolate...</td>\n",
       "      <td>A23G150</td>\n",
       "      <td>20100907</td>\n",
       "      <td>95893</td>\n",
       "      <td>[Rumbaut|Luc Joseph Paul Antoine Marie|Lebbeke...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12658202</td>\n",
       "      <td>Stabilized pharmaceutical compositions contain...</td>\n",
       "      <td>The present invention provides a pharmaceutica...</td>\n",
       "      <td>A61K31397</td>\n",
       "      <td>20100203</td>\n",
       "      <td>70017</td>\n",
       "      <td>[Wakiyama|Naoki|Yokohama-shi||JP, Usui|Fusao|Y...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12746833</td>\n",
       "      <td>MEDICAL APPLIANCE WITH A CONNECTION AND DISCON...</td>\n",
       "      <td>A medical appliance including a medical instru...</td>\n",
       "      <td>A61B100</td>\n",
       "      <td>20100825</td>\n",
       "      <td>94236</td>\n",
       "      <td>[Mathieu|Nicolas|Ecully||FR]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12843590</td>\n",
       "      <td>SAWHORSE BRACKET SYSTEM AND METHOD</td>\n",
       "      <td>Embodiments of sawhorse systems are shown and ...</td>\n",
       "      <td>B25H106</td>\n",
       "      <td>20100726</td>\n",
       "      <td>85438</td>\n",
       "      <td>[Johnson|Michael|Waterville|WA|US, Johnson|Phi...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12748977</td>\n",
       "      <td>One Piece Hip and Ridge Shingle</td>\n",
       "      <td>The invention provides a one piece shingle for...</td>\n",
       "      <td>E04D120</td>\n",
       "      <td>20100329</td>\n",
       "      <td>70726</td>\n",
       "      <td>[Ward|Bryan|Olathe|KS|US, Rosewall|Ray|Blue Sp...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   application_number                                              title  \\\n",
       "0            12745329                                PROCESS AND PRODUCT   \n",
       "1            12658202  Stabilized pharmaceutical compositions contain...   \n",
       "2            12746833  MEDICAL APPLIANCE WITH A CONNECTION AND DISCON...   \n",
       "3            12843590                 SAWHORSE BRACKET SYSTEM AND METHOD   \n",
       "4            12748977                    One Piece Hip and Ridge Shingle   \n",
       "\n",
       "                                            abstract main_ipcr_label  \\\n",
       "0  Process for producing a chocolate or chocolate...         A23G150   \n",
       "1  The present invention provides a pharmaceutica...       A61K31397   \n",
       "2  A medical appliance including a medical instru...         A61B100   \n",
       "3  Embodiments of sawhorse systems are shown and ...         B25H106   \n",
       "4  The invention provides a one piece shingle for...         E04D120   \n",
       "\n",
       "   filing_date  examiner_id                                      inventor_list  \n",
       "0     20100907        95893  [Rumbaut|Luc Joseph Paul Antoine Marie|Lebbeke...  \n",
       "1     20100203        70017  [Wakiyama|Naoki|Yokohama-shi||JP, Usui|Fusao|Y...  \n",
       "2     20100825        94236                       [Mathieu|Nicolas|Ecully||FR]  \n",
       "3     20100726        85438  [Johnson|Michael|Waterville|WA|US, Johnson|Phi...  \n",
       "4     20100329        70726  [Ward|Bryan|Olathe|KS|US, Rosewall|Ray|Blue Sp...  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = range(2010, 2019)\n",
    "df = [pd.read_csv(f'csvs/{name}.csv') for name in names]\n",
    "df = pd.concat(df, ignore_index=True, copy=False)\n",
    "df = df[~df['examiner_id'].isna()]\n",
    "df['examiner_id'] = df['examiner_id'].astype(np.int32)\n",
    "df['inventor_list'] =df['inventor_list'].apply(eval)\n",
    "print(df.shape)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d67ebb41b21a1cb0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:14.752740Z",
     "start_time": "2024-12-01T08:33:14.008339Z"
    }
   },
   "outputs": [],
   "source": [
    "# Shuffle\n",
    "df = df.sample(frac=1, random_state=42, replace=False, ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dacbae2e86c8fe24",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:14.803083Z",
     "start_time": "2024-12-01T08:33:14.798397Z"
    }
   },
   "outputs": [],
   "source": [
    "data = {}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccfa8be0912a9d60",
   "metadata": {},
   "source": [
    "## Labels\n",
    "\n",
    "The International Patent Classification (IPC) system has 4 levels of categorization, which are as follows:\n",
    "\n",
    "1. Section: There are 8 main sections, represented by the letters A to H (e.g., Section A, Section B, etc.), each covering a broad field of technology.\n",
    "2. Class: Each section is subdivided into multiple classes, typically represented by two-digit numbers. For example, A01 refers to agriculture; B01 refers to apparatus for physical or chemical processes.\n",
    "3. Subclass: Each class is further divided into subclasses, which are denoted by three-digit numbers. For example, A01B refers to agricultural machines; B01D refers to separation processes.\n",
    "4. Group: Subclasses are further divided into groups, typically indicated by more detailed numeric codes. These groups provide a more precise classification of specific technological aspects. For example, A01B1 refers to a specific type of agricultural machinery.\n",
    "\n",
    "Summary:\n",
    "\n",
    "- Section → Class → Subclass → Group\n",
    "\n",
    "Each level progresses from broad categories to more specific and detailed classifications, enabling a precise and organized system for patent description and retrieval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8a631e26df8a6c05",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:16.312660Z",
     "start_time": "2024-12-01T08:33:14.845137Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "main_ipcr_label\n",
       "D07    183\n",
       "C14    135\n",
       "B82     64\n",
       "B33     57\n",
       "G12     39\n",
       "H99      2\n",
       "F99      1\n",
       "E99      1\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We use level 2. (e.g., A01, B01,...)\n",
    "labels = df['main_ipcr_label'].str[:3]\n",
    "label_counts = df['main_ipcr_label'].str[:3].value_counts()\n",
    "label_counts.tail(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7220b0221929bb56",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:17.198111Z",
     "start_time": "2024-12-01T08:33:16.355816Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2762187, 7)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Select labels with counts >= 100\n",
    "min_counts = 100\n",
    "labels_deleted = label_counts[label_counts >= min_counts].index\n",
    "df = df[labels.isin(labels_deleted)]\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c4921ddef95b225",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:18.266008Z",
     "start_time": "2024-12-01T08:33:17.286145Z"
    }
   },
   "outputs": [],
   "source": [
    "df.loc[:, 'label'] = df['main_ipcr_label'].str[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5e68241068153a03",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:18.786890Z",
     "start_time": "2024-12-01T08:33:18.279976Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of classes 120\n"
     ]
    }
   ],
   "source": [
    "label_encoder = LabelEncoder()\n",
    "labels = label_encoder.fit_transform(df['label'])\n",
    "print('Number of classes', len(label_encoder.classes_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "84a7a7a63e001349",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:18.820476Z",
     "start_time": "2024-12-01T08:33:18.813927Z"
    }
   },
   "outputs": [],
   "source": [
    "labels = labels.astype(np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eeaa8e4a1b2bdc07",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:18.840571Z",
     "start_time": "2024-12-01T08:33:18.834743Z"
    }
   },
   "outputs": [],
   "source": [
    "data['patent_labels'] = labels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e99f2439ad2a7ba",
   "metadata": {},
   "source": [
    "## Patent-Examiner Links"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3a967c0916bf30ed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:18.868401Z",
     "start_time": "2024-12-01T08:33:18.857137Z"
    }
   },
   "outputs": [],
   "source": [
    "patent_ids = np.arange(df.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "42fdbf6230aaa6bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:19.400626Z",
     "start_time": "2024-12-01T08:33:18.885300Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(10641)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "examiner_id_encoder = LabelEncoder()\n",
    "examiner_ids = examiner_id_encoder.fit_transform(df['examiner_id'])\n",
    "examiner_ids.max()+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b690c62d181e327c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:19.435167Z",
     "start_time": "2024-12-01T08:33:19.425590Z"
    }
   },
   "outputs": [],
   "source": [
    "examiner_ids = examiner_ids.astype(np.int16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7ebb6da22eca4769",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:19.457826Z",
     "start_time": "2024-12-01T08:33:19.452414Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of patent-examiner links: 2762187\n"
     ]
    }
   ],
   "source": [
    "print('Number of patent-examiner links:', examiner_ids.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f3c172c40a4ffb1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:19.577174Z",
     "start_time": "2024-12-01T08:33:19.572744Z"
    }
   },
   "outputs": [],
   "source": [
    "data['patent-examiner'] = (patent_ids, examiner_ids)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff6515cc5b109ec",
   "metadata": {},
   "source": [
    "## Patent-Inventor Links"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fc7f29576fff34a3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:24.237691Z",
     "start_time": "2024-12-01T08:33:19.615209Z"
    }
   },
   "outputs": [],
   "source": [
    "df.loc[:, 'inventor_list'] = df['inventor_list'].apply(lambda xx: [x.lower() for x in xx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d27fb011526e4c76",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:46.322969Z",
     "start_time": "2024-12-01T08:33:42.585328Z"
    }
   },
   "outputs": [],
   "source": [
    "inventors = list(itertools.chain(*(df['inventor_list'].values)))\n",
    "inventor_count_dict = Counter(inventors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "11caff034b1389b4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:33:54.138803Z",
     "start_time": "2024-12-01T08:33:53.358330Z"
    }
   },
   "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>inventor</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bower|christopher|raleigh|nc|us</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>menard|etienne|limonges||fr</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>meitl|matthew|durham|nc|us</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>carr|joseph|chapel hill|nc|us</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>tsai|jui-yi|newtown|pa|us</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          inventor  count\n",
       "0  bower|christopher|raleigh|nc|us     99\n",
       "1      menard|etienne|limonges||fr      2\n",
       "2       meitl|matthew|durham|nc|us    112\n",
       "3    carr|joseph|chapel hill|nc|us     12\n",
       "4        tsai|jui-yi|newtown|pa|us     33"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inventor_count_df = pd.DataFrame(list(inventor_count_dict.items()), columns=['inventor', 'count'])\n",
    "inventor_count_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b59f081244865ae1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:34:10.565550Z",
     "start_time": "2024-12-01T08:34:10.547799Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count\n",
       "1    1683797\n",
       "2     494218\n",
       "3     224304\n",
       "4     126584\n",
       "5      79738\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inventor_count_df['count'].value_counts().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8770afdd302ada4c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:35:09.567262Z",
     "start_time": "2024-12-01T08:35:09.521929Z"
    }
   },
   "outputs": [],
   "source": [
    "# Select inventors with at least 2 patents\n",
    "selected_inventors = inventor_count_df['inventor'][inventor_count_df['count']>=2].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fb911b678227e9ac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:35:51.176432Z",
     "start_time": "2024-12-01T08:35:15.823056Z"
    }
   },
   "outputs": [],
   "source": [
    "inventor_id_encoder_all = LabelEncoder()\n",
    "inventor_ids_all = inventor_id_encoder_all.fit_transform(inventors)\n",
    "patent_ids_all = np.concatenate([np.full(count, i) \n",
    "                                 for i, count in enumerate(df['inventor_list'].apply(len))])\n",
    "selected_inventor_ids = inventor_id_encoder_all.transform(selected_inventors)\n",
    "masks = np.isin(inventor_ids_all, selected_inventor_ids) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fc5a14d92342af4d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:35:54.794539Z",
     "start_time": "2024-12-01T08:35:54.680524Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of patent-inventor links: 6071805\n"
     ]
    }
   ],
   "source": [
    "inventor_ids = inventor_ids_all[masks].astype(np.int32)\n",
    "patent_ids = patent_ids_all[masks]\n",
    "print('Number of patent-inventor links:', len(inventor_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b17d7ccb6baeabe8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T08:36:02.947198Z",
     "start_time": "2024-12-01T08:36:02.943592Z"
    }
   },
   "outputs": [],
   "source": [
    "data['patent-inventor'] = (patent_ids, inventor_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "003311a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "patent_years = (df['filing_date'] // 10000).values.astype(np.int16)\n",
    "data['patent_years'] = patent_years"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2c3ad5b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'patent_labels': array([115,  56,  11, ..., 115,  58,  51], dtype=int8),\n",
       " 'patent-examiner': (array([      0,       1,       2, ..., 2762184, 2762185, 2762186]),\n",
       "  array([5875, 1162, 5676, ..., 6876, 5808, 7515], dtype=int16)),\n",
       " 'patent-inventor': (array([      0,       0,       0, ..., 2762184, 2762185, 2762186]),\n",
       "  array([ 218391, 1633817, 1630717, ..., 2301356,  766552, 1247700],\n",
       "        dtype=int32)),\n",
       " 'patent_years': array([2016, 2011, 2011, ..., 2016, 2016, 2016], dtype=int16)}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96ff79e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('patent.pkl', 'wb') as f:\n",
    "    pickle.dump(data, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c12b35e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('Patent.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "964ff612",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "cbf19f89927467f1",
   "metadata": {},
   "source": [
    "### Node Features\n",
    "\n",
    "We generate embeddings as features for each node in the graph. Node embeddings are generated by passing the title and the abstract through a Sentence-BERT model and obtaining a 384-embedding vector for each movie node.\n",
    "\n",
    "\n",
    "According to [Sentence-Transformers docs](https://www.sbert.net/docs/pretrained_models.html), the **all-MiniLM-L6-v2** model provides the best quality. So we use it to generate node features.\n",
    "\n",
    "all-MiniLM-L6-v2\n",
    "\n",
    "Description:\tAll-round model tuned for many use-cases. Trained on a large and diverse dataset of over 1 billion training pairs.\n",
    "Base Model:\tnreimers/MiniLM-L6-H384-uncased\n",
    "Max Sequence Length:\t256\n",
    "Dimensions:\t384\n",
    "Normalized Embeddings:\ttrue\n",
    "Suitable Score Functions:\tdot-product (util.dot_score), cosine-similarity (util.cos_sim), euclidean distance\n",
    "Size:\t80 MB\n",
    "Pooling:\tMean Pooling\n",
    "Training Data:\t1B+ training pairs. For details, see model card.\n",
    "Model Card:\thttps://huggingface.co/sentence-transformers/all-MiniLM-L6-v2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c2278bf",
   "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>application_number</th>\n",
       "      <th>title</th>\n",
       "      <th>abstract</th>\n",
       "      <th>main_ipcr_label</th>\n",
       "      <th>filing_date</th>\n",
       "      <th>examiner_id</th>\n",
       "      <th>inventor_list</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>15243228</td>\n",
       "      <td>METHODS OF FORMING PRINTABLE INTEGRATED CIRCUI...</td>\n",
       "      <td>Methods of forming integrated circuit devices ...</td>\n",
       "      <td>H01L2712</td>\n",
       "      <td>20160822</td>\n",
       "      <td>75592</td>\n",
       "      <td>['bower|christopher|raleigh|nc|us', 'menard|et...</td>\n",
       "      <td>H01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13033229</td>\n",
       "      <td>RUTHENIUM CARBENE COMPLEXES FOR OLED MATERIAL</td>\n",
       "      <td>Neutral ruthenium(II) complexes having at leas...</td>\n",
       "      <td>C07F1500</td>\n",
       "      <td>20110223</td>\n",
       "      <td>60315</td>\n",
       "      <td>['tsai|jui-yi|newtown|pa|us']</td>\n",
       "      <td>C07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13218612</td>\n",
       "      <td>BED INSERT</td>\n",
       "      <td>An insert for a bed facilitates use of a mattr...</td>\n",
       "      <td>A47C2100</td>\n",
       "      <td>20110826</td>\n",
       "      <td>74819</td>\n",
       "      <td>[\"o'reagan|james r.|greer|sc|us\"]</td>\n",
       "      <td>A47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13828921</td>\n",
       "      <td>IMAGE DISPLAY APPARATUS AND METHOD FOR OPERATI...</td>\n",
       "      <td>An image display apparatus and a method for op...</td>\n",
       "      <td>G06F301</td>\n",
       "      <td>20130314</td>\n",
       "      <td>66365</td>\n",
       "      <td>['choi|eunjung|seoul||kr', 'choi|kwangsoo|seou...</td>\n",
       "      <td>G06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>15373712</td>\n",
       "      <td>BI-DIRECTIONAL ROLLER ASSEMBLY</td>\n",
       "      <td>Disclosed is a method of assembling a roller a...</td>\n",
       "      <td>F16L318</td>\n",
       "      <td>20161209</td>\n",
       "      <td>58099</td>\n",
       "      <td>['costa|carlos|somerset|ma|us']</td>\n",
       "      <td>F16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   application_number                                              title  \\\n",
       "0            15243228  METHODS OF FORMING PRINTABLE INTEGRATED CIRCUI...   \n",
       "1            13033229      RUTHENIUM CARBENE COMPLEXES FOR OLED MATERIAL   \n",
       "2            13218612                                         BED INSERT   \n",
       "3            13828921  IMAGE DISPLAY APPARATUS AND METHOD FOR OPERATI...   \n",
       "4            15373712                     BI-DIRECTIONAL ROLLER ASSEMBLY   \n",
       "\n",
       "                                            abstract main_ipcr_label  \\\n",
       "0  Methods of forming integrated circuit devices ...        H01L2712   \n",
       "1  Neutral ruthenium(II) complexes having at leas...        C07F1500   \n",
       "2  An insert for a bed facilitates use of a mattr...        A47C2100   \n",
       "3  An image display apparatus and a method for op...         G06F301   \n",
       "4  Disclosed is a method of assembling a roller a...         F16L318   \n",
       "\n",
       "   filing_date  examiner_id  \\\n",
       "0     20160822        75592   \n",
       "1     20110223        60315   \n",
       "2     20110826        74819   \n",
       "3     20130314        66365   \n",
       "4     20161209        58099   \n",
       "\n",
       "                                       inventor_list label  \n",
       "0  ['bower|christopher|raleigh|nc|us', 'menard|et...   H01  \n",
       "1                      ['tsai|jui-yi|newtown|pa|us']   C07  \n",
       "2                  [\"o'reagan|james r.|greer|sc|us\"]   A47  \n",
       "3  ['choi|eunjung|seoul||kr', 'choi|kwangsoo|seou...   G06  \n",
       "4                    ['costa|carlos|somerset|ma|us']   F16  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('Patent.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "22d32b65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'patent_labels': array([115,  56,  11, ..., 115,  58,  51], dtype=int8),\n",
       " 'patent-examiner': (array([      0,       1,       2, ..., 2762184, 2762185, 2762186]),\n",
       "  array([5875, 1162, 5676, ..., 6876, 5808, 7515], dtype=int16)),\n",
       " 'patent-inventor': (array([      0,       0,       0, ..., 2762184, 2762185, 2762186]),\n",
       "  array([ 218391, 1633817, 1630717, ..., 2301356,  766552, 1247700],\n",
       "        dtype=int32)),\n",
       " 'patent_years': array([2016, 2011, 2011, ..., 2016, 2016, 2016], dtype=int16)}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "with open('patent.pkl', 'rb') as f:\n",
    "    data = pickle.load(f)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cbf8349f28ec104",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "SentenceTransformer(\n",
       "  (0): Transformer({'max_seq_length': 256, 'do_lower_case': False}) with Transformer model: BertModel \n",
       "  (1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})\n",
       "  (2): Normalize()\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from sentence_transformers import SentenceTransformer\n",
    "\n",
    "device = 'cuda:0' if torch.cuda.is_available() else 'cpu'\n",
    "model = SentenceTransformer('../sentence-transformers/all-MiniLM-L6-v2', device=device)\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6c1a927b",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = df['title'] + ' ' + df['abstract']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c419b9b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Batches: 100%|██████████| 675/675 [33:12<00:00,  2.95s/it]\n"
     ]
    }
   ],
   "source": [
    "feats = model.encode(text, batch_size=4096, show_progress_bar=True, convert_to_numpy=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f3e6cfd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['patent_feats'] = feats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1c132a42",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('patent.pkl', 'wb') as f:\n",
    "    pickle.dump(data, f)"
   ]
  }
 ],
 "metadata": {
  "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}