-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm2.py
56 lines (40 loc) · 1.68 KB
/
svm2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
import matplotlib.pyplot as plt
df = pd.read_csv("cell_samples.csv")
print(df.columns)
cell = df[df["Class"]==4][0:50].plot(kind='scatter',x='Clump',y='UnifSize',color="DarkBlue", label="Malignant");
df[df['Class']==2][0:50].plot(kind="scatter", x="Clump",y="UnifSize", color="Yellow", label= "Benign", ax=cell);
# necesarry to have all coloumns with integar datatypes
"""
checking if all are integars or not, if not then
converting them to integar
"""
# print(df.dtypes)
''' BareNuc is not integar, so lets make it
'''
df = df[pd.to_numeric(df["BareNuc"], errors="coerce").notnull()]
df["BareNuc"] = df["BareNuc"].astype(int)
#print(df.dtypes)
x = np.asanyarray(df[['Clump', 'UnifSize', 'UnifShape', 'MargAdh', 'SingEpiSize','BareNuc', 'BlandChrom', 'NormNucl', 'Mit']])
#print(x[0:5])
y = np.asanyarray(df["Class"].astype(int))
#print(y[0:5])
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=3)
#print ('Train set:', x_train.shape, y_train.shape)
#print ('Test set:', x_test.shape, y_test.shape)
from sklearn import svm
clf = svm.SVC(kernel="rbf")
clf.fit(x_train, y_train)
y_hat = clf.predict(x_test)
print(y_hat[137])
from sklearn.metrics import f1_score,jaccard_score,classification_report
print("F1 score is:", f1_score(y_test, y_hat, average="weighted"))
print("Accuracy is:", jaccard_score(y_test, y_hat, pos_label=2))
print(classification_report(y_test, y_hat))
#plt.show()