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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/dibyanshushekhardey/Hello-ML/blob/main/2.%20Classification/Supervised%20I/Support%20Vector%20Machines/SVM_NuSVC_and_LinearSVC.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="NDJBMsRbW2vk"
from sklearn.datasets import load_iris
iris = load_iris()

# + id="KxZCzvfck29h"
# iris.data
X = iris.data
y = iris.target

# + id="VCmd3ASylFFs"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

# + [markdown] id="6buXxpYLrhcJ"
# **Nu-Support Vector Classification**

# + colab={"base_uri": "https://localhost:8080/"} id="S2_937tzlHdl" outputId="6dc7d999-deff-447c-b451-a934d3cb389a"
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import NuSVC
clf = make_pipeline(StandardScaler(), NuSVC())
clf.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="Ak9OLnFulctY" outputId="11d9e551-dc38-4f30-b586-1258b6ec8019"
#Prediction
y_pred = clf.predict(X_test)
print(y_pred)

# + colab={"base_uri": "https://localhost:8080/"} id="AXNTbeCHlfL2" outputId="79f091d3-ebfa-406f-d418-ee4f1c614290"
#Check precision
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
print(classification_report(y_test, y_pred))
print("----------------")
print(accuracy_score(y_test, y_pred))

# + colab={"base_uri": "https://localhost:8080/"} id="szYuhPKqliAh" outputId="ac899566-1dfb-43e7-984f-ec9e591dddc1"
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm

# + colab={"base_uri": "https://localhost:8080/", "height": 279} id="6QITKxE6lmd0" outputId="65b1fdfa-4061-4644-af59-b50655e2a22a"
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt

df_cm = pd.DataFrame(cm, range(3), range(3))
# plt.figure(figsize=(10,7))
sn.set(font_scale=1.4) # for label size
sn.heatmap(df_cm, annot=True, annot_kws={"size": 14}) 

plt.show()

# + [markdown] id="stENWeY7tD5P"
# **Linear Support Vector Classification**

# + colab={"base_uri": "https://localhost:8080/"} id="-5NXYZC9lpoI" outputId="f9934e28-90fe-41aa-d499-362ea3aeb26f"
from sklearn.svm import LinearSVC
clf1 = make_pipeline(StandardScaler(),
                    LinearSVC(random_state=0, tol=1e-5))
clf1.fit(X_train, y_train)


# + colab={"base_uri": "https://localhost:8080/"} id="rP62j3ZrmcIa" outputId="2db0321f-ad17-451b-82a2-eced52ca54e1"
y_pred1 = clf1.predict(X_test)
print(y_pred1)

# + colab={"base_uri": "https://localhost:8080/"} id="1IY9D2Ccmg5o" outputId="6242569d-58b6-4b81-8140-9662e6ebcdc1"
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
print(classification_report(y_test, y_pred1))
print("----------------")
print(accuracy_score(y_test, y_pred1))

# + colab={"base_uri": "https://localhost:8080/"} id="iQtATcBFm3vu" outputId="1b487a83-0cba-4773-f2a8-b58b5780501e"
from sklearn.metrics import confusion_matrix
cm1 = confusion_matrix(y_test, y_pred1)
cm1

# + colab={"base_uri": "https://localhost:8080/", "height": 279} id="3DIw84Tjm8b4" outputId="873976c2-46ad-488c-88f6-c0123a87f6a4"
df_cm1 = pd.DataFrame(cm1, range(3), range(3)) # Range fitted based on the previous array
# plt.figure(figsize=(10,7))
sn.set(font_scale=1.4) # for label size
sn.heatmap(df_cm1, annot=True, annot_kws={"size": 12}) # font size , size range based on theprevious plot matrix

plt.show()

# + id="K4UyfjMZnBkC"


StarcoderdataJupyterScriptsDedupFiltered