#!/usr/bin/env python
# coding: utf-8

# In[1]:


#svm
from tensorflow.keras import datasets
from sklearn.model_selection import train_test_split
import numpy as np
import cv2


# In[2]:


TRAIN_SIZE = 5000
TEST_SIZE = 1000

(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = datasets.mnist.load_data()

x_train_sub, _, y_train_sub, _ = train_test_split(
    x_train_raw, y_train_raw,
    train_size=TRAIN_SIZE,
    stratify=y_train_raw,
    random_state=42
)

x_test_sub, _, y_test_sub, _ = train_test_split(
    x_test_raw, y_test_raw,
    train_size=TEST_SIZE,
    stratify=y_test_raw,
    random_state=42
)

x_train_eq = np.array([cv2.equalizeHist(img) for img in x_train_sub])
x_test_eq = np.array([cv2.equalizeHist(img) for img in x_test_sub])


x_train_flat = x_train_eq.reshape(-1, 28 * 28) / 255.0
x_test_flat = x_test_eq.reshape(-1, 28 * 28) / 255.0


# In[3]:


from sklearn.model_selection import train_test_split, GridSearchCV, cross_validate
from sklearn.svm import SVC

svc = SVC(kernel="linear")
grid_search = GridSearchCV(svc, {"C": [0.1, 1, 10, 100]}, cv=5, scoring="accuracy", n_jobs=-1)
grid_search.fit(x_train_flat, y_train_sub)


# In[4]:


import pandas as pd

best_estimators = {}

best_model = grid_search.best_estimator_
best_params = grid_search.best_params_
best_score = grid_search.best_score_

best_estimators["Linear"] = best_model

table1_data = []
table1_data.append({
    "Kernel" : "Linear",
    "Best C": best_params.get("C", "N/A"),
    "Best Gamma": best_params.get("gamma", "N/A"),
    "Best Degree": best_params.get("degree", "N/A"),
    "Best Acc": best_score
})

df_table1 = pd.DataFrame(table1_data)
print(df_table1.to_string(index=False))


# In[5]:


table2_data = []

cv_res = cross_validate(best_model, x_train_flat, y_train_sub, 
            cv=5, scoring=["accuracy", "recall_weighted", "f1_weighted"])


# In[6]:


cv_res

