Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Apr 5 10:50:47 2019
  4.  
  5. @author: mattdoe
  6. """
  7.  
  8. from data_preprocessor_db import data_storage # validation data
  9. from sklearn.preprocessing import MinMaxScaler
  10. from sklearn import svm
  11. from sklearn.model_selection import train_test_split
  12. from sklearn.metrics import confusion_matrix
  13. from numpy import array
  14. import pickle
  15.  
  16.  
  17.  
  18. # for seperation of data_storage
  19. # Link_ID = list()
  20. Input, Output = list(), list()
  21.  
  22. # seperate data_storage in Input and Output data
  23. for items in data_storage:
  24. # Link_ID = items[0] # identifier not needed
  25. Input.append((float(items[1]), float(items[2]), float(items[3]), float(items[4]), float(items[5]), float(items[6]), float(items[7]), float(items[8]), float(items[9]))) # Input: all characteristics
  26. Output.append(float(items[10])) # Output: scenario_class 1 to 9
  27.  
  28. # Input tuple to array
  29. A = array(Input)
  30.  
  31. # normalise array between 0 and 1
  32. scaler = MinMaxScaler(feature_range=(-1, 1))
  33. scaledA = scaler.fit_transform(A)
  34.  
  35. # Output tuple to array
  36. B = array(Output)
  37.  
  38. # split train and test data; ration: 70:30
  39. # shuffle = False: doesn't sort data randomly
  40. # shuffle = True: default: sorts data randomly
  41. A_train, A_test, B_train, B_test = train_test_split(A, B, test_size=0.3, shuffle=True, random_state=40)
  42.  
  43. # create model
  44. model = svm.SVC(kernel='linear', C = 1.0)
  45.  
  46. # fit model
  47. model.fit(A_train, B_train)
  48.  
  49. # get support vectors
  50. # model.support_vectors_
  51.  
  52. # get indices of support vectors
  53. # model.support_
  54.  
  55. # get number of support vectors for each class
  56. # model.n_support_
  57.  
  58. filename = 'ml_svm.sav'
  59. pickle.dump(model, open(filename, 'wb'))
  60.  
  61. # load the model from disk
  62. loaded_model = pickle.load(open(filename, 'rb'))
  63.  
  64. # test to all data records
  65. # result = loaded_model.score(A, B)
  66.  
  67. # test with test data
  68. # score represents the mean accuracy of given test data and labels
  69. result = loaded_model.score(A_test, B_test) # relative
  70. print(result)
  71.  
  72. # confusion matrix compares true value with predicted value
  73. # true value <--> predicted value
  74. predicted = model.predict(A_test)
  75. tn, fp, fn, tp = confusion_matrix(B_test, predicted, labels=[1, 2, 3, 4, 5, 6, 7, 8, 9]).ravel()
  76.  
  77. Traceback (most recent call last):
  78.  
  79. File "<ipython-input-8-8649dd873bbd>", line 1, in <module>
  80. runfile('C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_svm.py', wdir='C:/Workspace/Master-Thesis/Programm/MapValidationML')
  81.  
  82. File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 827, in runfile
  83. execfile(filename, namespace)
  84.  
  85. File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 110, in execfile
  86. exec(compile(f.read(), filename, 'exec'), namespace)
  87.  
  88. File "C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_svm.py", line 75, in <module>
  89. tn, fp, fn, tp = confusion_matrix(B_test, predicted, labels=[1, 2, 3, 4, 5, 6, 7, 8, 9]).ravel()
  90.  
  91. ValueError: too many values to unpack (expected 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement