<jupyter_start><jupyter_text>SVM (Support Vector Machines)Kali ini kita akan lakukan klasifikasi dengan menggunakan algoritma SVM. Kita akan lakukan training untuk sebuah model dari data historis pasien, kemudian klasifikasi apakah pasien mengidap penyakit kanker yang jinak atau ganas. Seperti biasa, kita import library terlebih dahulu:<jupyter_code>import pandas as pd
import pylab as pl
import numpy as np
import scipy.optimize as opt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
%matplotlib inline 
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Kita gunakan data kanker dari UCI machine learning repository (Asuncion dan Newman, 2007)|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|<jupyter_code>!wget -O cell_samples.csv https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/ML0101ENv3/labs/cell_samples.csv<jupyter_output>--2020-10-08 05:41:12--  https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/ML0101ENv3/labs/cell_samples.csv
Resolving s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)... 192.168.3.11
Connecting to s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)|192.168.3.11|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20675 (20K) [text/csv]
Saving to: ‘cell_samples.csv’

cell_samples.csv    100%[===================>]  20.19K  --.-KB/s    in 0.1s    

2020-10-08 05:41:13 (136 KB/s) - ‘cell_samples.csv’ saved [20675/20675]<jupyter_text>Nah sekarang kita baca data **cell_samples.csv** dengan dataframe pandas:<jupyter_code>cell_df = pd.read_csv("cell_samples.csv")
cell_df.head()<jupyter_output><empty_output><jupyter_text>Kita coba lihat kelas distribusinya berdassarkan Clump thickness dan Uniformity<jupyter_code>ax = cell_df[cell_df['Class'] == 4][0:50].plot(kind='scatter', x='Clump', y='UnifSize', color='DarkBlue', label='malignant');
cell_df[cell_df['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>Pre-processingKita cek dulu tipe data dari semua kolom:<jupyter_code>cell_df.dtypes<jupyter_output><empty_output><jupyter_text>Nah, kolom BareNuc bukan numerik, mari kita paksa untuk ganti tipe data (atau bisa kita hapus)<jupyter_code>cell_df = cell_df[pd.to_numeric(cell_df['BareNuc'], errors='coerce').notnull()]
cell_df['BareNuc'] = cell_df['BareNuc'].astype('int')
cell_df.dtypes
feature_df = cell_df[['Clump', 'UnifSize', 'UnifShape', 'MargAdh', 'SingEpiSize', 'BareNuc', 'BlandChrom', 'NormNucl', 'Mit']]
X = np.asarray(feature_df)
X[0:5]<jupyter_output><empty_output><jupyter_text>Kita mau model prediksi dengan benar, misal kalo kanker jinak(benign (=2)) atau kanker ganas (malignant (=4)). Jadi kita tambah satu kolom untuk klasifikasi. Misal kita kasi nama **Class**. Nah karena cuma boleh dua nilai, jadi kita harus ubah dulu.<jupyter_code>cell_df['Class'] = cell_df['Class'].astype('int')
y = np.asarray(cell_df['Class'])
y [0:5]<jupyter_output><empty_output><jupyter_text>--- Train/Test DatasetNah sekarang kita bagi dua dataset kita, satu sebagai train data dan satunya sebagai test data.<jupyter_code>X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
print ('Train set:', X_train.shape,  y_train.shape)
print ('Test set:', X_test.shape,  y_test.shape)<jupyter_output>Train set: (546, 9) (546,)
Test set: (137, 9) (137,)<jupyter_text>--- Model (SVM dengan Scikit-Learn)Dalam SVM ada 4 hal penting:1. Linear2. Polynomial3. Radial Basis Function (RBF)4. SigmoidUntuk ini kita belajar yang default (RBF) saja dulu ya, soalnya saya juga belum paham....<jupyter_code>from sklearn import svm
clf = svm.SVC(kernel='rbf', gamma='auto')
clf.fit(X_train, y_train) 
yhat = clf.predict(X_test)
yhat [0:5]<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, yhat, labels=[2,4])
np.set_printoptions(precision=2)

print (classification_report(y_test, yhat))

# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['Benign(2)','Malignant(4)'],normalize= False,  title='Confusion matrix')<jupyter_output>precision    recall  f1-score   support

           2       1.00      0.94      0.97        90
           4       0.90      1.00      0.95        47

    accuracy                           0.96       137
   macro avg       0.95      0.97      0.96       137
weighted avg       0.97      0.96      0.96       137

Confusion matrix, without normalization
[[85  5]
 [ 0 47]]<jupyter_text>Sekarang kita test dengan **F1_score** untuk lihat akurasinya<jupyter_code>from sklearn.metrics import f1_score
f1_score(y_test, yhat, average='weighted')<jupyter_output><empty_output><jupyter_text>Sekarang kita test dengan **Jaccard Index** untuk lihat akurasinya<jupyter_code>from sklearn.metrics import jaccard_similarity_score
jaccard_similarity_score(y_test, yhat)<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/metrics/classification.py:635: DeprecationWarning: jaccard_similarity_score has been deprecated and replaced with jaccard_score. It will be removed in version 0.23. This implementation has surprising behavior for binary and multiclass classification tasks.
  'and multiclass classification tasks.', DeprecationWarning)
{'fraction_non_alphanumeric': 0.12390524048815506, 'fraction_numerical': 0.032017229002153626, 'mean_word_length': 4.809841534612177, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '306288', 'license': 'MIT', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2493, 'n_words': 548, 'path': 'Metdat-science/Pertemuan 10/5c. Classification dengan SVM.ipynb', 'repo_name': 'nathanaeleyd/-Metodologi-Data-Science', 'size': '6969'}