Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Apr 5 10:50:47 2019
- @author: mattdoe
- """
- from data_preprocessor_db import data_storage # validation data
- from sklearn.preprocessing import MinMaxScaler
- from sklearn import svm
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import confusion_matrix
- from numpy import array
- import pickle
- # for seperation of data_storage
- # Link_ID = list()
- Input, Output = list(), list()
- # seperate data_storage in Input and Output data
- for items in data_storage:
- # Link_ID = items[0] # identifier not needed
- 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
- Output.append(float(items[10])) # Output: scenario_class 1 to 9
- # Input tuple to array
- A = array(Input)
- # normalise array between 0 and 1
- scaler = MinMaxScaler(feature_range=(-1, 1))
- scaledA = scaler.fit_transform(A)
- # Output tuple to array
- B = array(Output)
- # split train and test data; ration: 70:30
- # shuffle = False: doesn't sort data randomly
- # shuffle = True: default: sorts data randomly
- A_train, A_test, B_train, B_test = train_test_split(A, B, test_size=0.3, shuffle=True, random_state=40)
- # create model
- model = svm.SVC(kernel='linear', C = 1.0)
- # fit model
- model.fit(A_train, B_train)
- # get support vectors
- # model.support_vectors_
- # get indices of support vectors
- # model.support_
- # get number of support vectors for each class
- # model.n_support_
- filename = 'ml_svm.sav'
- pickle.dump(model, open(filename, 'wb'))
- # load the model from disk
- loaded_model = pickle.load(open(filename, 'rb'))
- # test to all data records
- # result = loaded_model.score(A, B)
- # test with test data
- # score represents the mean accuracy of given test data and labels
- result = loaded_model.score(A_test, B_test) # relative
- print(result)
- # confusion matrix compares true value with predicted value
- # true value <--> predicted value
- predicted = model.predict(A_test)
- tn, fp, fn, tp = confusion_matrix(B_test, predicted, labels=[1, 2, 3, 4, 5, 6, 7, 8, 9]).ravel()
- Traceback (most recent call last):
- File "<ipython-input-8-8649dd873bbd>", line 1, in <module>
- runfile('C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_svm.py', wdir='C:/Workspace/Master-Thesis/Programm/MapValidationML')
- File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 827, in runfile
- execfile(filename, namespace)
- File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 110, in execfile
- exec(compile(f.read(), filename, 'exec'), namespace)
- File "C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_svm.py", line 75, in <module>
- tn, fp, fn, tp = confusion_matrix(B_test, predicted, labels=[1, 2, 3, 4, 5, 6, 7, 8, 9]).ravel()
- ValueError: too many values to unpack (expected 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement