Heart Decease Prediction using Support Vector Machines
Jupyter Notebook
# Import required librariesimportnumpyasnpimportcsvimportcv2importsklearnimporttimefromsklearnimportpreprocessing, metrics, cross_validationfromsklearn.model_selectionimporttrain_test_splitfromsklearn.preprocessingimportStandardScalerfromsklearn.svmimportLinearSVC, SVC# NOTE: the next import is only valid for scikit-learn version <= 0.17# for scikit-learn >= 0.18 use:# from sklearn.model_selection import train_test_splitfromsklearn.cross_validationimporttrain_test_split
# Constantsdata_path="data/"csv_params= []
csv_labels= []
# Reading the content of csv filewithopen(data_path+'heart-statlog.csv') ascsv_file:
csv_reader=csv.reader(csv_file)
# Uncomment this line if file contains headers# next(csv_reader, None)foreach_lineincsv_reader:
csv_params.append(each_line[:-1])
csv_labels.append(each_line[-1])
# Converting the data type from list to numpy arrayinput_params=np.array(csv_params)
input_labels=np.array(csv_labels)
print("Shape of input params: ", input_params.shape) #(rows, columns)print("Shape of input labels: ", input_labels.shape) #(rows, columns)
Shape of input params: (270, 13)
Shape of input labels: (270,)
# Scaling the input params by using standard scalerX_scaler=StandardScaler().fit(input_params)
X_scaled=X_scaler.transform(input_params)
print("Scaled Output: ", X_scaled[0])
Scaled Output: [ 1.71209356 0.6894997 0.87092765 -0.07540984 1.40221232 -0.41702883
0.98166365 -1.75920811 -0.7012223 1.18101235 0.67641928 2.47268219
-0.87570581]
C:\Users\AKhil\Anaconda3\envs\carnd-term1\lib\site-packages\sklearn\utils\validation.py:475: DataConversionWarning: Data with input dtype <U3 was converted to float64 by StandardScaler.
warnings.warn(msg, DataConversionWarning)
# Splitting the data-set in train and test datarand_state=np.random.randint(0, 100)
X_train, X_test, y_train, y_test=train_test_split(
X_scaled, input_labels, test_size=0.2, random_state=rand_state)
print("Length of training data: ", len(X_train), len(y_train))
print("Length of test data: ", len(X_test), len(y_test))
Length of training data: 216 216
Length of test data: 54 54
# Training the SVM modelsvc=LinearSVC()
# Check the training time for the SVCt=time.time()
svc.fit(X_train, y_train)
t2=time.time()
print(round(t2-t, 2), 'Seconds to train SVC...')
# Check the score of the SVCprint('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
# Check the prediction time for a single samplet=time.time()
0.03 Seconds to train SVC...
Test Accuracy of SVC = 0.8889