{"id":248,"date":"2020-01-24T15:33:46","date_gmt":"2020-01-24T06:33:46","guid":{"rendered":"http:\/\/aiacademy.jp\/media\/?p=248"},"modified":"2024-08-08T16:28:40","modified_gmt":"2024-08-08T07:28:40","slug":"svm%ef%bc%88%e3%82%b5%e3%83%9d%e3%83%bc%e3%83%88%e3%83%99%e3%82%af%e3%82%bf%e3%83%bc%e3%83%9e%e3%82%b7%e3%83%b3%ef%bc%89%e3%81%a8%e3%81%af","status":"publish","type":"post","link":"https:\/\/aiacademy.jp\/media\/?p=248","title":{"rendered":"SVM\uff08\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u30de\u30b7\u30f3\uff09\u3068\u306f"},"content":{"rendered":"<div id=\"sgb-css-id-1\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size is-style-fill has-medium-font-size\"><a class=\"wp-block-button__link has-vivid-green-cyan-background-color has-background wp-element-button\" href=\"https:\/\/lin.ee\/3E4GzWk\" rel=\"nofollow noopener\" target=\"_blank\">LINE\u53cb\u3060\u3061\u767b\u9332 \u25b6 \u6a5f\u68b0\u5b66\u7fd2\u30b3\u30fc\u30b9 \u25b6 \u53d7\u8b1b\u5272\u5f15\u30af\u30fc\u30dd\u30f3 \u7121\u6599\u52d5\u753b<\/a><\/div>\n<\/div>\n<\/div>\n\n<div class=\"toc\">    <div id=\"toc_container\" class=\"sgb-toc--no-bullets js-smooth-scroll\" data-dialog-title=\"Table of Contents\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">\u306f\u3058\u3081\u306b<\/a>  <\/li>  <li>    <a href=\"#i-1\">SVM(Support Vector Machine)<\/a>  <\/li>  <li>    <a href=\"#i-2\">\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3068\u306f<\/a>  <\/li>  <li>    <a href=\"#i-3\">SVM\u306e\u4ed5\u7d44\u307f<\/a>  <\/li>  <li>    <a href=\"#i-4\">SVM\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8<\/a>  <\/li>  <li>    <a href=\"#i-5\">\u7dda\u5f62SVM\u3068\u975e\u7dda\u5f62SVM<\/a>  <\/li>  <li>    <a href=\"#i-6\">SVM\u306e\u5b9f\u88c5\u4f8b<\/a>  <\/li>  <li>    <a href=\"#i-7\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/a>  <\/li>  <li class=\"last\">    <a href=\"#i-8\">\u307e\u3068\u3081<\/a>  <\/li><\/ul>\n      \n    <\/div><\/div><div class=\"toc\"><p><\/p>\n<\/div><h2 id=\"i-0\">\u306f\u3058\u3081\u306b<\/h2>\n<p>\u3053\u306e\u7ae0\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001Jupyter Notebook\u306eScikit-learn\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u20190.20.0\u2019\u53ca\u3073\u3001<br>\nGoogle Colab\u4e0a\u3067Scikit-learn\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u20190.20.2\u2019\u306b\u3066\u52d5\u4f5c\u78ba\u8a8d\u304c\u3067\u304d\u3066\u3044\u307e\u3059\u3002<br>\n\u305d\u308c\u4ee5\u524d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u306f\u52d5\u4f5c\u3057\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2 id=\"i-1\">SVM(Support Vector Machine)<\/h2>\n<p><strong>SVM(Support Vector Machine)\u306f\u3001\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u306e\u4e00\u7a2e\u3067\u3001\u975e\u5e38\u306b\u5f37\u529b\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002<\/strong><br>\n\u6559\u5e2b\u3042\u308a\u5b66\u7fd2\u3067\u3001\u5206\u985e\u3068\u56de\u5e30\u3092\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u4e3b\u306b\u5206\u985e\u306e\u30bf\u30b9\u30af\u3067\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h2 id=\"i-2\">\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3068\u306f<\/h2>\n<p>\u307e\u305a\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u540d\u524d\u306b\u3082\u51fa\u3066\u304f\u308bSupport Vector\u3068\u306f\u3001\u30c7\u30fc\u30bf\u3092\u5206\u5272\u3059\u308b\u76f4\u7dda\u306b\u6700\u3082\u8fd1\u3044\u30c7\u30fc\u30bf\u70b9\u306e\u4e8b\u3067\u3059\u3002<br>\nSVM\u3067\u306f\u3001\u3053\u306e\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u304c\u5927\u304d\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\u3002<br>\n\u307e\u305f\u3001\u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3092\u5b9a\u3081\u3066\u3053\u306e\u3088\u3046\u306a\u5206\u5272\u7dda\u304c\u6c7a\u307e\u308c\u3070\u3001\u3042\u3068\u306f\u30b3\u30ec\u3088\u308a\u4e0a\u306b\u3042\u308b\u304b\u4e0b\u306b\u3042\u308b\u304b\u3067\u3001\u3069\u306e\u30af\u30e9\u30b9\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u306e\u4e88\u6e2c\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/qiita-image-store.s3.amazonaws.com\/0\/171715\/2811fd5f-c030-874f-e3bb-e4af866f4705.jpeg\" alt=\"\"><\/p>\n<h2 id=\"i-3\">SVM\u306e\u4ed5\u7d44\u307f<\/h2>\n<p>SVM\u306e\u809d\u306f\u3001\u30de\u30fc\u30b8\u30f3\u6700\u5927\u5316\u3068\u30ab\u30fc\u30cd\u30eb\u6cd5\u306b\u3088\u308a\u975e\u7dda\u5f62\u30c7\u30fc\u30bf\u3092\u6271\u3048\u308b\u3068\u3044\u3046\u3068\u3053\u308d\u3067\u3059\u3002<br>\n\u305f\u3060\u3001SVM\u306e\u4ed5\u7d44\u307f\u3092\u304d\u3061\u3093\u3068\u7406\u89e3\u3057\u3088\u3046\u3068\u601d\u3046\u3068\u305d\u308c\u306a\u308a\u306b\u9aa8\u304c\u6298\u308c\u307e\u3059\u3002<br>\n<strong>\u3061\u3087\u3063\u3068\u7406\u8ad6\u306f\u307e\u3060\u624b\u3092\u3064\u3051\u308b\u4e88\u5b9a\u304c\u306a\u3044<\/strong>\u304c\u3001<strong>\u5f37\u529b\u3068\u5642\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u8a66\u3057\u3066\u307f\u305f\u3044\u3002<\/strong>\u3068\u3044\u3046\u65b9\u306f\u3001\u4ed5\u7d44\u307f\u3092\u6c17\u306b\u305b\u305a\u8aad\u307f\u9032\u3081\u3066\u304f\u3060\u3055\u3044\u3002<br>\n\u3068\u3044\u3046\u306e\u3082\u3001SVM\u306e\u5b9f\u88c5\u306fscikit-learn\u3092\u4f7f\u3048\u3070\u3001\u3068\u3066\u3082\u7c21\u5358\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3089\u3067\u3059\u3002<br>\n\u3053\u3053\u306e\u7406\u8ad6\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u77e5\u308a\u305f\u3044\u3068\u3044\u3046\u65b9\u306f\u3001<a href=\"?id=116\">\u201c\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0(\u7406\u8ad6\u7de8)\u201d\u306e\u201dSVM\u201d<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\n(\u5927\u5b66\u3067\u5b66\u3076\u57fa\u790e\u6570\u5b66\u3068\u6570\u7406\u6700\u9069\u5316\u306e\u77e5\u8b58\u304c\u3042\u308b\u65b9\u306f\u30c8\u30e9\u30a4\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\u3061\u306a\u307f\u306b\u3001SVM\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306f\u96e3\u3057\u3044\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002)<\/p>\n<h2 id=\"i-4\">SVM\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8<\/h2>\n<p>\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u629e\u3059\u308b\u4e0a\u3067\u3001\u5404\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8\u3092\u628a\u63e1\u3057\u3066\u304a\u304f\u4e8b\u306f\u91cd\u8981\u3067\u3059\u3002<br>\n<strong>\u25cf SVM\u306e\u30e1\u30ea\u30c3\u30c8<\/strong><br>\n\u30c7\u30fc\u30bf\u306e\u6b21\u5143\u304c\u5927\u304d\u304f\u306a\u3063\u3066\u3082\u8b58\u5225\u7cbe\u5ea6\u304c\u826f\u3044<br>\n\u6700\u9069\u5316\u3059\u3079\u304d\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5c11\u306a\u3044<\/p>\n<p><strong>\u25cf SVM\u306e\u30c7\u30e1\u30ea\u30c3\u30c8<\/strong><br>\n\u5b66\u7fd2\u30c7\u30fc\u30bf\u304c\u5897\u3048\u308b\u3068\u8a08\u7b97\u91cf\u304c\u81a8\u5927\u306b\u306a\u308b<br>\n\u57fa\u672c\u7684\u306b\uff12\u30af\u30e9\u30b9\u5206\u985e\u306b\u7279\u5316\u3057\u3066\u3044\u308b<br>\n\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u304c\u5fc5\u8981<br>\n(SVM\u3067\u306f\u8ddd\u96e2\u3092\u6e2c\u5b9a\u3059\u308b\u306e\u3067\u3001\u5927\u304d\u3044\u7bc4\u56f2\u3092\u3068\u308b\u7279\u5fb4\u91cf\u306b\u5f15\u304d\u305a\u3089\u308c\u306a\u3044\u3088\u3046\u306b\u3059\u308b)<\/p>\n<p><a href=\"https:\/\/aiacademy.jp\/bootcamp\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2020\/01\/bootcamp_banner2-1.png\" alt=\"\" width=\"1200\" height=\"400\" class=\"alignnone size-full wp-image-502\"><\/a><\/p>\n<h2 id=\"i-5\">\u7dda\u5f62SVM\u3068\u975e\u7dda\u5f62SVM<\/h2>\n<p><strong>SVM\u3067\u306f\u3001\u7dda\u5f62\u3068\u975e\u7dda\u5f62\u3092\u6271\u3046\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/strong><br>\n\u975e\u7dda\u5f62SVM\u3068\u306f\u6b21\u306e\u3088\u3046\u306aSVM\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/qiita-image-store.s3.amazonaws.com\/0\/155702\/a9a4c2ea-a6ec-e957-9389-a60118542b8f.png\" alt=\"\"><\/p>\n<p>Scikit-learn\u306eLinearSVC()\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u7dda\u5f62SVM\u3092\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\n\u307e\u305f\u3001SVC()\u3092\u4f7f\u3046\u3053\u3068\u3067\u975e\u7dda\u5f62SVM\u3092\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code># \u7dda\u5f62SVM\u306e\u5834\u5408\nfrom sklearn.svm import LinearSVC\nmodel1 = LinearSVC()\n\n# \u975e\u7dda\u5f62SVM\u306e\u5834\u5408\nfrom sklearn.svm import SVC\nmodel2 = SVC()\n<\/code><\/pre>\n<h2 id=\"i-6\">SVM\u306e\u5b9f\u88c5\u4f8b<\/h2>\n<p>\u5b9f\u88c5\u4f8b\u3068\u3057\u3066\u3001SVM\u3067\u753b\u50cf\u8a8d\u8b58\u3092\u884c\u3044\u307e\u3059\u3002<br>\n\u307e\u305a\u3001\u306f\u3058\u3081\u306bScikit-learn\u306b\u3042\u3089\u304b\u3058\u3081\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<br>\n\uff08\u521d\u3081\u3066\u884c\u3046\u969b\u306b\u3001\u3053\u306e\u4f5c\u696d\u306f\u6570\u5206\u304b\u304b\u308a\u307e\u3059\u3002\uff09<\/p>\n<pre><code>import sklearn as sk\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import fetch_olivetti_faces\n\nfaces = fetch_olivetti_faces()\nprint(faces.DESCR)\n<\/code><\/pre>\n<p>faces.DESCR\u306b\u3088\u3063\u3066\u3001\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u8aac\u660e\u3092\u307f\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\n\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u3001<br>\n\u3053\u306e\u30c7\u30fc\u30bf\u306f40\u4eba\u306e\u9854\u309210\u679a\u305a\u3064\u306e\u8a08400\u679a\u306e\u753b\u50cf\u304c\u3042\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<br>\n\u307e\u305f\u3001\u3053\u306e\u753b\u50cf\u30c7\u30fc\u30bf\u306f64\u00d764 pixel\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>\n\u3069\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u304c\u3042\u308b\u304b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306b\u3088\u3063\u3066\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>print(faces.keys())\n<\/code><\/pre>\n<p>\u6b21\u306e\u3088\u3046\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell:\">['target', 'data', 'DESCR', 'images']\n<\/code><\/pre>\n<p>shape\u306b\u3088\u3063\u3066\u3001\u305d\u306e\u30c7\u30fc\u30bf\u306e\u8981\u7d20\u6570\u304c\u78ba\u8a8d\u3067\u304d\u308b\u306e\u3067\u307f\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>print(faces.images.shape)\nprint(faces.data.shape)\nprint(faces.target.shape)\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell:\">(400, 64, 64)\n(400, 4096)\n(400,)\n<\/code><\/pre>\n<p>images\u306f400\u00d764\u00d764\uff0864\u00d764\u306e\u753b\u50cf400\u679a\uff09\u3001<br>\ndata\u306f400\u00d74096\uff0864\u00d764\u3092\u4e00\u5217\u306b\u3057\u305f\u3082\u306e400\u500b\uff09<br>\ntarget\u306f400\u00d71\uff08\u6b63\u89e3\u30c7\u30fc\u30bf400\u500b\uff09<br>\n\u3068\u3044\u3046\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<br>\n\u3067\u306f\u3001\u5b9f\u969b\u306b\u30c7\u30fc\u30bf\u3092plot\u3057\u3066\u753b\u50cf\u3092\u51fa\u529b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\uff08\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6700\u521d\u306e\u3046\u3061\u306f\u7406\u89e3\u51fa\u6765\u306a\u304f\u3066\u3082\u69cb\u3044\u307e\u305b\u3093\u3002\uff09<\/p>\n<pre><code>def print_faces(images, target, top_n):\n    #\u51fa\u529b\u3059\u308b\u753b\u50cf\u306e\u30b5\u30a4\u30ba\u3092\u8a2d\u5b9a\u3059\u308b\n    fig = plt.figure(figsize=(12, 12))\n    fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)\n    for i in range(top_n):\n        p = fig.add_subplot(20, 20,i+1,xticks=[],yticks=[])\n        p.imshow(images[i],cmap=plt.cm.bone)\n\n        #\u6b63\u89e3\u30e9\u30d9\u30eb\u3092\u8868\u793a\n        p.text(0,14,str(target[i]))\n        p.text(0,60,str(i))\n    plt.show() # \u5b9f\u884c\u74b0\u5883\u306b\u3088\u3063\u3066\u306f\u306a\u304f\u3066\u3082\u63cf\u753b\u3055\u308c\u307e\u3059\u3002\n\nprint_faces(faces.images, faces.target,20)\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u9854\u306e\u753b\u50cf\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/camo.qiitausercontent.com\/76af97a33a4fe8c66a9c583dbf12afbc8439b733\/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3130353838372f37303232616636322d333064312d323064652d386433652d3735396465353234373938612e706e67\" alt=\"\"><\/p>\n<p>\u30c7\u30fc\u30bf\u306e\u4e2d\u8eab\u304c\u78ba\u8a8d\u3067\u304d\u305f\u306e\u3067\u3001\u5206\u985e\u3092\u884c\u306a\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u4eca\u56de\u306f\u3001\u753b\u50cf\u30c7\u30fc\u30bf\u304b\u3089\u305d\u308c\u304c\u8ab0\u306a\u306e\u304b\u3092\u5206\u985e\u3057\u307e\u3059\u3002<br>\nscikit-learn\u306eSVC(Support Vector Classifier)\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><code>from sklearn.svm import SVC\nsvc_1 = SVC(kernel='linear')\n<\/code><\/pre>\n<p><strong>SVC\u306b\u306f\u3001\u69d8\u3005\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u3042\u308a\u307e\u3059\u304c\u4e00\u756a\u91cd\u8981\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u306fkernel\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/strong><br>\n\u3053\u3053\u3067\u306f\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u305b\u3093\u304c\u3069\u306e\u3088\u3046\u306a\u7a7a\u9593\u3092\u4f5c\u6210\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<br>\n\u4eca\u56de\u306f\u4e00\u756a\u5358\u7d14\u3067\u8a08\u7b97\u901f\u5ea6\u304c\u306f\u3084\u3044linear kernel\u3092\u4f7f\u7528\u3057\u307e\u3059<br>\n\uff08\u4ed6\u306b\u5b9a\u756a\u306e\u3082\u306e\u3068\u3057\u3066rbf kernel\u304c\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002\uff09<br>\n\u307e\u305a\u3001\u30c7\u30fc\u30bf\u3092\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\u3057\u307e\u3059\u3002<\/p>\n<pre><code>from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(faces.data, faces.target,test_size=0.25,random_state=0)\n<\/code><\/pre>\n<p>\u305d\u3057\u3066\u3001\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u5b66\u7fd2\u3055\u305b\u4ea4\u5dee\u691c\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<pre><code>from sklearn.model_selection import cross_val_score, KFold\nfrom scipy.stats import sem\n\ndef evaluate_cross_validation(clf, x, y, K):                        \n    cv = KFold(n_splits=K, random_state=0,shuffle=True)\n    scores = cross_val_score(clf,x,y,cv=cv)\n    print(scores)\n    print (\"Mean score: {} (+\/-{})\".format( np.mean (scores), sem(scores)))\n\nevaluate_cross_validation(svc_1,x_train,y_train,5)\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306a\u51fa\u529b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell:\">[ 0.93333333  0.86666667  0.91666667  0.93333333  0.91666667]\nMean score: 0.9133333333333334 (+\/-0.012247448713915886)\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u306e\u30b9\u30b3\u30a2\u3092\u898b\u3066\u307f\u308b\u3068\u300191%\u306e\u30b9\u30b3\u30a2\u304c\u51fa\u3066\u3044\u308b\u306e\u3067\u304b\u306a\u308a\u826f\u3044\u7d50\u679c\u3068\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<br>\n\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u4e21\u65b9\u3067\u6027\u80fd\u3092\u8a55\u4fa1\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>def train_and_evaluate(clf,x_train,x_test,y_train,y_test):\n    clf.fit(x_train,y_train)\n\n    print(\"Accuracy(\u5b66\u7fd2\u30c7\u30fc\u30bf):\",clf.score(x_train,y_train))\n    print(\"Accuracy(\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf):\",clf.score(x_test,y_test))\n\ntrain_and_evaluate(svc_1, x_train,x_test,y_train,y_test)\n<\/code><\/pre>\n<p>\u95a2\u6570train_and_evaluate\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001<br>\nAccuracy(\u5b66\u7fd2\u30c7\u30fc\u30bf): 1.0<br>\nAccuracy(\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf): 0.99<br>\n\u3068\u51fa\u529b\u3055\u308c\u826f\u3044\u30e2\u30c7\u30eb\u304c\u4f5c\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2 id=\"i-7\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/h2>\n<pre><code>import sklearn as sk\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import fetch_olivetti_faces\n\nfaces = fetch_olivetti_faces()\nprint(faces.DESCR)\n\nprint(faces.keys())\n\n# \u8981\u7d20\u6570\u306e\u78ba\u8a8d\nprint(faces.images.shape)\nprint(faces.data.shape)\nprint(faces.target.shape)\n\n\ndef print_faces(images, target, top_n):\n    #\u51fa\u529b\u3059\u308b\u753b\u50cf\u306e\u30b5\u30a4\u30ba\u3092\u8a2d\u5b9a\u3059\u308b\n    fig = plt.figure(figsize=(12, 12))\n    fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)\n    for i in range(top_n):\n        p = fig.add_subplot(20, 20,i+1,xticks=[],yticks=[])\n        p.imshow(images[i],cmap=plt.cm.bone)\n\n        # \u6b63\u89e3\u30e9\u30d9\u30eb\u3092\u8868\u793a\n        p.text(0,14,str(target[i]))\n        p.text(0,60,str(i))\n\nprint_faces(faces.images, faces.target,20)\n\n\nfrom sklearn.svm import SVC\nsvc_1 = SVC(kernel='linear')\n\n\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(faces.data, faces.target,test_size=0.25,random_state=0)\n\n\nfrom sklearn.model_selection import cross_val_score, KFold\nfrom scipy.stats import sem\n\n# \u4ea4\u5dee\u691c\u5b9a\ndef evaluate_cross_validation(clf, x, y, K):\n\n    cv = KFold(n_splits=K, random_state=0,shuffle=True)\n    scores = cross_val_score(clf,x,y,cv=cv)\n    print(scores)\n    print (\"Mean score: {} (+\/-{})\".format( np.mean (scores), sem(scores)))\n\nevaluate_cross_validation(svc_1,x_train,y_train,5)\n\n\ndef train_and_evaluate(clf,x_train,x_test,y_train,y_test):\n    clf.fit(x_train,y_train)\n\n    print(\"Accuracy(\u5b66\u7fd2\u30c7\u30fc\u30bf):\",clf.score(x_train,y_train))\n    print(\"Accuracy(\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf):\",clf.score(x_test,y_test))\n\ntrain_and_evaluate(svc_1, x_train,x_test,y_train,y_test)\n<\/code><\/pre>\n<h2 id=\"i-8\">\u307e\u3068\u3081<\/h2>\n<p>\u3053\u306e\u7ae0\u3067\u306f\u3001SVM\u306b\u95a2\u3057\u3066\u5b66\u3073\u3001Python\u3067SVM\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u304d\u3001\u753b\u50cf\u306e\u5206\u985e\u3092\u884c\u3044\u307e\u3057\u305f\u3002<br>\nSVM\u3068\u306f\u4f55\u304b\u3001\u30e1\u30ea\u30c3\u30c8\u3084\u30c7\u30e1\u30ea\u30c3\u30c8\u3092\u3057\u3063\u304b\u308a\u3068\u304a\u3055\u3048\u307e\u3057\u3087\u3046\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6b21 \u306f\u3058\u3081\u306b SVM(Support Vector Machine) \u30b5\u30dd\u30fc\u30c8\u30d9\u30af\u30bf\u30fc\u3068\u306f SVM\u306e\u4ed5\u7d44\u307f SVM\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8 \u7dda\u5f62SVM\u3068\u975e\u7dda\u5f62SVM SVM\u306e\u5b9f\u88c5\u4f8b \u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0 \u307e\u3068\u3081 \u306f\u3058\u3081 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,105,130],"tags":[],"class_list":{"0":"post-248","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-105","9":"category-130"},"_links":{"self":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":4,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":4878,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/4878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}