# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## 라이브러리 import

import pandas as pd
import numpy as np
import pickle
from sklearn import svm,metrics
import joblib

# ## 데이터 불러오기

# +
Fold = 5

# k-fold 학습/검증 데이터
for i in range(Fold):
    
    path1 = './K_FoldData/Training_Fold%d'%(i+1)
    path2 = './K_FoldData/Validation_Fold%d'%(i+1)
    c1 = 'Training_Fold%d   = np.array(pd.read_csv(path1, sep=",", header=None))'%(i+1)
    c2 = 'Validation_Fold%d = np.array(pd.read_csv(path2, sep=",", header=None))'%(i+1)
    exec(c1)
    exec(c2)

# K-fold 학습/검증 레이블
TrainingFold_Label   = np.array(pd.read_csv('./K_FoldData/TrainingFold_Label'  , sep=",", header=None).T.squeeze())
ValidationFold_Label = np.array(pd.read_csv('./K_FoldData/ValidationFold_Label', sep=",", header=None).T.squeeze())
    
    
# 전체 학습용 데이터
Training_All       = np.array(pd.read_csv('./K_FoldData/Training_All', sep = ",", header = None))
Training_All_Label = np.array(pd.read_csv('./K_FoldData/Training_All_Label', sep = ",", header = None).T.squeeze())

print(Training_Fold1.shape)
print(Validation_Fold1.shape)
print(TrainingFold_Label.shape)
print(ValidationFold_Label.shape)
print(Training_All.shape)
# -

# ## SVM(Support Vector Machine) 기반 모델링 및 성능평가

# +
Add    = 0
Divide = 0

for i in range(Fold):
    c1 = 'Training_CurrentFold = Training_Fold%d'%(i+1)
    exec(c1)
    c2 = 'Validation_CurrentFold = Validation_Fold%d'%(i+1)
    exec(c2)    
    
    svmModel_CurrentFold = svm.SVC(kernel='linear')
    svmModel_CurrentFold.fit(Training_CurrentFold , TrainingFold_Label)
    Predicted = np.array(svmModel_CurrentFold.predict(Validation_CurrentFold))
    
    c3 = 'svmscore_Fold%d = metrics.accuracy_score(ValidationFold_Label , Predicted)'%(i+1)
    exec(c3)
        
    Add += metrics.accuracy_score(ValidationFold_Label , Predicted)
    Divide += 1
    
Avg_accuracy = Add/Divide

print('[Result of K-fold Cross Validation] \n')
print(' Fold 1: {:.2f}% \n Fold 2: {:.2f}% \n Fold 3: {:.2f}% \n Fold 4: {:.2f}% \n Fold 5: {:.2f}%'.
        format(svmscore_Fold1*100, svmscore_Fold2*100, svmscore_Fold3*100, svmscore_Fold4*100, svmscore_Fold5*100))
print('\n Average accuracy: {:.2f}%'.format(Avg_accuracy*100))
# -

# ## SVM model 모델 생성(전체 데이터) 및 저장

# +
SVM_model = svm.SVC(kernel='linear')
SVM_model.fit(Training_All, Training_All_Label)

joblib.dump(SVM_model, './MLmodels/SVM_model.plk')
# -

# ## SVM model 불러와서 진단

SVM_model_Final = joblib.load('./MLmodels/SVM_model.plk')


# +
Add    = 0
Divide = 0

for i in range(Fold):
    c1 = 'Validation_CurrentFold = Validation_Fold%d'%(i+1)
    exec(c1)    
    
    Predicted = np.array(SVM_model_Final.predict(Validation_CurrentFold))
    
    c2 = 'svmscore_Fold%d = metrics.accuracy_score(ValidationFold_Label , Predicted)'%(i+1)
    exec(c2)
        
    Add += metrics.accuracy_score(ValidationFold_Label , Predicted)
    Divide += 1
    
Avg_accuracy = Add/Divide

print('[Performance of SVM model] \n')
print(' Fold 1: {:.2f}% \n Fold 2: {:.2f}% \n Fold 3: {:.2f}% \n Fold 4: {:.2f}% \n Fold 5: {:.2f}%'.
        format(svmscore_Fold1*100, svmscore_Fold2*100, svmscore_Fold3*100, svmscore_Fold4*100, svmscore_Fold5*100))
print('\n Average accuracy: {:.2f}%'.format(Avg_accuracy*100))

# 강의 영상에서의 정확도와는 다를 수 있습니다.
# -



StarcoderdataJupyterScriptsDedupFiltered