0% found this document useful (0 votes)
6 views7 pages

VL Assignment4

This document is a submission for Homework Assignment #4 in CS156 (Introduction to AI) by student Vu Q Le. It details the implementation of a machine learning model using PCA and SVM for classifying cancer types based on input data, including data preprocessing, model training, and evaluation metrics such as accuracy and confusion matrices. The results indicate high accuracy on both training and test sets, demonstrating the effectiveness of the model.

Uploaded by

10915828
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

VL Assignment4

This document is a submission for Homework Assignment #4 in CS156 (Introduction to AI) by student Vu Q Le. It details the implementation of a machine learning model using PCA and SVM for classifying cancer types based on input data, including data preprocessing, model training, and evaluation metrics such as accuracy and confusion matrices. The results indicate high accuracy on both training and test sets, demonstrating the effectiveness of the model.

Uploaded by

10915828
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VL_Assignment4 3/14/22, 6:37 PM

CS156 (Introduction to AI), Spring 2022

Homework Assignment #4 submission


Student Name: Vu Q Le

Student ID: 014769391

Email address: [Link]@[Link]

Solution
In [1]:
import numpy as np
import pandas as pd
import [Link] as plt
import [Link] as plt
from [Link] import PCA
from [Link] import StandardScaler
from sklearn.model_selection import train_test_split, StratifiedKFold
from [Link] import LinearSVC
from [Link] import SVC
from [Link] import accuracy_score, precision_score, recall_score
from sklearn.model_selection import cross_val_score
from [Link] import plot_confusion_matrix
from collections import Counter
from [Link] import classification_report
from sklearn.model_selection import GridSearchCV
[Link](42)

In [2]:
#Import data and view data
df = pd.read_csv("homework4_input_data.csv")
df

about:srcdoc Page 1 of 7
VL_Assignment4 3/14/22, 6:37 PM

Out[2]: id ASS1 SPX C6orf141 SP5 SP6 ITGA8 ATP2A1 ATP2A3

TCGA-
AB-
0 3.935027 0.523329 0.000000 0.000000 0.102277 2.686908 2.837357 7.444575
2828-
03

TCGA-
AB-
1 3.372801 0.000000 0.000000 0.000000 0.116270 2.083429 5.567935 8.361999
2846-
03

TCGA-
AB-
2 4.198301 0.000000 0.000000 0.000000 0.249176 1.546059 6.605116 9.138359
2870-
03

TCGA-
AB-
3 4.115014 0.000000 0.000000 0.222018 0.632254 2.158989 6.858708 9.201254
2872-
03

TCGA-
AB-
4 3.662169 0.467823 0.000000 0.000000 0.000000 1.183388 5.567935 9.040883
2881-
03

... ... ... ... ... ... ... ... ...

TCGA-
PG-
4331 6.752567 2.061867 2.220238 1.670146 2.089802 4.105404 2.631701 3.974129
A5BC-
01

TCGA-
PG-
4332 6.481851 0.195973 3.082744 3.152085 1.974102 2.222543 2.449799 5.039194
A6IB-
01

TCGA-
PG-
4333 7.776659 0.859361 2.016015 1.995366 3.934322 2.520344 3.657478 3.805650
A7D5-
01

TCGA-
PG-
4334 8.421619 1.287896 0.000000 0.147612 2.094130 2.266953 1.881556 3.754020
A914-
01

TCGA-
PG-
4335 6.678433 0.859361 5.170569 0.631781 1.974102 2.234900 2.836151 3.720234
A915-
01

4336 rows × 3002 columns

about:srcdoc Page 2 of 7
VL_Assignment4 3/14/22, 6:37 PM

In [3]:
from [Link] import PCA

x = [Link](['id','Class'], axis =1).values

pca = PCA(n_components=2)

principalComponents = pca.fit_transform(x)
principalDf = [Link](data = principalComponents
, columns = ['pc1', 'pc2'])

In [4]:
## Concatenate principle 1 and principe 2 with Class
finalDf = [Link]([principalDf, df[['Class']]], axis = 1)
finalDf
colors = {"Breast": '#4287f5',
"Bladder": '#19c5e3',
"Colon": '#80d941',
"Glioblastoma": '#179933',
"Head&Neck": '#f07e78',
"Kidney": '#f01e13',
"Leukemia": '#f0841f',
"LungAdeno": '#db5209',
"LungSquamous": '#ce8ced',
"Ovarian": '#551075',
"Rectal": '#e3d329',
"Uterine": '#cc3423'}

In [5]:
fig = [Link](figsize = (12,12))
ax = fig.add_subplot(1,1,1)
ax.set_title('PCA of 12 cancer types', fontsize = 20)
labels = ['Breast','Bladder','Colon','Glioblastoma','Head&Neck',
'Kidney','Leukemia','LungAdeno','LungSquamous',
'Ovarian','Rectal','Urines']
componentColors = ['#4287f5','#19c5e3','#80d941','#179933','#f07e78',
'#f01e13','#f0841f','#db5209','#ce8ced','#551075',
'#e3d329','#cc3423']
for target, color in zip (labels,componentColors):
indicesToKeep = finalDf['Class'] == target
[Link]([Link][indicesToKeep, 'pc1']
, [Link][indicesToKeep, 'pc2']
, c = color
, s = 50)
[Link](labels)
[Link]()

about:srcdoc Page 3 of 7
VL_Assignment4 3/14/22, 6:37 PM

Normalize the data using StandardScaler


In [6]:
scaler = StandardScaler()
X_rescaled = scaler.fit_transform(x)

In [7]:
Y = finalDf['Class']
X_train, X_test, Y_train, Y_test = train_test_split(X_rescaled, Y, test_size=0.2
X_train.shape, Y_train.shape, X_test.shape, Y_test.shape

((3468, 3000), (3468,), (868, 3000), (868,))


Out[7]:

about:srcdoc Page 4 of 7
VL_Assignment4 3/14/22, 6:37 PM

Build SVM Model


In [8]:
model = SVC(kernel='linear', C=1, random_state=42)
[Link](X_train,Y_train)
print('Accuracy of linear SVC on training set: {:.2f}'.format([Link](X_train
print('Accuracy of linear SVC on test set: {:.2f}'.format([Link](X_test,

accuracies = cross_val_score(model, X_train, Y_train, cv=5)

print("Individual cross-validation accuracies: ",accuracies)


print("Mean cross validation accuracy: " + str([Link]()))

Accuracy of linear SVC on training set: 1.00


Accuracy of linear SVC on test set: 0.97
Individual cross-validation accuracies: [0.96541787 0.96541787 0.9610951 0.
96825397 0.96969697]
Mean cross validation accuracy: 0.9659763547371616

Plot two confusion matrices for test set predictions


In [9]:
# this code is adopted from this example:
# [Link]

np.set_printoptions(precision=2)
titles_options = [("Confusion matrix, without normalization", None),
("Normalized confusion matrix", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(model, X_test, Y_test,
display_labels= [Link](Y),
cmap=[Link],
normalize=normalize,
xticks_rotation='vertical')
disp.ax_.set_title(title)

print(title)
print(disp.confusion_matrix)

[Link]()

Confusion matrix, without normalization


[[ 66 0 0 0 0 0 0 0 0 0 0 0]
[ 0 175 0 0 0 0 0 0 0 0 0 0]
[ 0 0 66 0 0 0 0 0 0 0 8 0]
[ 0 2 0 21 0 0 0 0 0 0 0 0]
[ 0 1 0 0 81 0 0 0 1 0 0 0]
[ 0 0 0 0 0 84 0 0 0 0 0 0]
[ 0 0 0 0 0 0 20 0 0 0 0 0]
[ 0 0 0 0 0 0 0 84 1 0 0 0]
[ 0 0 0 0 0 0 0 2 76 0 0 0]

about:srcdoc Page 5 of 7
VL_Assignment4 3/14/22, 6:37 PM

[ 0 0 0 0 0 0 0 0 0 68 0 0]
[ 0 0 11 0 0 0 0 0 0 0 15 0]
[ 0 0 0 0 0 0 0 0 0 0 0 86]]
Normalized confusion matrix
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0.89 0. 0. 0. 0. 0. 0. 0. 0.11 0. ]
[0. 0.09 0. 0.91 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0.01 0. 0. 0.98 0. 0. 0. 0.01 0. 0. 0. ]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0.99 0.01 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0.03 0.97 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. ]
[0. 0. 0.42 0. 0. 0. 0. 0. 0. 0. 0.58 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. ]]

about:srcdoc Page 6 of 7
VL_Assignment4 3/14/22, 6:37 PM

about:srcdoc Page 7 of 7

You might also like