Skip to main content

Partitioning Heart Data

We'll continue our descent by loading, pre-processing, and partitioning the heart disease data. This process will be similar to our previous experiment, but it'll be using the heart disease dataset instead of the iris dataset.

First, we'll load the heart disease data into a dataframe. That can be done with the following lines of code. We'll also rename the columns to be reflective of the dataset's documentation.
The "ca" and "thal" columns have some null values, so let's just remove those rows from the dataframe. There are ways to impute the data rather than just getting rid of the records, but those methods go beyond the scope of this blog and thus we'll pick the easy way out.
Afterwards, we have a couple of categorical variable, if you can recall from the previous post. As they're read in, they're represented by integral values. I'm actually not sure how mxNet deals with these. Either they can be considered categorical values and mxNet implicitly forms a sparse vector of booleans or mxNet might just treat them as true integers, with a false scaling to them. To prevent the latter from happening, I'm going to manually construct the sparse vectors of booleans for each categorical variable. Note that this observation was made while doing a bit of experimentation with this dataset and mxNet. Without splitting these categorical columns into sparse vectors, I simply couldn't get the trained network to converge to a solution that outperformed randomness.

Let's also generate our output boolean. The dataset's output is a categorical value of 0-4, where 0 is a lack of heart disease and anything above 0 represents heart disease of increasing severity. There wasn't too detailed of documentation regarding the delineation of values 1,2,3,4 so I'm going to go ahead and just clump all the positive outputs together as a 1 that indicates the presence of heart disease.
Okay, so after our categorical variables have been expanded, we're going to want to partition a training and testing set. Let's randomly generate indices for both sets and partition the original dataframe into testing and training dataframes. Like last time, we'll use an 80:20 ratio for training and testing set size.
Once we have our training and testing sets, we can partition the ground truth labels from the inputs. Let's do that.
Okay, so there we have it. We have both training and testing sets and for each set we have inputs and ground truth labels. In our training and testing sets, categorical variables have been expanded from integral values to vectors of boolean values. Let's hope that this yields a dataset that allows for convergence upon a solution.

Let's continue in the next post by attempting to train this network.

Comments

Popular posts from this blog

Classifying Flowers Part 1: Data

Everybody loves plants. So we'll continue our descent by building a neural network that can do something with plants. This construct will be able to take characteristics that describe a plant and, if we're good enough, hopefully the neural network will be able to tell us with accuracy what type of plant it is. To do this, we'll use the Iris flowers dataset, which contains 150 samples of 3 different species (50 samples each). The species in this dataset are Iris setosa, Iris versicolor, and Iris virginica. The dataset can be found at the following links. Data:  https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data ReadME:  https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.names Here's a screenshot of the data we'll be working with: Initially, this data doesn't look too exciting and if it at all confuses you, you're perfectly normal. But we can figure some immediate information just from looking at the format ...

Classifying Flowers Part 2: Training and Evaluation

In the previous post, we fiddled with data and set out for an ambitious task of classifying plants. We ended that post after having downloaded, prepared, and partitioned our data, resulting two non-overlapping sets of data: a test set and a training set. We'll continue now by building, training, and evaluating a neural network to classify these flowers. First, we'll need to separate the values that we'll use as input to the network from the values we're intending to use as the output from the network. In other words, we need to place all the sepal and petal widths and lengths into a bag that the network can read, and all the species corresponding to the individual sets of sepal and petal widths and lengths into another bag. That way, when the network trains, it only takes into account the 4 inputs. When it produces an output, we can use the other bag (the one with species inside) to see if the network has predicted the species correctly. Before doing that, we'...

Evaluating our Heart Disease Classifier

Let's continue our descent by evaluating how good our heart disease classifier is. We can do this by generating predictions on the test set and see how the predictions compare to the test set's ground truth labels. That can be done with the following lines of code: At the bottom we can see that the classifier predicted 24 true negatives, 9 false positives, 8 false negatives, and 19 true positives. That's pretty okay. There is obviously some inaccuracy in the predictions, but let's calculate the accuracy anyways. (24+19)/(24+19+8+9)=71.6. So the test accuracy was 71.6, while, if you recall from the last post, the training accuracy was nearing 90%. This disparity between training and testing accuracy is a result of overfitting. Essentially, 50,000 training loops was too much training for this little of data. The resulting network overfit to the noise inherent in the training data and, as a result, failed to generalize as well on the test set. Therefore, the testing...