{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59575,"databundleVersionId":8060720,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"> # **Do upvote**","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-21T18:37:59.168438Z","iopub.execute_input":"2024-07-21T18:37:59.168860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Importing libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport glob\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:02.398894Z","iopub.execute_input":"2024-07-21T18:38:02.399296Z","iopub.status.idle":"2024-07-21T18:38:03.089740Z","shell.execute_reply.started":"2024-07-21T18:38:02.399266Z","shell.execute_reply":"2024-07-21T18:38:03.088854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load and Inspect Data**","metadata":{}},{"cell_type":"code","source":"# Function to process patent data in chunks\ndef process_patent_data(directory_path, chunk_size=10000):\n    parquet_files = glob.glob(f\"{directory_path}/*.parquet\")\n    for file in parquet_files:\n        df = pd.read_parquet(file)\n        for start in range(0, len(df), chunk_size):\n            yield df.iloc[start:start + chunk_size]","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:05.139748Z","iopub.execute_input":"2024-07-21T18:38:05.140123Z","iopub.status.idle":"2024-07-21T18:38:05.146219Z","shell.execute_reply.started":"2024-07-21T18:38:05.140093Z","shell.execute_reply":"2024-07-21T18:38:05.145002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Path to the directory containing all Parquet files\ndirectory_path = \"/kaggle/input/uspto-explainable-ai/patent_data\"","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:07.653621Z","iopub.execute_input":"2024-07-21T18:38:07.654027Z","iopub.status.idle":"2024-07-21T18:38:07.658691Z","shell.execute_reply.started":"2024-07-21T18:38:07.653997Z","shell.execute_reply":"2024-07-21T18:38:07.657489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load a small sample of the data to inspect its structure\nsample_df = next(process_patent_data(directory_path, 10000))\nprint(sample_df.head())","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:08.056254Z","iopub.execute_input":"2024-07-21T18:38:08.056960Z","iopub.status.idle":"2024-07-21T18:38:08.217695Z","shell.execute_reply.started":"2024-07-21T18:38:08.056928Z","shell.execute_reply":"2024-07-21T18:38:08.216608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the metadata\nmeta_df = pd.read_parquet(\"/kaggle/input/uspto-explainable-ai/patent_metadata.parquet\")\nprint(meta_df.head())","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:09.542065Z","iopub.execute_input":"2024-07-21T18:38:09.542799Z","iopub.status.idle":"2024-07-21T18:38:35.682961Z","shell.execute_reply.started":"2024-07-21T18:38:09.542767Z","shell.execute_reply":"2024-07-21T18:38:35.681848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Evaluate Key Statistics**","metadata":{}},{"cell_type":"code","source":"# Basic statistics of the sample data\nprint(sample_df.describe(include='all'))","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:35.684759Z","iopub.execute_input":"2024-07-21T18:38:35.685077Z","iopub.status.idle":"2024-07-21T18:38:35.703833Z","shell.execute_reply.started":"2024-07-21T18:38:35.685050Z","shell.execute_reply":"2024-07-21T18:38:35.702867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of a few key features\nplt.figure(figsize=(10, 6))\nsns.histplot(meta_df['publication_date'], kde=True, bins=30)\nplt.title('Distribution of Publication Dates')\nplt.xlabel('Publication Date')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:38:35.705282Z","iopub.execute_input":"2024-07-21T18:38:35.705766Z","iopub.status.idle":"2024-07-21T18:40:33.572690Z","shell.execute_reply.started":"2024-07-21T18:38:35.705731Z","shell.execute_reply":"2024-07-21T18:40:33.571620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Family ID frequency\nplt.figure(figsize=(12, 6))\nsns.histplot(meta_df['family_id'].dropna(), kde=False, bins=50)\nplt.title('Frequency of Family IDs')\nplt.xlabel('Family ID')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:40:33.575619Z","iopub.execute_input":"2024-07-21T18:40:33.576056Z","iopub.status.idle":"2024-07-21T18:40:48.633264Z","shell.execute_reply.started":"2024-07-21T18:40:33.576010Z","shell.execute_reply":"2024-07-21T18:40:48.632159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Missing Data Visualisation**","metadata":{}},{"cell_type":"code","source":"import missingno as msno\n\nplt.figure(figsize=(12, 6))\nmsno.matrix(meta_df)\nplt.title('Missing Data Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:42:52.672533Z","iopub.execute_input":"2024-07-21T18:42:52.673448Z","iopub.status.idle":"2024-07-21T18:43:20.125225Z","shell.execute_reply.started":"2024-07-21T18:42:52.673417Z","shell.execute_reply":"2024-07-21T18:43:20.124029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualize Important Features**","metadata":{}},{"cell_type":"code","source":"# Number of Patents Over Time\nmeta_df['publication_date'] = pd.to_datetime(meta_df['publication_date'])\nmeta_df['year'] = meta_df['publication_date'].dt.year\npatents_per_year = meta_df['year'].value_counts().sort_index()\nplt.figure(figsize=(12, 6))\npatents_per_year.plot(kind='line')\nplt.title('Number of Patents Over Time')\nplt.xlabel('Year')\nplt.ylabel('Number of Patents')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-21T18:43:20.127239Z","iopub.execute_input":"2024-07-21T18:43:20.127575Z","iopub.status.idle":"2024-07-21T18:43:21.479607Z","shell.execute_reply.started":"2024-07-21T18:43:20.127546Z","shell.execute_reply":"2024-07-21T18:43:21.478607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Thanks for your support**","metadata":{}}]}