# ---
# 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
# ---

# # Grid Search

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

# ## Data Loading

PATH = "../../../Classification/Kernel_SVM/Python/Social_Network_Ads.csv"

dataset = pd.read_csv(PATH)

X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# ## Train Test Split

# +
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.25, 
                                                    random_state=42)
# -

# ## Feature Scaling

# +
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# -

# ## Kernel SVM

# +
from sklearn.svm import SVC

classifier = SVC(kernel="rbf", random_state=42)
classifier.fit(X_train, y_train)
# -

# ## Prediction

sample = [[30, 87000]]
print(classifier.predict(sc.transform(sample)))

# + jupyter={"outputs_hidden": true}
y_pred = classifier.predict(X_test)
print(np.concatenate((y_test.reshape(len(y_test),1), y_pred.reshape(len(y_pred),1)), axis=1))
# -

# ## K-Fold Cross Validation

# +
from sklearn.model_selection import cross_val_score

accuracies = cross_val_score(estimator=classifier, X=X_train, 
                             y=y_train, cv=10, n_jobs=-1)
# -

print("Mean \t\t\t%.2f" % accuracies.mean())
print("Standard Deviation \t%.2f" % accuracies.std())

# ## Grid Search

# +
from sklearn.model_selection import GridSearchCV

parameters = [
    {"C": [1, 10, 100, 1000], "kernel": ['linear']},
    {"C": [1, 10, 100, 1000], "kernel": ['rbf'], "gamma": [0.5, 0.1, 0.01, 0.001, 0.0001]},
    {"C": [1], "kernel": ['rbf'], "gamma": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]}
]

gs = GridSearchCV(estimator=classifier, param_grid=parameters, 
                  scoring='accuracy', cv=10, n_jobs=-1)
gs = gs.fit(X_train, y_train)

# +
best_accuracy = gs.best_score_
best_parameters = gs.best_params_

print("Best Accuracy \t\t\t%.2f" % best_accuracy)
print("Best Parameters \t", best_parameters)

StarcoderdataJupyterScriptsDedupFiltered