Jumat, 31 Mei 2019

SVM Python

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
%matplotlib inline


def display(img):
    cv2.imshow("show",img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    b=edges.shape[0]
    c=edges.shape[1]

    #for i in range(b):
    #    for j in range(c):
    #        D=edges[i][j]
    #        #print(str(i)+","+str(j)+"="+str(D))
    return img 

def myresize(img,b,c):
    dim = (b, c)
    resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    return resized

def resshape(img):
    width = int(img.shape[0])
    height = int(img.shape[1])
    wh=width * height
    dim = (wh, 1)
    resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    return resized

def uk(img):
    width = int(img.shape[0])
    height = int(img.shape[1])
    print (width ,' x ' , height)
    return (width ,' x ' , height)

def uk2(img):
    width =len(img[0])
    height = len(img[1])
    print (width ,' x ' , height)
    return (width ,' x ' , height)

def uk3(img):
    s =img.shape()
    return s

def model(img):
    lap=type(img)
    print(lap)
    return lap




bankdata = pd.read_csv("data/wavelet.csv")
bankdata.head()   #<class 'pandas.core.frame.DataFrame'>


train=bankdata
model(train)
#X = bankdata.drop('Class', axis=1) 
#y = bankdata['Class']

X = train.iloc[:, :-1].values.astype('int32') #:[:, :-1], [:, 4]=just c4,[:, 1:]c1 dst=1371  x  4
y = train.iloc[:, 4].values.astype('int32')

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30) 


from sklearn.svm import SVC 
svclassifier = SVC(kernel='linear') 
svclassifier.fit(X_train, y_train) 




print(X_test)
y_pred = svclassifier.predict(X_test)  

print(y_test)
print(y_pred)

correct = 0
total = len(y_test)
for i in range(total):
    predicted = y_pred[i] #np.argmax(y_pred[i])
    test = y_test[i] #np.argmax(y_test[i])
    #print(predicted ,'=',test)
    correct = correct + (1 if predicted == test else 0)
print('Accuracy: {:f}'.format(correct/total))


hasil:
[[-3 -8 10  0]
 [ 1  6  0  0]
 [-1  9  1 -5]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 1 1 1 1
 1 0 1 0 1 0 1 1 1 1 1 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 1 1 1
 1 0 1 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 0 0 1 0
 0 1 0 0 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 1 1 0 1 1 0 1 1 0 0 0 0 1
 0 0 0 0 1 0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 0 1 1 0
 1 0 0 1 1 1 0 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 1 0 1
 1 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 1 1
 1 0 0 0 1 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 1 1 1
 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 1 1 0 0 0 0 1 1 0 0 1 0 1 1 0
 0 1 1 1 1 0 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0
 1 1 0 1 0]
Accuracy: 0.975728


from sklearn.metrics import classification_report, confusion_matrix  
print(confusion_matrix(y_test,y_pred))  
print(classification_report(y_test,y_pred))  

hasil
[[210   9]
 [  1 192]]
              precision    recall  f1-score   support

           0       1.00      0.96      0.98       219
           1       0.96      0.99      0.97       193

    accuracy                           0.98       412
   macro avg       0.98      0.98      0.98       412
weighted avg       0.98      0.98      0.98       412


#pengujian
dataUji=[3.6216,8.6661,-2.8073,-0.44699]
uji=list()
uji.append(dataUji)

y_pred = svclassifier.predict(uji)  
print("Hasil Pengujian ")
print(y_pred)

Hasil Pengujian 
[0]
pengujian uji=list() uji.append(X_test[0]) uji.append(X_test[1]) uji.append(X_test[2]) uji.append(X_test[3]) uji.append(X_test[4]) y_pred = svclassifier.predict(uji) print("Hasil Pengujian ") print(y_pred)
Hasil Pengujian 
[1 0 0 1 1]







Tidak ada komentar:

Posting Komentar