<jupyter_start><jupyter_text>Support Vector Machines <NAME>  Importing required libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import svm<jupyter_output><empty_output><jupyter_text>Dataset Description The dataset consists of several hundred human cell sample records, each of which contains the values of a set of cell characteristics. The fields in each record are:|Field name|Description||--- |--- ||ID|Clump thickness||Clump|Clump thickness||UnifSize|Uniformity of cell size||UnifShape|Uniformity of cell shape||MargAdh|Marginal adhesion||SingEpiSize|Single epithelial cell size||BareNuc|Bare nuclei||BlandChrom|Bland chromatin||NormNucl|Normal nucleoli||Mit|Mitoses||Class|Benign or malignant|  Reading the dataset<jupyter_code>dataFrame = pd.read_csv('cell_samples.csv')
dataFrame.shape
dataFrame.head()<jupyter_output><empty_output><jupyter_text>- The ID field contains the patient identifiers. - The characteristics of the cell samples from each patient are contained in fields Clump to Mit. The values are graded from 1 to 10, with 1 being the closest to benign.- The Class field contains the diagnosis, as confirmed by separate medical procedures, as to whether the samples are benign (value = 2) or malignant (value = 4). Distribution of the classes based on Clump thickness and Uniformity of cell size:<jupyter_code>ax = dataFrame[dataFrame['Class'] == 4][0:50].plot(kind='scatter', x='Clump', y='UnifSize', color='DarkBlue', label='malignant')

dataFrame[dataFrame['Class'] == 2][0:50].plot(kind='scatter', x='Clump', y='UnifSize', color='Yellow', label='benign', ax=ax)

plt.show()<jupyter_output><empty_output><jupyter_text>Data pre-processing and selection<jupyter_code>dataFrame.dtypes<jupyter_output><empty_output><jupyter_text>- __BareNuc__ column includes some values that are not numerical. We can drop those rows<jupyter_code>dataFrame = dataFrame[pd.to_numeric(dataFrame['BareNuc'], errors='coerce').notnull()]

dataFrame['BareNuc'] = dataFrame['BareNuc'].astype('int64')
dataFrame.dtypes<jupyter_output><empty_output><jupyter_text>Splitting into Features and Labels  Features<jupyter_code>X = dataFrame.iloc[:, 1:-1]
X.head()
X = np.asarray(X)
X[0:5]<jupyter_output><empty_output><jupyter_text>Labels<jupyter_code>y = dataFrame.iloc[:, -1]
y.head()
y = np.asarray(y)
y[0:5]<jupyter_output><empty_output><jupyter_text>Splitting into Training and Testing set<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
X_train.shape,  X_test.shape
y_train.shape,  y_test.shape<jupyter_output><empty_output><jupyter_text>ModelingThe SVM algorithm offers a choice of kernel functions for performing its processing. Mapping data into a higher dimensional space is called kernelling. The mathematical function used for the transformation is known as the kernel function, and can be of different types, such as:    1. Linear    2. Polynomial    3. Radial basis function (RBF)    4. Sigmoid<jupyter_code>svm = svm.SVC(kernel='rbf', gamma='auto')
svm.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>y_pred = svm.predict(X_test)
y_pred[0:5]<jupyter_output><empty_output><jupyter_text>Evaluation  1. Accuracy<jupyter_code>from sklearn import metrics

accuracy = metrics.accuracy_score(y_test, y_pred)
print('SVM Accuracy : ', accuracy)<jupyter_output>SVM Accuracy :  0.9635036496350365<jupyter_text>2. Jaccard Index<jupyter_code>from sklearn.metrics import jaccard_similarity_score

jaccard_similarity_score(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>3. Confusion Matrix<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix<jupyter_output><empty_output><jupyter_text>- __Precision__ is a measure of the accuracy provided that a class label has been predicted. It is defined by: precision = TP / (TP + FP)- __Recall__ is true positive rate. It is defined as: Recall =  TP / (TP + FN)- __F1 score:__Now we are in the position to calculate the F1 scores for each label based on the precision and recall of that label. The F1score is the harmonic average of the precision and recall, where an F1 score reaches its best value at 1 (perfect precision and recall) and worst at 0. It is a good way to show that a classifer has a good value for both recall and precision.<jupyter_code>print(classification_report(y_test, y_pred))
cm = confusion_matrix(y_test, y_pred)
print(cm)<jupyter_output>[[85  5]
 [ 0 47]]<jupyter_text>Visualize the Confusion Matrix<jupyter_code>plt.figure(figsize=(6,6))
sns.set(font_scale=1.5)
sns.heatmap(cm, annot=True, fmt='d', linewidths=.5, square = True, cmap = 'Spectral')

plt.ylabel('Actual label', size=15)
plt.xlabel('Predicted label', size=15)

algo_title='Support Vector Machines'
all_sample_title = algo_title + '\n\n' + 'Accuracy Score: {0}%'.format(accuracy)
plt.title(all_sample_title, size = 15)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10419968304278922, 'fraction_numerical': 0.015055467511885896, 'mean_word_length': 7.263502454991817, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '392276', 'license': 'MIT', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1669, 'n_words': 463, 'path': '02_Classification/04_Support Vector Machines/SVM.ipynb', 'repo_name': 'AdityaBagad/Machine-Learning-in-Python', 'size': '5054'}