{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1766899",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1599, 12)\n",
      "(4898, 12)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# two-class classification\n",
    "data_red = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv', dtype = None, delimiter=';')\n",
    "data_white = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv', dtype = None, delimiter=';')\n",
    "\n",
    "print(data_red.shape)\n",
    "print(data_white.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "560b799f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X0 = data_red.to_numpy()\n",
    "X1 = data_white.to_numpy()\n",
    "\n",
    "N0,D = X0.shape\n",
    "N1,D = X1.shape\n",
    "\n",
    "# target value\n",
    "t0 = np.zeros(N0, dtype = int)\n",
    "t1 = np.ones(N1, dtype = int)\n",
    "\n",
    "# 전체 dataset\n",
    "X = np.vstack((X0, X1)) # input, 12차원의 입력\n",
    "t = np.hstack((t0, t1)) # output, {0, 1} 구분, two-class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3aac30bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn.preprocessing\n",
    "X = sklearn.preprocessing.normalize(X)\n",
    "N,D = X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2c13a6d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1072676 characters]\n",
      "text/plain": [
       "<Figure size 1500x1500 with 144 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "cmap_sample = matplotlib.colors.ListedColormap(['black', 'red'])\n",
    "cmap_region = matplotlib.colors.ListedColormap(['gray', 'white'])\n",
    "\n",
    "plt.figure(figsize = (15,15))\n",
    "for i in range(0,D):\n",
    "    for j in range(0,D):\n",
    "        plt.subplot(D,D,i*D + j + 1)\n",
    "        plt.scatter(X[:,i],X[:,j], c = t, s = 1, cmap = cmap_sample, vmin = 0, vmax = +1)\n",
    "        \n",
    "        ax = plt.gca()\n",
    "        ax.axes.xaxis.set_visible(False)\n",
    "        ax.axes.yaxis.set_visible(False)\n",
    "\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60693f7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9368866653838753\n",
      "0.9315384615384615\n",
      "0.9263036367134886\n",
      "0.9161538461538462\n",
      "0.9295747546661536\n",
      "0.9223076923076923\n"
     ]
    }
   ],
   "source": [
    "from sklearn import svm\n",
    "from sklearn.linear_model import Perceptron\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "model1 = svm.SVC(kernel = 'rbf')\n",
    "model2 = LogisticRegression()\n",
    "model3 = Perceptron()\n",
    "\n",
    "X_train, X_test, t_train, t_test = train_test_split(X, t, test_size = 0.20)\n",
    "\n",
    "N_train,D_train = X_train.shape\n",
    "N_test,D_test = X_test.shape\n",
    "\n",
    "for model in [model1, model2, model3]:\n",
    "    model.fit(X_train,t_train)    \n",
    "\n",
    "    pred_train = model.predict(X_train)\n",
    "    pred_test = model.predict(X_test)\n",
    "\n",
    "    print(sklearn.metrics.accuracy_score(t_train, pred_train))\n",
    "    print(sklearn.metrics.accuracy_score(t_test, pred_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "466ceb5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[484 484]\n",
      "(968,)\n",
      "(968, 12)\n"
     ]
    }
   ],
   "source": [
    "print(model1.n_support_)\n",
    "print(model1.support_.shape)\n",
    "print(model1.support_vectors_.shape)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
