{
"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": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" application_number | \n",
" title | \n",
" abstract | \n",
" main_ipcr_label | \n",
" filing_date | \n",
" examiner_id | \n",
" inventor_list | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 12745329 | \n",
" PROCESS AND PRODUCT | \n",
" Process for producing a chocolate or chocolate... | \n",
" A23G150 | \n",
" 20100907 | \n",
" 95893 | \n",
" [Rumbaut|Luc Joseph Paul Antoine Marie|Lebbeke... | \n",
"
\n",
" \n",
" 1 | \n",
" 12658202 | \n",
" Stabilized pharmaceutical compositions contain... | \n",
" The present invention provides a pharmaceutica... | \n",
" A61K31397 | \n",
" 20100203 | \n",
" 70017 | \n",
" [Wakiyama|Naoki|Yokohama-shi||JP, Usui|Fusao|Y... | \n",
"
\n",
" \n",
" 2 | \n",
" 12746833 | \n",
" MEDICAL APPLIANCE WITH A CONNECTION AND DISCON... | \n",
" A medical appliance including a medical instru... | \n",
" A61B100 | \n",
" 20100825 | \n",
" 94236 | \n",
" [Mathieu|Nicolas|Ecully||FR] | \n",
"
\n",
" \n",
" 3 | \n",
" 12843590 | \n",
" SAWHORSE BRACKET SYSTEM AND METHOD | \n",
" Embodiments of sawhorse systems are shown and ... | \n",
" B25H106 | \n",
" 20100726 | \n",
" 85438 | \n",
" [Johnson|Michael|Waterville|WA|US, Johnson|Phi... | \n",
"
\n",
" \n",
" 4 | \n",
" 12748977 | \n",
" One Piece Hip and Ridge Shingle | \n",
" The invention provides a one piece shingle for... | \n",
" E04D120 | \n",
" 20100329 | \n",
" 70726 | \n",
" [Ward|Bryan|Olathe|KS|US, Rosewall|Ray|Blue Sp... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" inventor | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" bower|christopher|raleigh|nc|us | \n",
" 99 | \n",
"
\n",
" \n",
" 1 | \n",
" menard|etienne|limonges||fr | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" meitl|matthew|durham|nc|us | \n",
" 112 | \n",
"
\n",
" \n",
" 3 | \n",
" carr|joseph|chapel hill|nc|us | \n",
" 12 | \n",
"
\n",
" \n",
" 4 | \n",
" tsai|jui-yi|newtown|pa|us | \n",
" 33 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" application_number | \n",
" title | \n",
" abstract | \n",
" main_ipcr_label | \n",
" filing_date | \n",
" examiner_id | \n",
" inventor_list | \n",
" label | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 15243228 | \n",
" METHODS OF FORMING PRINTABLE INTEGRATED CIRCUI... | \n",
" Methods of forming integrated circuit devices ... | \n",
" H01L2712 | \n",
" 20160822 | \n",
" 75592 | \n",
" ['bower|christopher|raleigh|nc|us', 'menard|et... | \n",
" H01 | \n",
"
\n",
" \n",
" 1 | \n",
" 13033229 | \n",
" RUTHENIUM CARBENE COMPLEXES FOR OLED MATERIAL | \n",
" Neutral ruthenium(II) complexes having at leas... | \n",
" C07F1500 | \n",
" 20110223 | \n",
" 60315 | \n",
" ['tsai|jui-yi|newtown|pa|us'] | \n",
" C07 | \n",
"
\n",
" \n",
" 2 | \n",
" 13218612 | \n",
" BED INSERT | \n",
" An insert for a bed facilitates use of a mattr... | \n",
" A47C2100 | \n",
" 20110826 | \n",
" 74819 | \n",
" [\"o'reagan|james r.|greer|sc|us\"] | \n",
" A47 | \n",
"
\n",
" \n",
" 3 | \n",
" 13828921 | \n",
" IMAGE DISPLAY APPARATUS AND METHOD FOR OPERATI... | \n",
" An image display apparatus and a method for op... | \n",
" G06F301 | \n",
" 20130314 | \n",
" 66365 | \n",
" ['choi|eunjung|seoul||kr', 'choi|kwangsoo|seou... | \n",
" G06 | \n",
"
\n",
" \n",
" 4 | \n",
" 15373712 | \n",
" BI-DIRECTIONAL ROLLER ASSEMBLY | \n",
" Disclosed is a method of assembling a roller a... | \n",
" F16L318 | \n",
" 20161209 | \n",
" 58099 | \n",
" ['costa|carlos|somerset|ma|us'] | \n",
" F16 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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
}