{"id":250,"date":"2020-01-24T15:35:30","date_gmt":"2020-01-24T06:35:30","guid":{"rendered":"http:\/\/aiacademy.jp\/media\/?p=250"},"modified":"2024-08-08T16:27:28","modified_gmt":"2024-08-08T07:27:28","slug":"%e6%b1%ba%e5%ae%9a%e6%9c%a8%e3%81%a8%e3%81%af","status":"publish","type":"post","link":"https:\/\/aiacademy.jp\/media\/?p=250","title":{"rendered":"\u6c7a\u5b9a\u6728\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\u30c7\u30fc\u30bf\u30b5\u30a4\u30a8\u30f3\u30b9\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\">    <span><\/span>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-0\">\u306f\u3058\u3081\u306b<\/a>      <\/li>      <li>        <a href=\"#i-1\">\u6c7a\u5b9a\u6728\u3068\u306f<\/a>      <\/li>      <li>        <a href=\"#i-2\">\u8a13\u7df4\u30c7\u30fc\u30bf\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<\/a>      <\/li>      <li>        <a href=\"#i-3\">Google Colab\u3067CSV\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-4\">CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c<\/a>      <\/li>      <li>        <a href=\"#i-5\">\u524d\u51e6\u7406 \/ \u7279\u5fb4\u91cf\u9078\u629e<\/a>      <\/li>      <li>        <a href=\"#i-6\">\u6b20\u640d\u5024\u306e\u88dc\u5b8c<\/a>      <\/li>      <li>        <a href=\"#i-7\">LabelEncoder<\/a>      <\/li>      <li>        <a href=\"#i-8\">OneHotEncoder<\/a>      <\/li>      <li>        <a href=\"#i-9\">\u5b66\u7fd2<\/a>      <\/li>      <li>        <a href=\"#i-10\">\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u53ef\u8996\u5316\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-11\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-12\">\u307e\u3068\u3081<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-13\">Python\u3084\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\uff1f<\/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\u3067\u306f\u6c7a\u5b9a\u6728\u3092\u7528\u3044\u3066\u5206\u985e\u3092\u884c\u3044\u3001\u6728\u3092\u51fa\u529b\u3057\u307e\u3059\u304c\u3001Windows OS\u306e\u74b0\u5883\u306e\u5834\u5408\u3001\u3046\u307e\u304f\u6728\u304c\u51fa\u529b\u3055\u308c\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<br>\n\u305d\u306e\u5834\u5408\u306f\u3001\u3053\u306e\u7ae0\u306e<a href=\"?id=35&amp;section=\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u53ef\u8996\u5316\u3059\u308b\">\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u53ef\u8996\u5316\u3059\u308b<\/a>\u3092\u3088\u304f\u304a\u8aad\u307f\u3044\u305f\u3060\u3051\u307e\u3059\u3068\u5e78\u3044\u3067\u3059\u3002<br>\n\u307e\u305f\u3001Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3084\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u3088\u3063\u3066\u3001Warning\u306a\u3069\u304c\u8868\u793a\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2 id=\"i-1\">\u6c7a\u5b9a\u6728\u3068\u306f<\/h2>\n<p>\u56de\u5e30\u306a\u3069\u306e\u7dda\u5f62\u30e2\u30c7\u30eb\u3084SVM\u3067\u306f\u3001\u305d\u306e\u30e2\u30c7\u30eb\u304c\u3069\u306e\u3088\u3046\u306b\u5206\u985e\u3055\u308c\u305f\u304b\u308f\u304b\u3089\u306a\u3044\u5206\u985e\u904e\u7a0b\u304c\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u306e\u624b\u6cd5\u3067\u3057\u305f\u3002<br>\n\u3057\u304b\u3057\u3001\u3068\u304d\u306b\u306f\u5206\u985e\u904e\u7a0b\u3092\u77e5\u308b\u3053\u3068\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<br>\n\u4f8b\u3048\u3070\u3001\u30e1\u30fc\u30eb\u304c\u30b9\u30d1\u30e0\u30e1\u30fc\u30eb\u304b\u305d\u3046\u3067\u306a\u3044\u304b\u3092\u5206\u985e\u3059\u308b\u4f8b\u3092\u8003\u3048\u308b\u3068<br>\n\u4eca\u307e\u3067\u306e\u5b66\u7fd2\u624b\u6cd5\u3060\u3068\u3069\u306e\u5358\u8a9e\u306b\u3088\u3063\u3066\u30b9\u30d1\u30e0\u30e1\u30fc\u30eb\u3068\u5224\u65ad\u3055\u308c\u305f\u304b\u304c\u308f\u304b\u308a\u307e\u305b\u3093\u3002<br>\n\u306a\u306e\u3067\u3001\u30b9\u30d1\u30e0\u30e1\u30fc\u30eb\u306b\u542b\u307e\u308c\u308b\u5358\u8a9e\u3092\u898b\u3064\u3051\u308b\u306b\u306f\u5206\u985e\u904e\u7a0b\u3092\u77e5\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\n\u4eca\u56de\u5b66\u7fd2\u3059\u308b<strong><em>\u6c7a\u5b9a\u6728\u306e\u4e00\u756a\u306e\u7279\u5fb4\u306f\u3001\u5206\u985e\u904e\u7a0b\u304c\u660e\u77ad\u3068\u3044\u3046\u3053\u3068<\/em><\/strong>\u3067\u3059\u3002<br>\n\u6c7a\u5b9a\u6728\u306f\u3068\u3066\u3082\u5358\u7d14\u306a\u624b\u6cd5\u3067\u3059\u304c\u3001\u975e\u5e38\u306b\u52b9\u679c\u7684\u306a\u6559\u5e2b\u3042\u308a\u5b66\u7fd2\u624b\u6cd5\u3067\u3059\u3002<br>\n\u6c7a\u5b9a\u6728\u306e\u7c21\u5358\u306a\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/camo.qiitausercontent.com\/e4b56fcac8b471f598d5a279b02cd7fef02bb654\/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3130353838372f61336434666137312d643964652d396365382d333634662d3930303838646139373564382e706e67\" alt=\"\"><\/p>\n<p>\u6700\u521d\u306b\u3001\u305d\u306e\u30e1\u30fc\u30eb\u306e\u6587\u66f8\u306b\u300c\u7121\u6599\u300d\u3068\u3044\u3046\u5358\u8a9e\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3092\u554f\u3044\u307e\u3059\u3002<br>\n\u3082\u3057\u3001\u542b\u307e\u308c\u3066\u3044\u308b\u306a\u3089\u305d\u306e\u30e1\u30fc\u30eb\u306f\u30b9\u30d1\u30e0\u3068\u5206\u985e\u3055\u308c\u307e\u3059\u3002<br>\n\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u306a\u3089\u3001\u3055\u3089\u306b\u305d\u306e\u30e1\u30fc\u30eb\u306f\u9023\u7d61\u5148\u306b\u5165\u3063\u3066\u3044\u308b\u4eba\u304b\u3089\u6765\u305f\u304b\u3068\u3044\u3046\u8cea\u554f\u3092\u3057\u307e\u3059\u3002<br>\n\u305d\u308c\u304c\u3001Yes\u306a\u3089\u3070\u30b9\u30d1\u30e0\u30e1\u30fc\u30eb\u3067\u306f\u306a\u3044\u3001No\u306a\u3089\u30b9\u30d1\u30e0\u30e1\u30fc\u30eb\u3068\u5206\u985e\u3055\u308c\u307e\u3059\u3002<br>\n\u3053\u306e\u3088\u3046\u306b\u6c7a\u5b9a\u6728\u306f\u4eba\u304c\u7406\u89e3\u3059\u308b\u306e\u306b\u5bb9\u6613\u3067\u3001\u4f55\u304c\u305d\u306e\u5206\u985e\u3092\u6c7a\u5b9a\u3065\u3051\u305f\u304b\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2 id=\"i-2\">\u8a13\u7df4\u30c7\u30fc\u30bf\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<\/h2>\n<p>\u6c7a\u5b9a\u6728\u306e\u5b9f\u88c5\u4f8b\u3092\u30bf\u30a4\u30bf\u30cb\u30c3\u30af\u53f7\u306e\u751f\u6b7b\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u793a\u3057\u307e\u3059\u3002<br>\n\u4eca\u56de\u306f\u3001\u30bf\u30a4\u30bf\u30cb\u30c3\u30af\u3067\u751f\u304d\u5ef6\u3073\u305f\u304b\u751f\u304d\u5ef6\u3073\u3089\u308c\u306a\u304b\u3063\u305f\u304b\u3092\u5206\u985e\u3057\u307e\u3059\u3002<br>\n<a href=\"http:\/\/biostat.mc.vanderbilt.edu\/wiki\/pub\/Main\/DataSets\/titanic.txt\" rel=\"nofollow noopener\" target=\"_blank\">\u30c7\u30fc\u30bf\u30da\u30fc\u30b8<\/a><\/p>\n<p>1.\u4e0a\u8a18\u306e\u30b5\u30a4\u30c8\u306b\u30a2\u30af\u30bb\u30b9<br>\n2.\u53f3\u30af\u30ea\u30c3\u30af\u3092\u62bc\u3057\u3066\u300c\u540d\u524d\u3092\u3064\u3051\u3066\u4fdd\u5b58\u300d<br>\n\u203b\u53f3\u30af\u30ea\u30c3\u30af\u51fa\u6765\u306a\u3044\u5834\u5408\uff08\u3082\u3057\u304f\u306f\u300c\u540d\u524d\u3092\u3064\u3051\u3066\u4fdd\u5b58\u300d\u304c\u51fa\u3066\u3053\u306a\u3044\u5834\u5408\uff09\u306f\u3001titanic.txt\u306e\u5185\u5bb9\u3092\u5168\u3066\u9078\u629e\u3057\u3001\u30b3\u30d4\u30fc\u3057\u3001\u30c6\u30ad\u30b9\u30c8\u30a8\u30c7\u30a3\u30bf\u3084\u30e1\u30e2\u5e33\u7b49\u306b\u30da\u30fc\u30b9\u30c8\uff08\u8cbc\u308a\u4ed8\u3051\uff09\u3057\u3001\u300ctitanic.csv\u300d\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4fdd\u5b58\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\n3.\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u5185\u3067\u3001\u300ctitanic.csv\u300d\u3068\u3057\u3066\u4fdd\u5b58<br>\n\u4ee5\u4e0a\u3067\u5b8c\u4e86\u3067\u3059\uff01<\/p>\n<h2 id=\"i-3\">Google Colab\u3067CSV\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>Gogole Colab\u3067\u5b9f\u884c\u3059\u308b\u5834\u5408\u3001\u5148\u307b\u3069.csv\u62e1\u5f35\u5b50\u3067\u4fdd\u5b58\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\n\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u304c\u51fa\u6765\u307e\u3059\u3002<br>\n\u4e3b\u306b3\u3064\u306e\u6d41\u308c\u3067\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u2460 Colab\u3092\u958b\u304d\u3001\u5de6\u5074\u306e &gt;\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b<br>\n\u2461\u5de6\u30e1\u30cb\u30e5\u30fc\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30af\u30ea\u30c3\u30af\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b<br>\n\u2462\u300e\u2191\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u300f\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001csv\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u629e\u3057\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b<\/p>\n<p><img decoding=\"async\" src=\"\/assets\/images_test\/35_16d04ec7617.png\" alt=\"\"><\/p>\n<p><img decoding=\"async\" src=\"\/assets\/images_test\/35_16d04ec9828.png\" alt=\"\"><\/p>\n<p><img decoding=\"async\" src=\"\/assets\/images_test\/35_16d04ecba98.png\" alt=\"\"><\/p>\n<h2 id=\"i-4\">CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c<\/h2>\n<p>\u3067\u306f\u3001\u5148\u307b\u3069\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305ftitanic.csv\u30d5\u30a1\u30a4\u30eb\u3092Python\u304b\u3089\u8aad\u307f\u8fbc\u3093\u3067\u898b\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>import csv\nimport numpy as np\nwith open('.\/titanic.csv','r') as csvfile:\n    titanic_reader = csv.reader(csvfile,delimiter=',',quotechar='\"')\n\n    #\u7279\u5fb4\u91cf\u306e\u540d\u524d\u304c\u66f8\u304b\u308c\u305fHeader\u3092\u8aad\u307f\u53d6\u308b\n    row = next(titanic_reader)\n    feature_names = np.array(row)\n\n    #\u30c7\u30fc\u30bf\u3068\u6b63\u89e3\u30e9\u30d9\u30eb\u3092\u8aad\u307f\u53d6\u308b\n    titanic_x, titanic_y = [],[]\n    for row in titanic_reader:\n        titanic_x.append(row)\n        titanic_y.append(row[2]) #\u6b63\u89e3\u30e9\u30d9\u30eb\u306f3\u5217\u76ee\u306e\"survived\"\n\n    titanic_x = np.array(titanic_x) #\u578b\u3092\u30ea\u30b9\u30c8\u304b\u3089numpy.ndarray\u306b\u3059\u308b\n    titanic_y = np.array(titanic_y) #\u578b\u3092\u30ea\u30b9\u30c8\u304b\u3089numpy.ndarray\u306b\u3059\u308b\n\nprint(feature_names)\nprint(titanic_x[0],titanic_y[0])\n<\/code><\/pre>\n<p>\u4e0a\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306b\u7279\u5fb4\u91cf\u30681\u756a\u76ee\u306edata\u306e\u4e2d\u8eab\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell\">['row.names' 'pclass' 'survived' 'name' 'age' 'embarked' 'home.dest' 'room'\n'ticket' 'boat' 'sex']\n['1' '1st' '1' 'Allen, Miss Elisabeth Walton' '29.0000' 'Southampton'\n'St Louis, MO' 'B-5' '24160 L221' '2' 'female'] 1\n<\/code><\/pre>\n<h2 id=\"i-5\">\u524d\u51e6\u7406 \/ \u7279\u5fb4\u91cf\u9078\u629e<\/h2>\n<p>\u6700\u521d\u306b\u3001\u4eca\u56de\u4f7f\u3046\u7279\u5fb4\u91cf\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br>\n\u307e\u305a\u3001\u524d\u51e6\u7406\u306e\u8aac\u660e\u3067\u3059\u3002<br>\n\u524d\u51e6\u7406\u3068\u306f\u3001\u751f\u30c7\u30fc\u30bf\u304b\u3089\u305d\u306e\u7279\u5fb4\u3092\u6570\u5024\u3067\u8868\u3057\u305f\u300c\u7279\u5fb4\u91cf\u300d\u306b\u3059\u308b\u3053\u3068\u3067\u3059\u3002<br>\n\u6a5f\u68b0\u5b66\u7fd2\u306e\u89b3\u70b9\u3067\u306f\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u5f79\u306b\u7acb\u305f\u305a\u3001\u610f\u5473\u306e\u3042\u308b\u6570\u5024\u306b\u5909\u63db\u3067\u304d\u3066\u521d\u3081\u3066\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u7b49\u306e\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306b\u5165\u529b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\n\u305d\u3057\u3066\u3001\u7279\u5fb4\u91cf\u3068\u306f\u3001<em>\u5165\u529b\u306e\u4f7f\u7528\u3059\u308b\u30c7\u30fc\u30bf\u306e\u3053\u3068<\/em>\u3067\u3042\u308a\u56de\u5e30\u306e\u7ae0\u3067\u306f\u8aac\u660e\u5909\u6570\u3068\u547c\u3093\u3067\u3044\u307e\u3057\u305f\u3002<br>\n\u4eca\u56de\u306f\u3001\u7279\u5fb4\u91cf\u3068\u3057\u3066\u300c\u30af\u30e9\u30b9\u300d\u300c\u5e74\u9f62\u300d\u300c\u6027\u5225\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre><code># class(1),age(4),sex(10)\u3092\u6b8b\u3059\ntitanic_x = titanic_x[:,[1, 4, 10]]\nfeature_names = feature_names[[1, 4, 10]]\n\nprint(feature_names)\nprint(titanic_x[12],titanic_y[12])\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u5185\u5bb9\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell\">['pclass' 'age' 'sex']\n['1st' 'NA' 'female'] 1\n<\/code><\/pre>\n<h2 id=\"i-6\">\u6b20\u640d\u5024\u306e\u88dc\u5b8c<\/h2>\n<p>\u51fa\u529b\u3057\u305f13\u756a\u76ee\u306e\u30c7\u30fc\u30bf\u3092\u898b\u308b\u3068\u3001age\u304c\u2019NA\u2019\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br>\n\u3053\u308c\u306f\u30c7\u30fc\u30bf\u304cNot Available\u3001\u3059\u306a\u308f\u3061\u5b58\u5728\u3057\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u8868\u3057\u3066\u304a\u308a<strong>\u6b20\u640d\u5024<\/strong>\u3068\u547c\u3070\u308c\u307e\u3059\u3002<br>\n<em>\u6b20\u640d\u5024\u304c\u3042\u308b\u3068\u5b66\u7fd2\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u4eca\u56de\u306f\u6b20\u640d\u5024\u3092\u5e74\u9f62\u306e\u5e73\u5747\u5024\u306b\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002<\/em><br>\n\uff08\u6b20\u640d\u5024\u3092\u57cb\u3081\u308b\u305f\u3081\u306b\u6700\u983b\u5024\u3084\u4e2d\u592e\u5024\u3092\u7528\u3044\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\uff09<\/p>\n<pre><code># \u5e74\u9f62\u306e\u6b20\u640d\u5024\u3092\u5e73\u5747\u5024\u3067\u57cb\u3081\u308b\nages = titanic_x[:,1]\n#NA\u4ee5\u5916\u306eage\u306e\u5e73\u5747\u5024\u3092\u8a08\u7b97\u3059\u308b\nmean_age = np.mean(titanic_x[ages != 'NA',1].astype(float))\n#age\u304cNA\u306e\u3082\u306e\u3092\u5e73\u5747\u5024\u306b\u7f6e\u304d\u63db\u3048\u308b\ntitanic_x[titanic_x[:, 1] == 'NA',1] = mean_age\n<\/code><\/pre>\n<h2 id=\"i-7\">LabelEncoder<\/h2>\n<p>\u307e\u305f\u3001\u51fa\u529b\u3057\u305f13\u756a\u76ee\u306e\u30c7\u30fc\u30bf\u306esex\u306f\u2019female\u2019\u3068\u6587\u5b57\u5217\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br>\n\u6587\u5b57\u5217\u306e\u30c7\u30fc\u30bf\u306e\u3053\u3068\u3092\u30ab\u30c6\u30b4\u30ea\u30ab\u30eb\u30c7\u30fc\u30bf\u3068\u3044\u3044\u307e\u3059\u3002<br>\n\u5b66\u7fd2\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u6587\u5b57\u5217\u3092\u6570\u5024\u306b\u4fee\u6b63\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\n\u305d\u3053\u3067\u3001LabelEncoder()\u3092\u4f7f\u3063\u3066\u6570\u5024\u306b\u76f4\u3057\u307e\u3059\u3002<br>\n\u6570\u5024\u306b\u76f4\u3059\u3068\u3044\u3046\u306e\u306f\u3001<br>\n\u2018female\u2019 \u2192 0<br>\n\u2018male\u2019 \u2192 1<br>\n\u306b\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<pre><code>from sklearn.preprocessing import LabelEncoder\nenc = LabelEncoder()\nlabel_encoder = enc.fit(titanic_x[:, 2])\nprint('Categorical classes:',label_encoder.classes_)\n\ninteger_classes = label_encoder.transform(label_encoder.classes_)\nprint('Integer classes:',integer_classes)\n\nt = label_encoder.transform(titanic_x[:, 2])\ntitanic_x[:,2] = t\n\nprint(feature_names)\nprint(titanic_x[12],titanic_y[12])\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u51fa\u529b\u3055\u308c\u2019female\u2019\u304c0\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell\">['pclass' 'age' 'sex']\n['1st' '31.19418104265403' '0'] 1\n<\/code><\/pre>\n<h2 id=\"i-8\">OneHotEncoder<\/h2>\n<p>\u5148\u7a0b\u306f\u3001\u2019sex\u2019\u30920,1\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002<br>\n\u3057\u304b\u3057\u3001pclass\u3082\u30ab\u30c6\u30b4\u30ea\u30ab\u30eb\u30c7\u30fc\u30bf\u306a\u306e\u3067\u6570\u5024\u306b\u4fee\u6b63\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\n\u305f\u3060\u3057\u3001age\u3068\u540c\u69d8\u306e\u624b\u6cd5\u3067<br>\n1st \u2192\u30000<br>\n2nd \u2192\u30001<br>\n3rd \u2192\u30002<br>\n\u3068\u3057\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002<br>\n\u306a\u305c\u306a\u3089\u3001\u30ab\u30c6\u30b4\u30ea\u30ab\u30eb\u30c7\u30fc\u30bf\u3067\u306f\u9806\u5e8f\u6027\u304c\u306a\u304f1st &lt; 2nd &lt; 3rd\u3068\u3044\u3046\u95a2\u4fc2\u304c\u6210\u308a\u7acb\u305f\u306a\u3044\u304b\u3089\u3067\u3059\u3002<br>\n\u3053\u308c\u3092\u3001\u6570\u5024\u306e1, 2, 3\u3068\u3057\u3066\u3057\u307e\u3046\u30681 &lt; 2 &lt; 3\u3068\u3044\u3046\u95a2\u4fc2\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u304a\u304b\u3057\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br>\n\u305d\u3053\u3067\u3001OneHotEncoder\u3068\u3044\u3046\u624b\u6cd5\u3092\u7528\u3044\u307e\u3059\u3002<br>\n\u3053\u308c\u306f\u3001\u305d\u308c\u305e\u308c\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u5f53\u3066\u306f\u307e\u308c\u3070\u300c\uff11\u300d\u5f53\u3066\u306f\u307e\u3089\u306a\u3051\u308c\u3070\u300c\uff10\u300d\u3068\u3059\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n<p><em>\uff0a\uff08\u6ce8\u610f\uff09\u4eca\u56de\u3001pclass\uff08\u968e\u7d1a\uff09\u306b\u5bfe\u3057\u3066\u3001OneEncoder\u3092\u7528\u3044\u307e\u3059\u304c\u3001<br>\n\u968e\u7d1a\u306f\u3001\u9806\u5e8f\u3042\u308a\u30c7\u30fc\u30bf\uff08\u9806\u5e8f\u5c3a\u5ea6\uff09\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u4eca\u56de\u306e\u76ee\u7684\u306f\u3001\u30bf\u30a4\u30bf\u30cb\u30c3\u30af\u53f7\u306e\u30c7\u30fc\u30bf\u304b\u3089\u3001\u6b7b\u3093\u3060\u304b\u751f\u304d\u5ef6\u3073\u305f\u304b\u3092\u4e88\u6e2c\u3059\u308b\u30e2\u30c7\u30eb\u3092\u4f5c\u308b\u4e0a\u3067\u8003\u3048\u308b\u3068\u9806\u5e8f\u306f\u95a2\u4fc2\u306a\u304f\u306a\u308a\u307e\u3059\u3002<br>\n\u4f8b\u3048\u3070\u3001\u5e74\u53ce\u304c1000\u4e07\u4ee5\u4e0a\u304b\u3069\u3046\u304b\u5206\u985e\u3059\u308b\u30e2\u30c7\u30eb\u306e\u5834\u5408\u3001pclass(\u968e\u7d1a)\u306f\u9806\u5e8f\u3042\u308a\u30c7\u30fc\u30bf\u306eLabelEncoder\u306b\u3057\u307e\u3059\u304c\u3001\u751f\u304d\u308b\u304b\u6b7b\u306c\u304b\u306e\u5206\u985e\u30e2\u30c7\u30eb\u306e\u5834\u5408\u3067\u3059\u306e\u3067\u3001pclass\u306f\u9806\u5e8f\u95a2\u4fc2\u306a\u3044\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002<br>\n\u5b9f\u969b\u3001LabelEncoder\u3067\u3082\u53ef\u80fd\u3067\u3059\u304c\u3001\u30e2\u30c7\u30eb\u306e\u6027\u80fd\u3092\u8003\u3048\u305f\u3068\u304d\u306b\u3001\u6700\u9069\u306a\u524d\u51e6\u7406\u306f\u3001OneHotEncoder\u3092\u9078\u629e\u3057\u3066\u3044\u307e\u3059\u3002<\/em><\/p>\n<p>\u305f\u3068\u3048\u3070\u3001A\u301cD\u541b\u306e\u597d\u304d\u306a\u679c\u7269\u304c\u4e0b\u306e\u8868\u3067\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4eba\u7269<\/th>\n<th>\u597d\u304d\u306a\u679c\u7269<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>A<\/td>\n<td>\u308a\u3093\u3054<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>\u3070\u306a\u306a<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>\u3076\u3069\u3046<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>\u308a\u3093\u3054<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u3053\u308c\u306b\u5bfe\u3057\u3001OneHotEncoder\u3092\u884c\u3046\u3068<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4eba\u7269<\/th>\n<th>\u308a\u3093\u3054<\/th>\n<th>\u3070\u306a\u306a<\/th>\n<th>\u3076\u3069\u3046<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>A<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u3068\u306a\u308a\u81ea\u5206\u304c\u5f53\u3066\u306f\u307e\u308b\u9805\u76ee\u306f1\u3001\u5f53\u3066\u306f\u307e\u3089\u306a\u3044\u9805\u76ee\u306f0\u3068\u306a\u308a\u307e\u3059\u3002<br>\n\u3067\u306f\u3001pclass\u306b\u5bfe\u3057OneHotEncoder\u3092\u884c\u306a\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>from sklearn.preprocessing import OneHotEncoder\nenc = LabelEncoder()\nlabel_encoder = enc.fit(titanic_x [:, 0])\nprint(\"Categorical classes:\", label_encoder.classes_)\ninteger_classes = label_encoder.transform(label_encoder.classes_).reshape(3, 1)\nprint(\"Integer classes:\", integer_classes)\nenc = OneHotEncoder()\none_hot_encoder = enc.fit(integer_classes)\n\n#\u6700\u521d\u306b\u3001Label Encoder\u3092\u4f7f\u3063\u3066pclass\u30920-2\u306b\u76f4\u3059\nnum_of_rows = titanic_x.shape[0]\nt = label_encoder.transform(titanic_x[:, 0]).reshape(num_of_rows, 1)\n#\u6b21\u306b\u3001OneHotEncoder\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30921, 0\u306b\u5909\u63db\nnew_features = one_hot_encoder.transform(t)\n#1,0\u306b\u306a\u304a\u3057\u3066\u30c7\u30fc\u30bf\u3092\u7d71\u5408\u3059\u308b\ntitanic_x = np.concatenate([titanic_x, new_features.toarray()], axis = 1)\n#OnehotEncoder\u3092\u3059\u308b\u524d\u306epclass\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\ntitanic_x = np.delete(titanic_x, [0], 1)\n#\u7279\u5fb4\u91cf\u306e\u540d\u524d\u3092\u66f4\u65b0\u3059\u308b\nfeature_names = ['age', 'sex', 'first class', 'second class', 'third class']\n\n# Convert to numerical values\ntitanic_x = titanic_x.astype (float)\ntitanic_y = titanic_y.astype (float)\n\nprint(feature_names)\nprint(titanic_x[0],titanic_y[0])\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u30c7\u30fc\u30bf\u304c\u5168\u3066\u6570\u5024\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<br>\n\u3053\u308c\u3067\u5b66\u7fd2\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre><code class=\"shell\">['age', 'sex', 'first class', 'second class', 'third class']\n[ 29.   0.   1.   0.   0.] 1.0\n<\/code><\/pre>\n<h2 id=\"i-9\">\u5b66\u7fd2<\/h2>\n<p>\u307e\u305a\u306f\u3001\u30c7\u30fc\u30bf\u3092\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u3051\u307e\u3059\u3002<br>\n\u305d\u306e\u5f8c\u3001<strong>scikit-learn\u306eDecision Tree Classifier\u3092\u4f7f\u3063\u3066\u5b66\u7fd2\u3057\u307e\u3059\u3002<\/strong><\/p>\n<pre><code># sklearn 0.20\u304b\u3089\u4e0b\u8a18\u306f\u5ec3\u6b62\u3055\u308c\u307e\u3059\n# from sklearn.cross_validation import train_test_split\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(titanic_x,titanic_y, test_size=0.25, random_state=33)\n# \u6b21\u306bscikit-learn\u306eDecision Tree Classifier\u3092\u4f7f\u3063\u3066\u5b66\u7fd2\u3057\u307e\u3059\u3002\n\nfrom sklearn import tree\nclf = tree.DecisionTreeClassifier(criterion='entropy', max_depth= 3, min_samples_leaf = 5)\nclf = clf.fit(x_train, y_train)\n# \u4eca\u56de\u306f\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066criterion, max_depth, min_samples_leaf\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\n\"\"\"\ncriterion\u30fb\u30fb\u30fb\u3000\u5206\u985e\u57fa\u6e96\uff08entropy\u3068gini\u304c\u3042\u308b\uff09\nmax_depth\u30fb\u30fb\u30fb\u3000\u6728\u306e\u6df1\u3055\nmin_samples_split\u30fb\u30fb\u30fb\u3000\u5206\u5272\u3059\u308b\u3068\u304d\u306b\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u6570\n\"\"\"\n<\/code><\/pre>\n<p>\u3053\u308c\u4ee5\u5916\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u3068\u304d\u3001scikit-learn\u306eHP(<a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.tree.DecisionTreeClassifier.html\" rel=\"nofollow noopener\" target=\"_blank\">DecisonTreeClassfier<\/a>)\u3092\u53c2\u8003\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u3053\u308c\u3067\u5b66\u7fd2\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<h2 id=\"i-10\">\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u53ef\u8996\u5316\u3059\u308b<\/h2>\n<p>\u3067\u306f\u3001\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u53ef\u8996\u5316\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u6c7a\u5b9a\u6728\u3092\u53ef\u8996\u5316\u3059\u308b\u306b\u306f\u3001<strong>Graphviz<\/strong>\u3068<strong>pydotplus<\/strong>\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<br>\n\u30bf\u30fc\u30df\u30ca\u30eb\uff08\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\uff09\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"shell\">pip install Graphviz\npip install pydotplus\n<\/code><\/pre>\n<p>\u203b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u3053\u3061\u3089\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\n\u307e\u305f<strong>InvocationException: GraphViz\u2019s executables not found<\/strong>\u3068\u3044\u3046\u30a8\u30e9\u30fc\u3067\u53ef\u8996\u5316\u304c\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code class=\"shell\">brew install graphviz\npip install -U pydotplus\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.graphviz.org\/Download..php\" rel=\"nofollow noopener\" target=\"_blank\">\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30b5\u30a4\u30c81<\/a><br>\n<a href=\"https:\/\/pypi.python.org\/pypi\/pydotplus\" rel=\"nofollow noopener\" target=\"_blank\">\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30b5\u30a4\u30c82<\/a><br>\n<em>\u203b\u4e0a\u8a18\u306e\u65b9\u6cd5\u304b\u3064Windows\u3067\u51fa\u6765\u306a\u3044\u5834\u5408\u306f\u3001\u30cd\u30c3\u30c8\u306e\u60c5\u5831\u3092\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/em><br>\n\u305d\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5165\u529b\u3059\u308b\u3068\u6728\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code>import pydotplus\nfrom sklearn.externals.six import StringIO\ndot_data = StringIO()\ntree.export_graphviz(clf, out_file=dot_data,feature_names = ['age','Sex','1st_c1ass','2nd_class','3rd_class'])\ngraph = pydotplus.graph_from_dot_data(dot_data.getvalue())\ngraph.write_pdf(\"tree.pdf\")\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/camo.qiitausercontent.com\/8b4e3c1288517211a786cbc03c5189a831e287c8\/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3130353838372f62653331383438622d666532632d376161362d633562652d6564636630313937333663312e706e67\" alt=\"\"><\/p>\n<p>\u3053\u306e\u6728\u306f\u3001\u5b66\u7fd2\u30c7\u30fc\u30bf\u306b\u3088\u308b\u5b66\u7fd2\u7d50\u679c\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<br>\n\u8cea\u554f\u3092\u7b54\u3048\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u5206\u985e\u304c\u3055\u308c\u307e\u3059\u3002<br>\n\u4f8b\u3048\u3070\u3001\u4e00\u756a\u4e0a\u306e\u90e8\u5206\u3067\u306f<br>\nsex &lt;= 0.5\u304b\uff1f\u3068\u3044\u3046\u3053\u3068\u3092\u805e\u3044\u3066\u3044\u307e\u3059\u3002<br>\n\uff08sex\u306f\u2019woman\u2019\u304b\uff1f\uff09<br>\n\u3053\u306e\u3068\u304d\u3001\u7b54\u3048\u304cyes\u306a\u3089\u5de6\u3078\u3001No\u306a\u3089\u53f3\u3078\u9032\u307f\u307e\u3059\u3002<br>\n\u9032\u3093\u3060\u5148\u306e\u8cea\u554f\u306b\u3082\u7b54\u3048\u3001\u540c\u69d8\u306byes\u306a\u3089\u3001\u5de6\u3078No\u306a\u3089\u53f3\u306b\u884c\u304d\u307e\u3059\u3002<br>\n\u3053\u308c\u3092\u6700\u4e0b\u6bb5\u307e\u3067\u7e70\u308a\u8fd4\u3057\u3001\u6700\u4e0b\u6bb5\u3068\u591a\u6570\u306e\u307b\u3046\u306e\u6b63\u89e3\u30e9\u30d9\u30eb\u304c\u3075\u3089\u308c\u307e\u3059\u3002<br>\nsex &lt;= 0.5 \uff08sex\u306f\u2019woman\u2019\u304b\uff09\u3000\u2192\u3000yes<br>\n3rd_class &lt;= 0.5 (3rd_class\u3067\u306f\u306a\u3044\uff09\u3000\u2192\u3000yes<br>\n1st_class &lt;= 0.5 (1st_class\u3067\u306f\u306a\u3044\uff09\u3000\u2192\u3000yes<br>\n\u3068\u9032\u3093\u3060\u3068\u304d\u3001\u6700\u5f8c\u306fValue = [8, 70]\u3068\u306a\u3063\u3066\u304a\u308a\u3001\u751f\u304d\u5ef6\u3073\u305f\u4eba\u306e\u307b\u3046\u304c\u591a\u3044\u3067\u3059\u3002<br>\n\u306a\u306e\u3067\u3001\u6027\u5225\u304c\u5973\u6027\u3067\u30012nd class\u306b\u3044\u305f\u4eba\u306f\u751f\u304d\u5ef6\u3073\u305f\u3068\u5206\u985e\u3055\u308c\u307e\u3059\u3002<br>\n\u3067\u306f\u3001first class\u306b\u3044\u305f10\u624d\u306e\u5973\u306e\u5b50\u306f\u3069\u3061\u3089\u306b\u5206\u985e\u3055\u308c\u308b\u304b\u81ea\u5206\u3067\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u7b54\u3048\u306f\u3001\u201d\u751f\u304d\u5ef6\u3073\u305f\u201d\u3067\u3059\u3002<br>\n\u3067\u306f\u3001\u6700\u5f8c\u306b\u7cbe\u5ea6\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u4e0b\u8a18\u4e00\u884c\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code># \u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306e\u8a55\u4fa1\nmeasure_performance(x_train, y_train, clf)\n<\/code><\/pre>\n<p>\u8ffd\u52a0\u5f8c\u306b\u5b9f\u884c\u3059\u308b\u3068\u6b21\u306e\u3088\u3046\u306a\u51fa\u529b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell\">Accuracy:0.838\nClassification report\nprecision    recall  f1-score   support\n0.0       0.82      0.98      0.89       662\n1.0       0.93      0.55      0.69       322\navg \/ total       0.85      0.84      0.82       984\nConfussion matrix\n[[649  13]\n[146 176]]\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u78ba\u8a8d\u3059\u308b\u3068\u3001\u306a\u304b\u306a\u304b\u306b\u826f\u3044\u5206\u985e\u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2 id=\"i-11\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/h2>\n<pre><code class=\"python:\">import csv\nimport numpy as np\n\nwith open('.\/titanic.csv','r') as csvfile:\n    titanic_reader = csv.reader(csvfile,delimiter=',',quotechar='\"')\n\n    #\u7279\u5fb4\u91cf\u306e\u540d\u524d\u304c\u66f8\u304b\u308c\u305fHeader\u3092\u8aad\u307f\u53d6\u308b\n    row = next(titanic_reader)\n    feature_names = np.array(row)\n\n    #\u30c7\u30fc\u30bf\u3068\u6b63\u89e3\u30e9\u30d9\u30eb\u3092\u8aad\u307f\u53d6\u308b\n    titanic_x, titanic_y = [],[]\n    for row in titanic_reader:\n        titanic_x.append(row)\n        titanic_y.append(row[2]) #\u6b63\u89e3\u30e9\u30d9\u30eb\u306f3\u5217\u76ee\u306e\"survived\"\n\n    titanic_x = np.array(titanic_x) #\u578b\u3092\u30ea\u30b9\u30c8\u304b\u3089numpy.ndarray\u306b\u3059\u308b\n    titanic_y = np.array(titanic_y) #\u578b\u3092\u30ea\u30b9\u30c8\u304b\u3089numpy.ndarray\u306b\u3059\u308b\n\nprint(feature_names)\nprint(titanic_x[0],titanic_y[0])\n\n\n# class(1),age(4),sex(10)\u3092\u6b8b\u3059\ntitanic_x = titanic_x[:,[1, 4, 10]]\nfeature_names = feature_names[[1, 4, 10]]\n\nprint(feature_names)\nprint(titanic_x[12],titanic_y[12])\n\n\n# \u5e74\u9f62\u306e\u6b20\u640d\u5024\u3092\u5e73\u5747\u5024\u3067\u57cb\u3081\u308b\nages = titanic_x[:,1]\n# NA\u4ee5\u5916\u306eage\u306e\u5e73\u5747\u5024\u3092\u8a08\u7b97\u3059\u308b\nmean_age = np.mean(titanic_x[ages != 'NA',1].astype(float))\n# age\u304cNA\u306e\u3082\u306e\u3092\u5e73\u5747\u5024\u306b\u7f6e\u304d\u63db\u3048\u308b\ntitanic_x[titanic_x[:, 1] == 'NA',1] =mean_age\n\n\nfrom sklearn.preprocessing import LabelEncoder\nenc = LabelEncoder()\nlabel_encoder = enc.fit(titanic_x[:, 2])\nprint('Cateorical classes:',label_encoder.classes_)\n\ninteger_classes = label_encoder.transform(label_encoder.classes_)\nprint('Integer classes:',integer_classes)\n\nt = label_encoder.transform(titanic_x[:, 2])\ntitanic_x[:,2] = t\n\nprint(feature_names)\nprint(titanic_x[12],titanic_y[12])\n\n\nfrom sklearn.preprocessing import OneHotEncoder\nenc = LabelEncoder()\nlabel_encoder = enc.fit(titanic_x [:, 0])\nprint(\"Categorical classes:\", label_encoder.classes_)\ninteger_classes = label_encoder.transform(label_encoder.classes_).reshape(3, 1)\nprint(\"Integer classes:\", integer_classes)\nenc = OneHotEncoder()\none_hot_encoder = enc.fit(integer_classes)\n\n# \u6700\u521d\u306b\u3001Label Encoder\u3092\u4f7f\u3063\u3066pclass\u30920-2\u306b\u76f4\u3059\nnum_of_rows = titanic_x.shape[0]\nt = label_encoder.transform(titanic_x[:, 0]).reshape(num_of_rows, 1)\n# \u6b21\u306b\u3001OneHotEncoder\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30921, 0\u306b\u5909\u63db\nnew_features = one_hot_encoder.transform(t)\n# 1,0\u306b\u306a\u304a\u3057\u3066\u30c7\u30fc\u30bf\u3092\u7d71\u5408\u3059\u308b\ntitanic_x = np.concatenate([titanic_x, new_features.toarray()], axis = 1)\n# OnehotEncoder\u3092\u3059\u308b\u524d\u306epclass\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\ntitanic_x = np.delete(titanic_x, [0], 1)\n# \u7279\u5fb4\u91cf\u306e\u540d\u524d\u3092\u66f4\u65b0\u3059\u308b\nfeature_names = ['age', 'sex', 'first class', 'second class', 'third class']\n\n# Convert to numerical values\ntitanic_x = titanic_x.astype (float)\ntitanic_y = titanic_y.astype (float)\n\nprint(feature_names)\nprint(titanic_x[0],titanic_y[0])\n\n# sklearn 0.20\u304b\u3089\u4e0b\u8a18\u306f\u5ec3\u6b62\u3055\u308c\u307e\u3059\u3002\n# from sklearn.cross_validation import train_test_split\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(titanic_x, titanic_y, test_size=0.25, random_state=33)\n\n\nfrom sklearn import metrics\ndef measure_performance(x,y,clf, show_accuracy=True,show_classification_report=True, show_confussion_matrix=True):\n    y_pred=clf.predict(x)\n    if show_accuracy:\n        print(\"Accuracy:{0:.3f}\".format(metrics.accuracy_score(y, y_pred)), \"\\n\")\n\n    if show_classification_report:\n        print(\"Classification report\")\n        print(metrics.classification_report(y, y_pred), \"\\n\")\n\n    if show_confussion_matrix:\n        print(\"Confussion matrix\")\n        print(metrics.confusion_matrix(y, y_pred),\"\\n\")\n\nfrom sklearn import tree\nclf = tree.DecisionTreeClassifier(criterion='entropy', max_depth= 3, min_samples_leaf = 5)\nclf = clf.fit(x_train, y_train)\n\nimport pydotplus\nfrom sklearn.externals.six import StringIO\ndot_data = StringIO()\ntree.export_graphviz(clf, out_file=dot_data,feature_names = ['age','Sex','1st_c1ass','2nd_class','3rd_class'])\ngraph = pydotplus.graph_from_dot_data(dot_data.getvalue())\ngraph.write_pdf(\"tree.pdf\")\n\n# \u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306e\u8a55\u4fa1\n\"\"\"\n\u3010\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306e\u8a55\u4fa1\u306b\u95a2\u3057\u3066\u306e\u88dc\u8db3\u3011\n\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306e\u8a55\u4fa1\u306b\u95a2\u3057\u3066measure_performance(x_train, y_train, clf)\n\u3068\u3042\u308a\u307e\u3059\u304c\u3001test\u306b\u5bfe\u3057\u3066\u3067\u306f\u306a\u304ftrain\u3067\u884c\u3063\u3066\u3044\u307e\u3059\u3002\n\u4e00\u822c\u7684\u306b\u6c4e\u5316\u6027\u80fd\u306f\u5b66\u7fd2\u306b\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u3068\u306f\u72ec\u7acb\u306a\u3082\u306e\u3067\u56f3\u308b\u3079\u304d\u3067\u3059\u304c\u3001\u3057\u3063\u304b\u308a\u3068\u5b66\u7fd2\u3067\u304d\u305f\u304b\u3001\u3068\u3044\u3046\u3053\u3068\u3092\u898b\u308b\u306e\u306btrain\u3067\u306e\u6027\u80fd\u3082\u51fa\u3059\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\ntrain\u3060\u3051\u3067\u6027\u80fd\u306e\u8a55\u4fa1\u306f\u3057\u307e\u305b\u3093\u3002\ntest\u3060\u3051\u306e\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u304c\u3001train\u3067\u3061\u3083\u3093\u3068\u5b66\u7fd2\u3067\u304d\u3066\u3044\u308b(\u3053\u308c\u306f\u6c4e\u5316\u6027\u80fd\u3067\u306f\u306a\u304f\u3001\u5c11\u306a\u304f\u3068\u3082train\u30c7\u30fc\u30bf\u306b\u306ffit\u3067\u304d\u3066\u3044\u308b)\u3068\u3044\u3046\u3053\u3068\u3092\u78ba\u8a8d\u3057\u305f\u4e0a\u3067\u3001test\u3067\u6c4e\u5316\u6027\u80fd\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\n\u306a\u304a\u6b63\u78ba\u306b\u306f\u4ea4\u5dee\u5224\u5b9a\u3084AIC, BIC, WAIC,SBIC\u7b49\u306e\u60c5\u5831\u91cf\u57fa\u6e96\u3067\u306e\u30e2\u30c7\u30eb\u306e\u6c4e\u5316\u6027\u80fd\u3092\u78ba\u8a8d\u3057\u307e\u3059\u304c\u3001DNN\u306e\u3088\u3046\u306b\u8a08\u7b97\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u30e2\u30c7\u30eb\u306e\u5834\u5408\u306f\u3001train, valid, test\u3068\u3044\u3046\u3075\u3046\u306b\u5143\u306e\u30c7\u30fc\u30bf\u3092\u5206\u5272\u3057\u3001valid\u3067\u306e\u6027\u80fd\u3092\u78ba\u8a8d\u3057\u306a\u304c\u3089train\u306b\u3088\u3063\u3066\u30e2\u30c7\u30eb\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u3092\u884c\u306a\u3063\u3066\u3044\u304d\u307e\u3059\u3002\n\n\"\"\"\n\nmeasure_performance(x_train, y_train, clf)\n\n\n<\/code><\/pre>\n<h2 id=\"i-12\">\u307e\u3068\u3081<\/h2>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u6c7a\u5b9a\u6728\u306b\u95a2\u3057\u3066\u5b66\u3073\u307e\u3057\u305f\u3002<br>\n\u6c7a\u5b9a\u6728\u306e\u6700\u5927\u306e\u5229\u70b9\u306f\u5206\u985e\u904e\u7a0b\u304c\u898b\u308c\u308b\u3053\u3068\u3067\u3059\u3002<br>\n\u30c7\u30fc\u30bf\u5206\u6790\u696d\u52d9\u3067\u3082\u7528\u3044\u308b\u6a5f\u4f1a\u304c\u591a\u3044\u305f\u3081\u3001\u3057\u3063\u304b\u308a\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h1 id=\"i-13\">Python\u3084\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\uff1f<\/h1>\n<p>Python\u3084\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\u3001\u666e\u6bb5\u304b\u3089Python\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u73fe\u5f79\u306e\u30c7\u30fc\u30bf\u30b5\u30a4\u30a8\u30f3\u30c6\u30a3\u30b9\u30c8\u3084\u6a5f\u68b0\u5b66\u7fd2\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u8cea\u554f\u3067\u304d\u308b\u74b0\u5883\u3067\u5b66\u3076\u3053\u3068\u3067\u3059\u3002<br>\n\u8cea\u554f\u3057\u653e\u984c\u304b\u3064\u3001\u4f53\u7cfb\u7684\u306b\u5b66\u3079\u308b\u52d5\u753b\u30b3\u30f3\u30c6\u30f3\u30c4\u3067\u30c7\u30fc\u30bf\u5206\u6790\u6280\u8853\u3092\u5b66\u3073\u305f\u3044\u65b9\u306f\u3001\u30aa\u30f3\u30e9\u30a4\u30f3\u3067\u597d\u304d\u306a\u6642\u9593\u306b\u52c9\u5f37\u3067\u304d\u308b<a href=\"https:\/\/aiacademy.jp\/bootcamp\">AI Academy Bootcamp<\/a>\u304c\u30aa\u30b9\u30b9\u30e1\u3067\u3059\u3002\u53d7\u8b1b\u6599\u3082\u696d\u754c\u6700\u5b89\u5024\u306e35,000\u5186\uff08\uff16\u30f6\u6708\u9593\u8cea\u554f\u3057\u653e\u984c\uff0b\u30aa\u30ea\u30b8\u30ca\u30eb\u306e\u52d5\u753b\u30b3\u30f3\u30c6\u30f3\u30c4\u3001\u30c6\u30ad\u30b9\u30c8\u30b3\u30f3\u30c6\u30f3\u30c4\u306e\u5229\u7528\u53ef\u80fd\uff09\u306a\u306e\u3067\u3001\u662f\u975e\u3054\u6d3b\u7528\u304f\u3060\u3055\u3044\u3002<\/p>\n<figure class=\"wp-block-image\"><a href=\"https:\/\/aiacademy.jp\/bootcamp\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-691\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-1024x341.png\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" srcset=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-1024x341.png 1024w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-300x100.png 300w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-768x256.png 768w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-940x313.png 940w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi.png 1200w\" alt=\"\" width=\"1024\" height=\"341\"><\/a><\/figure>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6b21 \u306f\u3058\u3081\u306b \u6c7a\u5b9a\u6728\u3068\u306f \u8a13\u7df4\u30c7\u30fc\u30bf\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9 Google Colab\u3067CSV\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5 CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c \u524d\u51e6\u7406 \/ \u7279\u5fb4\u91cf\u9078\u629e \u6b20\u640d\u5024\u306e\u88dc\u5b8c LabelEncode &#8230; <\/p>\n","protected":false},"author":1,"featured_media":487,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,105,110,130],"tags":[],"class_list":{"0":"post-250","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-105","9":"category-110","10":"category-130"},"_links":{"self":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/250","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=250"}],"version-history":[{"count":4,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":4877,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/4877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/media\/487"}],"wp:attachment":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}