Neural Network vs Random Forest vs Gaussian NB —What Should You Choose?
By: Hong Yong
Tags: Information Technology, Technology, Data Analysis, Data Science, Data

In an attempt to “predict the future”, we gather and analyze data, finding trends and patterns in it. There are tons of classifiers to choose from, for example, Random Forest, Logistic Regression, K-Nearest Neighbor, Support Vector Machine, and many more. Every classifier has its own unique characteristic, performing well in certain datasets but not in others. Hence, it is always a quest to seek out the best classifier to use for a certain data set.

In this article, we will be looking at three classifiers, namely Neural Network, Random Forest and Gaussian Naive Bayes to see how well they perform against a Breast Cancer data set from Kaggle [1].

Analyzing the data set

The data set has a total of 11 columns, stating the various attributes of a tumor and whether the tumor is benign or malignant.

The first column is the ID number of the sample taken and every row can be uniquely identified based on it.

Sample Code Number: ID Number

There are 9 attributes that will be useful in classifying the tumor.

Clump Thickness: 1–10

Uniformity of Cell Size: 1–10

Uniformity of Cell Shape: 1–10

Marginal Adhesion: 1–10

Single Epithelial Cell Size: 1–10

Bare Nuclei: 1–10

Bland Chromatin: 1–10

Normal Nucleoli: 1–10

Mitoses: 1–10

The classification of a tumor is determined by the Class column, with a 2 for benign and 4 for malignant.

Class: 2 (benign), 4 (malignant)

Preparing the data

First, we import the .csv file from Kaggle, and remove rows with null values and duplicated rows. Note that this might not work with other datasets where there are no way to uniquely identify each row, or if there are a lot of rows with null values in them.

import pandas as pd data = pd.read_csv("Breast Cancer Prediction.csv") data = data.drop_duplicates()

data = data.dropna(how='any',axis=0) # Changing the output to zeros and ones

# where benign = 0 and malignant = 1

for index, row in data.iterrows():

if row['Class'] == 2:

data.loc[index, 'Class'] = 0

else:

data.loc[index, 'Class'] = 1

We can proceed to visualize the correlation matrix between all the attributes to find out which attributes have a higher correlation with the Class attribute.

import seaborn as sns

import matplotlib.pyplot as plt correlation_matrix = data.corr() # Setting the dimensions of the heatmap

plt.subplots(figsize=(15, 15)) # Generate a custom diverging colormap

cmap = sns.diverging_palette(230, 20, as_cmap=True) # Draw the heatmap

sns.heatmap(correlation_matrix, cmap=cmap, center=0, fmt=".2f", annot=True, square=True, linewidths=.5, cbar_kws={"shrink": .5})

Correlation Matrix for the Breast Cancer Dataset

From the correlation matrix we can see that Uniformity of Cell Size, Uniformity of Cell Shape and Bare Nuclei have the highest correlation with Class. This means that they are crucial in classifying the tumor.

We will be using these three attributes and Bland Chromatin, the next best attribute, as inputs for the classifiers. This method of filtering out the attributes will be useful for larger datasets to reduce the chances of misclassification due to noise.

We split the dataset into training and test sets, with 80% for the training set, and 20% for the test set. Stratification is also used to ensure a similar distribution of Class for both the training and test sets.

data = data[["Uniformity of Cell Size", "Uniformity of Cell Shape", "Bare Nuclei", "Bland Chromatin", "Class"]].copy() training_set, test_set = train_test_split(data, test_size=0.2, shuffle=True, stratify=data['Class']) training_outputs = training_set['Class'].values

test_outputs = test_set['Class'].values training_set = training_set.drop(labels=['Class'], axis=1)

test_set = test_set.drop(labels=['Class'], axis=1)

With this, we are left with 65% benign samples and 35% malignant samples in each set. We can attempt to balance out the data by performing oversampling. Synthetic Minority Oversampling Technique (SMOTE) will be used.

from imblearn.over_sampling import SMOTE sm = SMOTE() training_set, training_outputs = sm.fit_resample(training_set, training_outputs)

This method will generate synthetic samples for the minority class, that is the malignant class. New instances are generated with the help of interpolation between the positive instances that lie together on the feature space.

Class distribution after oversampling

If we count the unique values in training_outputs using numpy.unique before and after resampling the data, we can see that the data has been balanced out.

# Converting to arrays

X = training_set.values[:,0:4]

X_test = test_set.values[:,0:4]

Y = np.array(training_outputs)

Y_test = np.array(test_outputs) # Standardize the features

sc = StandardScaler()

X = sc.fit_transform(X)

X_test = sc.fit_transform(X_test)

The dataframes are converted to arrays. The features are also standardized with StandardScaler to increase the convergence rate of the Neural Network. With this we are ready to carry out the experiment.