Skip to content

Instantly share code, notes, and snippets.

@balamurali-m
Last active August 10, 2018 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balamurali-m/3fd696b1a3e53341a6442cb020c79de6 to your computer and use it in GitHub Desktop.
Save balamurali-m/3fd696b1a3e53341a6442cb020c79de6 to your computer and use it in GitHub Desktop.
Classifications Metrics
"""
Classification Metrics
Author: Balamurali M
"""
import numpy as np
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.metrics import cohen_kappa_score, classification_report
from sklearn import svm
import warnings
warnings.filterwarnings('ignore')
#Generating matrix with random explanatory and response variables
matr = np.random.randint(2, size=(100, 20))
print (matr.shape)
train_exp = matr[:80, :19]
train_res = matr[:80, 19:]
test_exp = matr[80:, :19]
test_act = matr[80:, 19:]
class SVM1:
def __init__(self, w1, x1, y1, z1):
self.w1 = w1
self.x1 = x1
self.y1 = y1
self.z1 = z1
def SVM_fit(self):
a1 = svm.SVC()
return a1.fit(self.w1, self.x1)
matr_exp = SVM1(train_exp, train_res, test_exp, test_act)
fit1 = matr_exp.SVM_fit()
predicted1 = fit1.predict(test_exp)
print ('Actual class')
print (test_act)
print ('Predicted class')
print (predicted1)
conf_1 = confusion_matrix(test_act, predicted1) #confusion Matrix
print (conf_1)
tneg, fpos, fneg, tpos = confusion_matrix(test_act, predicted1).ravel()
print(tneg, fpos, fneg, tpos) #true negative, false positive, false negative, true positive
acc_1 = accuracy_score(test_act, predicted1)
print (acc_1) #accuracy score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment