{"id":5103,"date":"2024-06-12T11:46:06","date_gmt":"2024-06-12T02:46:06","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=5103"},"modified":"2024-06-12T11:46:06","modified_gmt":"2024-06-12T02:46:06","slug":"supportvector_implementation","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/supportvector_implementation\/","title":{"rendered":"\u3010\u6a5f\u68b0\u5b66\u7fd2\u3011\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u89e3\u8aac\uff5e\u5b9f\u88c5\u7de8\u2460\uff5e"},"content":{"rendered":"\n<p>\u4eca\u56de\u306f\u6a5f\u68b0\u5b66\u7fd2\u624b\u6cd5\u306e\u4e00\u3064\u3067\u3042\u308b\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306b\u3064\u3044\u3066python\u3092\u7528\u3044\u3066\u5b9f\u88c5\u3092\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u4eca\u56de\u306f\u524d\u56de\u6271\u3063\u305f\u6a5f\u68b0\u5b66\u7fd2\u624b\u6cd5\u306e\u4e00\u3064\u3067\u3042\u308b\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3059\u3002\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u809d\u3067\u3042\u308b\u300c\u30de\u30fc\u30b8\u30f3\u306e\u6700\u5927\u5316\u300d\u306b\u3064\u3044\u3066\u610f\u8b58\u3057\u306a\u304c\u3089\u53d6\u308a\u7d44\u3093\u3067\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<p>\u4eca\u56de\u306e\u5b9f\u88c5\u306fpython\u3092\u7528\u3044\u307e\u3059\u304c\u3001googlecolaboratory\u4e0a\u3067\u884c\u3044\u307e\u3059\u3002\u4eca\u56de\u6271\u3063\u305f\u30b3\u30fc\u30c9\u3092\u305d\u306e\u307e\u307e\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u540c\u3058\u3088\u3046\u306a\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\r\n<p>\u307e\u305f\u3001\u524d\u56de\u3001\u7dda\u5f62\u306e\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u307f\u306b\u3064\u3044\u3066\u6271\u3063\u305f\u306e\u3067\u4eca\u56de\u3082\u7dda\u5f62\u306b\u7d5e\u3063\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n<h2>\u6e96\u5099<\/h2>\n<p>python\u4e0a\u3067\u5b9f\u88c5\u3092\u884c\u3046\u305f\u3081\u306b\u307e\u305a\u306f\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u308a\u3001\u7d50\u679c\u306e\u63cf\u5199\u306a\u3069\u304c\u5bb9\u6613\u306b\u51fa\u6765\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom sklearn import svm\r\nfrom sklearn.datasets import make_blobs<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\n\n<h2>\u6271\u3046\u30c7\u30fc\u30bf<\/h2>\n<p>\u4eca\u56de\u306f\u4eba\u53e3\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u4eba\u53e3\u30c7\u30fc\u30bf\u306e\u5206\u985e\u3092\u8003\u3048\u3066\u3044\u304d\u307e\u3059\u3002\u4f5c\u6210\u3057\u305f\u4eba\u53e3\u30c7\u30fc\u30bf\u306f\u6b21\u306e\u3082\u306e\u3067\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u4eba\u53e3\u30c7\u30fc\u30bf\u306e\u751f\u6210\r\n# 2\u3064\u306e\u30af\u30e9\u30b9\u306b\u5206\u3051\u3089\u308c\u305f\u30c7\u30fc\u30bf\u3092\u751f\u6210\r\nX, y = make_blobs(n_samples=100, centers=2, random_state=6)<\/code><\/pre>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-105104-300x221.png\" alt=\"\" width=\"300\" height=\"221\" class=\"alignnone size-medium wp-image-5104\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-105104-300x221.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-105104.png 608w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><br \/>\r\n<!-- notionvc: 6a1775dc-ea5f-4192-9d4c-02ffe91abc79 --><\/p>\r\n<p>\u3053\u306e\u3088\u3046\u306b\u3001\u4e8c\u6b21\u5143\u306e\u30c7\u30fc\u30bf\u3067\u3059\u3002\u3053\u306e\u6642\u3001\u6c34\u8272\u306e\u30c7\u30fc\u30bf\u3068\u8336\u8272\u306e\u30c7\u30fc\u30bf\u3092\u4e0a\u624b\u304f\u5206\u985e\u3067\u304d\u308b\u3088\u3046\u306a\u8d85\u5e73\u9762\u3092\u8003\u3048\u307e\u3059\u3002\u30d7\u30ed\u30c3\u30c8\u3059\u308b\u3068\u5206\u304b\u308b\u3088\u3046\u306b\u4eca\u56de\u306f\u7dda\u5f62\u5206\u96e2\u304c\u51fa\u6765\u305d\u3046\u3067\u3059\u3002<\/p>\r\n<\/div>\n\n<h2>\u30e2\u30c7\u30eb\u306e\u8a13\u7df4<\/h2>\n<p>\u3067\u306f\u3001\u4f5c\u6210\u3057\u305f\u4eba\u53e3\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3001\u304a\u3088\u3073\u8a13\u7df4\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u7dda\u5f62\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u30e2\u30c7\u30eb\u3092\u8a13\u7df4\r\nmodel = svm.SVC(kernel='linear')\r\nmodel.fit(X, y)<\/code><\/pre>\r\n<p>\u4e8c\u884c\u76ee\u3067\u7dda\u5f62\u306e\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u3001\u4e09\u884c\u76ee\u3067\u4eba\u53e3\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\u30e2\u30c7\u30eb\u3092\u5b66\u7fd2\u3055\u305b\u3066\u3044\u307e\u3059\u3002\u30b3\u30fc\u30c9\u4e0a\u3067\u306f\u7c21\u6f54\u306b\u8868\u73fe\u3055\u308c\u307e\u3059\u304c\u3053\u306e\u6642\u3001\u6570\u7406\u7684\u7406\u89e3\u306e\u90e8\u5206\u3067\u6271\u3063\u305f\u30de\u30fc\u30b8\u30f3\u3092\u6700\u5927\u5316\u3055\u305b\u306a\u304c\u3089\u6700\u9069\u5316\u554f\u984c\u3092\u89e3\u304f\u3068\u3044\u3063\u305f\u51e6\u7406\u304c\u306a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<\/div>\n\n<h2>\u7d50\u679c\u306e\u63cf\u5199<\/h2>\n<p>\u3067\u306f\u3001\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u306e\u7d50\u679c\u3092\u63cf\u5199\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30c7\u30fc\u30bf\u306e\u63cf\u5199\r\nplt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)\r\n# \u6c7a\u5b9a\u5883\u754c\u3092\u63cf\u304f\r\nax = plt.gca()\r\nxlim = ax.get_xlim()\r\nylim = ax.get_ylim()\r\n# \u30e1\u30c3\u30b7\u30e5\u30b0\u30ea\u30c3\u30c9\u3092\u4f5c\u6210\u3057\u3066\u6c7a\u5b9a\u5883\u754c\u3092\u63cf\u753b\r\nxx, yy = np.meshgrid(np.linspace(xlim[0], xlim[1], 30),\r\nnp.linspace(ylim[0], ylim[1], 30))\r\nZ = model.decision_function(np.c_[xx.ravel(), yy.ravel()])\r\nZ = Z.reshape(xx.shape)\r\n# \u6c7a\u5b9a\u5883\u754c\u3068\u30de\u30fc\u30b8\u30f3\u3092\u63cf\u304f\r\nax.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,\r\nlinestyles=['--', '-', '--'])\r\n# \u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3092\u63cf\u304f\r\nax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], s=100,\r\nlinewidth=1, facecolors='none', edgecolors='k')\r\nplt.title(\"Linear SVM on Generated Data\")\r\nplt.xlabel(\"Feature 1\")\r\nplt.ylabel(\"Feature 2\")\r\nplt.show()<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110223-300x211.png\" alt=\"\" width=\"300\" height=\"211\" class=\"alignnone size-medium wp-image-5105\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110223-300x211.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110223.png 625w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\r\n<p>\u3053\u306e\u3088\u3046\u306b\u4e8c\u3064\u306e\u30af\u30e9\u30b9\u3092\u5206\u96e2\u3059\u308b\u3088\u3046\u306a\u6c7a\u5b9a\u5883\u754c\u304c\u5f97\u3089\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002\u306a\u304a\u3001\u3053\u3053\u3067\u4e38\u306e\u4ed8\u3044\u305f\u30c7\u30fc\u30bf\u306f\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3067\u3042\u308a\u3053\u308c\u3089\u306e\u8ddd\u96e2\u304c\u6700\u5927\u306b\u306a\u308b\u3088\u3046\u306b\u30e2\u30c7\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\n\n<h2>\u672a\u77e5\u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u4e88\u6e2c<\/h2>\n<p>\u305d\u308c\u3067\u306f\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u3092\u7528\u3044\u3066\u672a\u77e5\u306e\u30c7\u30fc\u30bf\u304c\u5165\u529b\u3055\u308c\u305f\u3068\u304d\u306b\u3069\u306e\u3088\u3046\u306b\u4e88\u6e2c\u3059\u308b\u306e\u304b\u3092\u78ba\u8a8d\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\r\n<p>\u4f5c\u6210\u3057\u305f\u672a\u77e5\u30c7\u30fc\u30bf\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u672a\u77e5\u30c7\u30fc\u30bf\u306e\u751f\u6210\r\nunknown_data = np.array([[2, 3], [5, 6], [3, 4]])<\/code><\/pre>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110934-300x207.png\" alt=\"\" width=\"300\" height=\"207\" class=\"alignnone size-medium wp-image-5106\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110934-300x207.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/06\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-06-12-110934.png 590w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\r\n<\/div>\r\n<p>\u00d7\u3067\u8868\u3055\u308c\u3066\u3044\u308b\u306e\u304c\u672a\u77e5\u30c7\u30fc\u30bf\u3067\u3059\u3002\u3053\u308c\u306f\u660e\u3089\u304b\u306b\u6c34\u8272\u306e\u30af\u30e9\u30b9\u306b\u6240\u5c5e\u3057\u305d\u3046\u3067\u3059\u304c\u30e2\u30c7\u30eb\u306f\u3069\u306e\u69d8\u306b\u4e88\u6e2c\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u672a\u77e5\u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u4e88\u6e2c\r\npredictions = model.predict(unknown_data)\r\n# \u4e88\u6e2c\u7d50\u679c\u306e\u8868\u793a\r\nfor i, point in enumerate(unknown_data):\r\nprint(f\"Point {point} is predicted to be in class {predictions[i]}\")<\/code><\/pre>\r\n<\/div>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>Point [2 3] is predicted to be in class 0\r\nPoint [5 6] is predicted to be in class 0\r\nPoint [3 4] is predicted to be in class 0<\/code><\/pre>\r\n<p>\u3053\u306e\u3088\u3046\u306b\u3069\u306e\u672a\u77e5\u30c7\u30fc\u30bf\u3082\u30af\u30e9\u30b9\uff10\u3001\u3059\u306a\u308f\u3061\u6c34\u8272\u306e\u30af\u30e9\u30b9\u3067\u3042\u308b\u3068\u3044\u3063\u305f\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u6b63\u3057\u304f\u4e88\u6e2c\u3055\u308c\u305f\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<\/div>\n\n<h2>\u7d42\u308f\u308a\u306b<\/h2>\n<p>\u4eca\u56de\u306f\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u7dda\u5f62\u5206\u985e\u306b\u304a\u3051\u308b\u5b9f\u88c5\u3092\u6271\u3044\u307e\u3057\u305f\u3002\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u6d41\u308c\u3084\u30a4\u30e1\u30fc\u30b8\u304c\u3088\u308a\u63b4\u307f\u3084\u3059\u304f\u306a\u3063\u305f\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\r\n<p>\u3053\u3053\u307e\u3067\u3001\u7dda\u5f62\u5206\u96e2\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306b\u3064\u3044\u3066\u76f4\u89b3\u7684\u7406\u89e3\u3001\u6570\u7406\u7684\u7406\u89e3\u3001\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3057\u305f\u304c\u6b21\u56de\u304b\u3089\u306f\u975e\u7dda\u5f62\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u3092\u6271\u3044\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f\u6a5f\u68b0\u5b66\u7fd2\u624b\u6cd5\u306e\u4e00\u3064\u3067\u3042\u308b\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306b\u3064\u3044\u3066python\u3092\u7528\u3044\u3066\u5b9f\u88c5\u3092\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u306f\u3058\u3081\u306b \u4eca\u56de\u306f\u524d\u56de\u6271\u3063\u305f\u6a5f\u68b0\u5b66\u7fd2\u624b\u6cd5\u306e\u4e00\u3064\u3067\u3042\u308b\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\u306e\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3059\u3002\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":2525,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[641,241,642,419],"class_list":["post-5103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-641","tag-241","tag-642","tag-419"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/5103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=5103"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/5103\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/2525"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=5103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=5103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=5103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}