Advertisement
Ritam_C

ds3l4q3

Sep 30th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. '''Name: Ritam Chakraborty
  2.   Roll: B20127
  3.   Phone: 7439257709'''
  4.  
  5. # import libraries
  6. import pandas as pd
  7. import numpy as np
  8. from numpy.linalg import det, inv
  9. # for computing confusion matrix and accuracy score
  10. from sklearn.metrics import confusion_matrix, accuracy_score
  11. # for making comparator to sort the distances and corresponding classes based on distances
  12.  
  13. def prob(x, cov_mat, mu_mat, Pci):
  14.     diff = x-mu_mat
  15.     diff_T = diff.T
  16.     inv_cov_mat = inv(cov_mat)
  17.     dotprod = np.dot(diff.T, inv_cov_mat)
  18.     prod = np.dot(dotprod, diff)
  19.     return Pci*np.exp(-0.5*prod)/(((2*np.pi)**(12.5))*np.sqrt(det(cov_mat)))
  20.  
  21. df_train = pd.read_csv("SteelPlateFaults-train.csv")
  22. df_test = pd.read_csv("SteelPlateFaults-test.csv")
  23. # remove the following attributes since they make the covariance matrix singular
  24. df_train.__delitem__("TypeOfSteel_A300")
  25. df_train.__delitem__("TypeOfSteel_A400")
  26. df_test.__delitem__("TypeOfSteel_A300")
  27. df_test.__delitem__("TypeOfSteel_A400")
  28.  
  29. # separate the classes from the training dataset
  30. df_class1 = df_train[df_train["Class"] == 1].copy()
  31. df_class1.__delitem__("Class")
  32. df_class0 = df_train[df_train["Class"] == 0].copy()
  33. df_class0.__delitem__("Class")
  34.  
  35. # compute mean vectors from both classes
  36. mu1 = df_class1.mean().to_numpy()
  37. mu0 = df_class0.mean().to_numpy()
  38.  
  39. # predict based on bayes classifier
  40. pred = []
  41. for ind in df_test.index:
  42.     p1 = prob(df_test.iloc[ind, :25].to_numpy(), df_class1.cov(), mu1, 509/(509+273))
  43.     p0 = prob(df_test.iloc[ind, :25].to_numpy(), df_class0.cov(), mu0, 273/(509+273))
  44.     if p1 > p0:
  45.         pred.append(1)
  46.     else:
  47.         pred.append(0)
  48.  
  49. #print the confusion matrix and accuracy scores
  50. print("Confusion matrix:")
  51. print(confusion_matrix(df_test["Class"], pred))
  52. print("Accuracy score:")
  53. print(accuracy_score(df_test["Class"], pred))
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement