NLP_Project_Code.ipynb 3.23 KB
Newer Older
Zaid A Ali's avatar
Zaid A Ali committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
7
    "For this project, our goal is create an NLP model to automatically assign ICD-9 encodings, given the clinical notes for each encounter)."
8
9
10
11
   ]
  },
  {
   "cell_type": "code",
12
   "execution_count": null,
13
   "metadata": {},
14
   "outputs": [],
15
16
17
18
19
20
21
22
   "source": [
    "#imports\n",
    "import pandas as pd\n",
    "print(\"All modules imported successfully\")"
   ]
  },
  {
   "cell_type": "code",
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diagnoses = pd.read_csv(\"DIAGNOSES_ICD.csv\")\n",
    "diagnoses_clean = diagnoses[[\"SUBJECT_ID\", \"HADM_ID\", \"ICD9_CODE\"]]\n",
    "\n",
    "note_events = pd.read_csv(\"NOTEEVENTS.csv\", engine=\"python\", on_bad_lines='skip')\n",
    "note_events_clean = note_events[[\"SUBJECT_ID\", \"HADM_ID\",\"DESCRIPTION\", \"TEXT\"]]\n",
    "\n",
    "full_dataset = pd.merge(diagnoses_clean, note_events_clean, on =[\"HADM_ID\", \"SUBJECT_ID\"])\n",
    "full_dataset = full_dataset[:40000]\n",
    "\n",
    "print(full_dataset)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
42
43
44
45
46
47
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
48
49
50
51
      "NLTK Downloader\n",
      "---------------------------------------------------------------------------\n",
      "    d) Download   l) List    u) Update   c) Config   h) Help   q) Quit\n",
      "---------------------------------------------------------------------------\n"
52
53
54
55
     ]
    }
   ],
   "source": [
56
57
58
59
60
61
62
63
64
65
66
    "import nltk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#change to lowercase\n",
    "full_dataset = full_dataset.lower()\n",
Zaid A Ali's avatar
Zaid A Ali committed
67
    "\n",
68
69
70
71
72
    "#removing punctuation\n",
    "import string\n",
    "print(full_dataset.punctuation)\n",
    "full_dataset_p = \"\".join([char for char in text if char not in full_dataset.punctuation])\n",
    "print(full_dataset_p)"
Zaid A Ali's avatar
Zaid A Ali committed
73
74
75
76
77
78
79
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
   "source": [
    "#tokenization\n",
    "from nltk import word_tokenize\n",
    "words = word_tokenize(full_dataset_p)\n",
    "#print(words)\n",
    "\n",
    "#stopword filtering\n",
    "from nltk.corpus import stopwords\n",
    "stop_words = stopwords.words('english')\n",
    "filtered_words = [word for word in words if word not in stop_words]\n",
    "#print(filtered_words)\n",
    "\n",
    "#stemming\n",
    "from nltk.stem.porter import PorterStemmer\n",
    "porter = PorterStemmer()\n",
    "stemmed = [porter.stem(word) for word in filtered_words]\n",
    "#print(stemmed)\n",
    "\n",
    "#POS\n",
    "from nltk import pos_tag\n",
    "pos = pos_tag(filtered_words)\n",
    "print(pos)"
   ]
Zaid A Ali's avatar
Zaid A Ali committed
103
104
105
106
  }
 ],
 "metadata": {
  "kernelspec": {
107
   "display_name": "Python [conda env:nlp2021]",
Zaid A Ali's avatar
Zaid A Ali committed
108
   "language": "python",
109
   "name": "conda-env-nlp2021-py"
Zaid A Ali's avatar
Zaid A Ali committed
110
111
112
113
114
115
116
117
118
119
120
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
121
   "version": "3.8.10"
Zaid A Ali's avatar
Zaid A Ali committed
122
123
124
  }
 },
 "nbformat": 4,
125
 "nbformat_minor": 4
Zaid A Ali's avatar
Zaid A Ali committed
126
}