Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #Kevin Tran
  2. #3-21-18
  3. #Problem 1 sklearn svm and class
  4.  
  5. import numpy as np
  6. from sklearn import preprocessing, cross_validation, neighbors, svm
  7. import pandas as pd
  8. import random
  9. from svm import Support_Vector_Machine
  10. from sklearn.metrics import classification_report
  11. from sklearn.metrics import confusion_matrix
  12.  
  13.  
  14. df = pd.read_csv('cancer.txt')
  15. df.replace('?', -99999, inplace=True)
  16. df.drop(df.columns[0], 1, inplace=True)
  17.  
  18. df.drop(df.columns[[1,2,4,5,6,7,8]], axis=1, inplace=True)
  19. X = np.array(df.drop([df.columns[2]], 1))
  20. y = np.array(df[df.columns[2]])
  21. X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.4)
  22.  
  23. clf = svm.SVC()
  24. clf.fit(X_train, y_train)
  25. confidence = clf.score(X_test, y_test)
  26.  
  27. print("Predicted Table of X_Test:")
  28. print(clf.predict(X_test), "\n")
  29. print("SKLEARN confidence score: ", confidence, "\n")
  30. print("Begin in class SVM\n")
  31.  
  32. pred = clf.predict(X_test)
  33. print(confusion_matrix(y_test, pred))
  34. print(classification_report(y_test, pred))
  35.  
  36. #
  37. # Email said to use example as the test
  38. #
  39.  
  40. df = pd.read_csv("cancer.txt")
  41. df.replace('?',-99999, inplace=True)
  42. df.drop(df.columns[0], 1, inplace=True)
  43.  
  44. full_data = df.astype(float).values.tolist()
  45. random.shuffle(full_data)
  46. #test_data = df['ct','bn']
  47.  
  48. test_size = 0.4
  49. train_set = {2:[], 4:[]}
  50. test_set = {2:[], 4:[]}
  51. train_data = full_data[:-int(test_size*len(full_data))]
  52. test_data = full_data[-int(test_size*len(full_data)):]
  53.  
  54. data_dict = train_set
  55. for i in train_data:
  56. train_set[i[-1]].append(i[:-1])
  57.  
  58. for i in test_data:
  59. test_set[i[-1]].append(i[:-1])
  60.  
  61. svm = Support_Vector_Machine()
  62. svm.fit(data=data_dict)
  63.  
  64. predict_us = [[0,10],
  65. [1,3],
  66. [3,4],
  67. [3,5],
  68. [5,5],
  69. [5,6],
  70. [6,-5],
  71. [5,8]]
  72.  
  73. for p in test_set:
  74. svm.predict(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement