{"id":12352,"date":"2024-10-20T16:35:03","date_gmt":"2024-10-20T07:35:03","guid":{"rendered":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/?p=12352"},"modified":"2024-10-20T21:11:32","modified_gmt":"2024-10-20T12:11:32","slug":"sklearn-svc","status":"publish","type":"post","link":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/2024\/10\/20\/sklearn-svc\/","title":{"rendered":"\u3010Python\/scikit-learn\u3011\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30de\u30b7\u30f3\uff08SVM\uff09\u3092\u4f7f\u3063\u305f\u30b7\u30f3\u30d7\u30eb\u306a\u5206\u985e\u30e2\u30c7\u30eb\u306e\u4f5c\u6210\u3068\u53ef\u8996\u5316"},"content":{"rendered":"<p>\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30de\u30b7\u30f3\uff08SVM\uff09\u306f\u3001\u5206\u985e\u3084\u56de\u5e30\u3001\u7570\u5e38\u691c\u77e5\u306a\u3069\u306b\u4f7f\u7528\u3055\u308c\u308b\u5f37\u529b\u306a\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002SVM\u306f\u3001\u30c7\u30fc\u30bf\u3092\u7570\u306a\u308b\u30af\u30e9\u30b9\u306b\u5206\u985e\u3059\u308b\u305f\u3081\u306b\u3001\u30c7\u30fc\u30bf\u9593\u306e\u300c\u30de\u30fc\u30b8\u30f3\u300d\u3092\u6700\u5927\u5316\u3059\u308b\u5883\u754c\u7dda\uff08\u30cf\u30a4\u30d1\u30fc\u30d7\u30ec\u30fc\u30f3\uff09\u3092\u898b\u3064\u3051\u307e\u3059\u3002\u3053\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u3001\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u3082\u30ab\u30fc\u30cd\u30eb\u30c8\u30ea\u30c3\u30af\u3068\u547c\u3070\u308c\u308b\u624b\u6cd5\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u3092\u9ad8\u6b21\u5143\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3053\u3068\u3067\u52b9\u679c\u7684\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<p>Scikit-learn\u3067\u306fSVM\u3092\u7c21\u5358\u306b\u5b9f\u88c5\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306fScikit-learn\u3067SVM\u3092\u4f7f\u7528\u3057\u30662\u30af\u30e9\u30b9\u5206\u985e\u3092\u884c\u3046\u4f8b\u3067\u3059\u3002Scikit-learn\u306b\u5185\u8535\u3055\u308c\u3066\u3044\u308b &#8220;`make_blobs&#8220;` \u95a2\u6570\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u5206\u985e\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# \u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom sklearn import datasets\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.svm import SVC\r\nfrom sklearn.metrics import accuracy_score\r\n\r\n# 2\u30af\u30e9\u30b9\u306e\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u751f\u6210\r\nX, y = datasets.make_blobs(n_samples=100, centers=2, random_state=6)\r\n\r\n# \u8a13\u7df4\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\r\n\r\n# SVM\u30e2\u30c7\u30eb\u306e\u4f5c\u6210\uff08\u7dda\u5f62\u30ab\u30fc\u30cd\u30eb\uff09\r\nsvm_model = SVC(kernel='linear')\r\n\r\n# \u30e2\u30c7\u30eb\u306e\u8a13\u7df4\r\nsvm_model.fit(X_train, y_train)\r\n\r\n# \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u4e88\u6e2c\r\ny_pred = svm_model.predict(X_test)\r\n\r\n# \u30e2\u30c7\u30eb\u306e\u7cbe\u5ea6\u3092\u8a08\u7b97\r\naccuracy = accuracy_score(y_test, y_pred)\r\nprint(f&quot;Accuracy: {accuracy * 100:.2f}%&quot;)\r\n\r\n# \u5883\u754c\u7dda\u3092\u63cf\u753b\r\ndef plot_svm_decision_boundary(model, X, y):\r\n    # \u8ef8\u306e\u7bc4\u56f2\u3092\u53d6\u5f97\r\n    x_min, x_max = X&#x5B;:, 0].min() - 1, X&#x5B;:, 0].max() + 1\r\n    y_min, y_max = X&#x5B;:, 1].min() - 1, X&#x5B;:, 1].max() + 1\r\n    xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),\r\n                         np.arange(y_min, y_max, 0.01))\r\n\r\n    # \u5404\u70b9\u306e\u30af\u30e9\u30b9\u3092\u4e88\u6e2c\r\n    Z = model.predict(np.c_&#x5B;xx.ravel(), yy.ravel()])\r\n    Z = Z.reshape(xx.shape)\r\n\r\n    # \u5883\u754c\u7dda\u306e\u63cf\u753b\r\n    plt.contourf(xx, yy, Z, alpha=0.8)\r\n    plt.scatter(X&#x5B;:, 0], X&#x5B;:, 1], c=y, edgecolors='k', marker='o')\r\n    plt.show()\r\n\r\n# \u8a13\u7df4\u30c7\u30fc\u30bf\u3068SVM\u306b\u3088\u308b\u5883\u754c\u7dda\u3092\u63cf\u753b\r\nplot_svm_decision_boundary(svm_model, X_test, y_test)\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u89e3\u8aac<\/h2>\n<ul>\n<li><strong>make_blobs<\/strong> \u95a2\u6570\u30672\u30af\u30e9\u30b9\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30c7\u30fc\u30bf\u3092\u30012\u6b21\u5143\u5e73\u9762\u4e0a\u3067\u6563\u5e03\u3055\u308c\u305f2\u3064\u306e\u30af\u30e9\u30b9\u306b\u5206\u3051\u307e\u3059\u3002<\/li>\n<li><strong>train_test_split<\/strong>\u3092\u4f7f\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u8a13\u7df4\u7528\u3068\u30c6\u30b9\u30c8\u7528\u306b\u5206\u5272\u3057\u307e\u3059\u3002<\/li>\n<li><strong>SVC<\/strong>\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066SVM\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f <strong>kernel=&#8217;linear&#8217;<\/strong> \u3068\u3057\u3066\u7dda\u5f62SVM\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u304c\u3001 <strong>kernel=&#8217;rbf&#8217;<\/strong>\uff08RBF\u30ab\u30fc\u30cd\u30eb\uff09\u306a\u3069\u306e\u975e\u7dda\u5f62\u30ab\u30fc\u30cd\u30eb\u3082\u4f7f\u7528\u53ef\u80fd\u3067\u3059\u3002<\/li>\n<li>\u30e2\u30c7\u30eb\u3092\u8a13\u7df4\u3057\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3067\u4e88\u6e2c\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n<li>\u4e88\u6e2c\u7d50\u679c\u306e\u7cbe\u5ea6\u3092 <strong>accuracy_score<\/strong>\u3067\u8a08\u7b97\u3057\u307e\u3059\u3002<\/li>\n<li>\u6700\u5f8c\u306b\u3001SVM\u304c\u751f\u6210\u3057\u305f\u30af\u30e9\u30b9\u5883\u754c\u3092\u30d7\u30ed\u30c3\u30c8\u3057\u3066\u3001\u30c7\u30fc\u30bf\u3068\u3068\u3082\u306b\u8996\u899a\u5316\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>make_blobs<\/strong> \u95a2\u6570\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306e\u8a18\u4e8b\u306b\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u3092\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"linkcard\"><div class=\"lkc-internal-wrap\"><a class=\"lkc-link no_icon\" href=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/2024\/10\/20\/make-blobs\/\" data-lkc-id=\"316\" target=\"_blank\"><div class=\"lkc-card\"><div class=\"lkc-info\"><div class=\"lkc-domain\">\u975eIT\u4f01\u696d\u306b\u52e4\u3081\u308b\u4e2d\u5e74\u30b5\u30e9\u30ea\u30fc\u30de\u30f3\u306eIT\u65e5\u8a18<\/div><\/div><div class=\"lkc-content\"><figure class=\"lkc-thumbnail\"><img decoding=\"async\" class=\"lkc-thumbnail-img\" src=\"\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/blobs_eye-150x150.png\" width=\"100px\" height=\"108px\" alt=\"\" \/><\/figure><div class=\"lkc-title\">\u3010Python\/scikit-learn\u3011make_blobs\u3092\u4f7f\u3063\u3066\u6a5f\u68b0\u5b66\u7fd2\u7528\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u4f5c\u6210\u3059\u308b<\/div><div class=\"lkc-excerpt\">make_blobs\u95a2\u6570\u306f\u3001Scikit-learn\u306edatasets\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3084\u5206\u985e\u306e\u30c6\u30b9\u30c8\u7528\u306b\u4eba\u5de5\u7684\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u7c21\u5358\u306b\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u8907\u6570\u306e\u30af\u30e9\u30b9\u30bf\u30fc\u3092\u6301\u3064\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3059\u308b\u306e\u306b\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002make_blobs\u95a2\u6570\u306e\u4e3b\u306a\u30d1\u30e9\u30e1\u30fc\u30bf \tn_samples: \u751f\u6210\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u6570\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001n_samples=100\u306f100\u500b\u306e\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u3092\u751f\u6210\u3057\u307e\u3059\u3002 \tcenters: \u751f\u6210\u3059\u308b\u30af\u30e9\u30b9\u30bf\u306e\u6570\u3001\u3082\u3057\u304f\u306f\u30af\u30e9\u30b9\u30bf\u306e\u4e2d\u5fc3\u4f4d\u7f6e\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002centers=3\u3068\u6307\u5b9a\u3059\u308b\u3068\u30013\u3064\u306e\u30af\u30e9\u30b9\u30bf\u3092\u6301\u3064\u30c7\u30fc\u30bf\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002 \tn_features: ...<\/div><\/div><div class=\"clear\"><\/div><\/div><\/a><\/div><\/div>\n<p>&nbsp;<\/p>\n<h2>\u51fa\u529b\u7d50\u679c\u306e\u89e3\u8aac<\/h2>\n<p>\u51fa\u529b\u7d50\u679c\u306f\u3053\u3061\u3089<\/p>\n<p><a href=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/svm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12353\" src=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/svm.png\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h5>\u80cc\u666f\u306e\u8272\u5206\u3051<\/h5>\n<ul>\n<li>\u80cc\u666f\u306e<span style=\"color: #800080;\"><strong>\u7d2b\u8272\u306e\u90e8\u5206<\/strong><\/span>\u306f\u3001\u4e00\u65b9\u306e\u30af\u30e9\u30b9\u306b\u5206\u985e\u3055\u308c\u305f\u9818\u57df\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li>\u80cc\u666f\u306e<span style=\"color: #ff9900;\"><strong>\u9ec4\u8272\u306e\u90e8\u5206<\/strong><\/span>\u306f\u3001\u3082\u3046\u4e00\u65b9\u306e\u30af\u30e9\u30b9\u306b\u5206\u985e\u3055\u308c\u305f\u9818\u57df\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li>\u3053\u306e2\u8272\u306e\u5883\u754c\u7dda\u306f\u3001SVM\u304c\u30c7\u30fc\u30bf\u3092\u5206\u3051\u308b\u305f\u3081\u306b\u898b\u3064\u3051\u305f\u30cf\u30a4\u30d1\u30fc\u30d7\u30ec\u30fc\u30f3\uff08\u6700\u9069\u306a\u5206\u985e\u5883\u754c\u7dda\uff09\u3067\u3059\u3002\u3053\u306e\u7dda\u306f\u3001\u30c7\u30fc\u30bf\u9593\u306e\u30de\u30fc\u30b8\u30f3\u3092\u6700\u5927\u5316\u3059\u308b\u3088\u3046\u306b\u8abf\u6574\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<h5>\u30c7\u30fc\u30bf\u70b9<\/h5>\n<ul>\n<li><span style=\"color: #800080;\"><strong>\u7d2b\u8272\u306e\u4e38<\/strong><\/span>\u306f\u4e00\u65b9\u306e\u30af\u30e9\u30b9\u306b\u5c5e\u3059\u308b\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li><span style=\"color: #ff9900;\"><strong>\u9ec4\u8272\u306e\u4e38<\/strong><\/span>\u306f\u3001\u3082\u3046\u4e00\u65b9\u306e\u30af\u30e9\u30b9\u306b\u5c5e\u3059\u308b\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li>\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\u306e\u6563\u3089\u3070\u308a\u65b9\u304b\u3089\u30012\u3064\u306e\u30af\u30e9\u30b9\u304c\u3042\u308b\u7a0b\u5ea6\u306f\u3063\u304d\u308a\u3068\u5206\u304b\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u304c\u3001\u5b8c\u5168\u306b\u7dda\u5f62\u5206\u96e2\u3067\u304d\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u304c\u898b\u3066\u53d6\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n<h5><strong>\u5206\u985e\u5883\u754c\u7dda<\/strong><\/h5>\n<ul>\n<li>\u30af\u30e9\u30b9\u9593\u306e\u5883\u754c\u7dda\uff08\u30cf\u30a4\u30d1\u30fc\u30d7\u30ec\u30fc\u30f3\uff09\u306f\u3001\u30c7\u30fc\u30bf\u306e\u5206\u5e03\u3092\u3082\u3068\u306bSVM\u304c\u8a08\u7b97\u3057\u305f\u3082\u306e\u3067\u3001\u3053\u306e\u7dda\u3092\u57fa\u6e96\u306b\u3069\u3061\u3089\u306e\u30af\u30e9\u30b9\u306b\u5c5e\u3059\u308b\u304b\u304c\u6c7a\u307e\u308a\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u7dda\u306f\u3084\u3084\u50be\u3044\u3066\u304a\u308a\u3001\u30af\u30e9\u30b9\u306e\u9593\u306b\u5927\u304d\u306a\u30de\u30fc\u30b8\u30f3\u3092\u78ba\u4fdd\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>SVM\u306f\u3001\u7dda\u5f62\u304a\u3088\u3073\u975e\u7dda\u5f62\u306e\u5206\u985e\u306b\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002Scikit-learn\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u975e\u5e38\u306b\u5c11\u306a\u3044\u30b3\u30fc\u30c9\u3067SVM\u3092\u5b9f\u88c5\u3057\u3066\u30c7\u30fc\u30bf\u3092\u5206\u985e\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u30ab\u30fc\u30cd\u30eb\u306e\u9078\u629e\u306b\u3088\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u5f62\u72b6\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u67d4\u8edf\u306b\u9069\u5fdc\u3067\u304d\u308b\u305f\u3081\u3001\u5b9f\u969b\u306e\u554f\u984c\u306b\u5bfe\u3057\u3066\u3082\u975e\u5e38\u306b\u52b9\u679c\u7684\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30de\u30b7\u30f3\uff08SVM\uff09\u306f\u3001\u5206\u985e\u3084\u56de\u5e30\u3001\u7570\u5e38\u691c\u77e5\u306a\u3069\u306b\u4f7f\u7528\u3055\u308c\u308b\u5f37\u529b\u306a\u6a5f\u68b0 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":12355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-12352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/comments?post=12352"}],"version-history":[{"count":3,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12352\/revisions"}],"predecessor-version":[{"id":12361,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12352\/revisions\/12361"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media\/12355"}],"wp:attachment":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media?parent=12352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/categories?post=12352"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/tags?post=12352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}