{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn import preprocessing\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.datasets import make_classification\n", "import matplotlib.pyplot as plt\n", "from matplotlib.pylab import rcParams\n", "rcParams['figure.figsize'] = 8, 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 製作兩個feature的分類資料\n", "X, y = make_classification(n_samples=300,\n", " n_features=2,\n", " n_redundant=0,\n", " n_informative=2,\n", " random_state=22,\n", " n_clusters_per_class=1,\n", " scale=100)\n", "# 將資料可視化\n", "plt.scatter(X[:, 0], X[:, 1], c=y)\n", "plt.xlabel('X1')\n", "plt.ylabel('X2')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prediction before normalization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.svm import SVC\n", "\n", "# 將資料分成訓練及測試集\n", "X_train, X_test, y_train, y_test = train_test_split(X,\n", " y,\n", " test_size=0.3,\n", " random_state=42)\n", "# import svm classifier\n", "model = SVC()\n", "model.fit(X_train, y_train)\n", "prediction = model.predict(X)\n", "print('accuracy before normalization:%.2f' % model.score(X_test, y_test))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 可視化結果" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 畫出原始資料\n", "plt.subplot(121)\n", "plt.scatter(X[:, 0], X[:, 1], c=y)\n", "plt.title('Actual')\n", "plt.xlabel('X1')\n", "plt.ylabel('X2')\n", "# 畫出預測結果\n", "plt.subplot(122)\n", "plt.scatter(X[:, 0], X[:, 1], c=prediction)\n", "plt.title('Prediction')\n", "plt.xlabel('X1')\n", "plt.ylabel('X2')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Predictino after normalization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.svm import SVC\n", "\n", "# 將資料做z-score normalization\n", "X = preprocessing.scale(X)\n", "# 將資料分成訓練及測試集\n", "X_train, X_test, y_train, y_test = train_test_split(X,\n", " y,\n", " test_size=0.3,\n", " random_state=42)\n", "# import svm classifier\n", "model = SVC()\n", "model.fit(X_train, y_train)\n", "prediction = model.predict(X)\n", "print('accuracy after normalization:%.2f' % model.score(X_test, y_test))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 將結果可視化" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 畫出原始資料\n", "plt.subplot(121)\n", "plt.scatter(X[:, 0], X[:, 1], c=y)\n", "plt.title('Actual')\n", "plt.xlabel('X1')\n", "plt.ylabel('X2')\n", "# 畫出預測結果\n", "plt.subplot(122)\n", "plt.scatter(X[:, 0], X[:, 1], c=prediction)\n", "plt.title('Prediction')\n", "plt.xlabel('X1')\n", "plt.ylabel('X2')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }