{"id":252,"date":"2020-01-24T15:36:02","date_gmt":"2020-01-24T06:36:02","guid":{"rendered":"http:\/\/aiacademy.jp\/media\/?p=252"},"modified":"2024-08-08T14:40:44","modified_gmt":"2024-08-08T05:40:44","slug":"%e3%83%a9%e3%83%b3%e3%83%80%e3%83%a0%e3%83%95%e3%82%a9%e3%83%ac%e3%82%b9%e3%83%88%e3%81%a8%e3%81%af","status":"publish","type":"post","link":"https:\/\/aiacademy.jp\/media\/?p=252","title":{"rendered":"\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\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\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3068\u306f<\/a>      <\/li>      <li>        <a href=\"#i-1\">\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8<\/a>      <\/li>      <li>        <a href=\"#i-2\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u5206\u985e\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-3\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u56de\u5e30\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-4\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-5\">\u307e\u3068\u3081<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-6\">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\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3068\u306f<\/h2>\n<p><strong>\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3068\u306f\u6c7a\u5b9a\u6728\u3092\u62e1\u5f35\u3057\u305f\u3082\u306e\u3067\u3001\u5206\u985e\u3001\u56de\u5e30\u3001\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306b\u7528\u3044\u308b\u3053\u3068\u304c\u53ef\u80fd\u306a\u6a5f\u68b0\u5b66\u7fd2\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u3072\u3068\u3064<\/strong>\u3067\u3059\u3002<br>\n<strong><em>\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306f\u3001\u8907\u6570\u306e\u6c7a\u5b9a\u6728\u3067\u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u5b66\u7fd2\u3092\u884c\u3046\u624b\u6cd5<\/em><\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<br>\n<strong>\u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u5b66\u7fd2\u3068\u306f\u3001\u8907\u6570\u306e\u5b66\u7fd2\u5668\u3092\u7528\u3044\u3066\u5b66\u7fd2\u3092\u884c\u3046\u624b\u6cd5<\/strong>\u3067\u3059\u3002<br>\n\u8907\u6570\u306e\u5b66\u7fd2\u5668\u3067\u5b66\u7fd2\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u7cbe\u5ea6\u304c\u9ad8\u304f\u306a\u308b\u3068\u4e00\u822c\u7684\u306b\u8a00\u308f\u308c\u3066\u3044\u307e\u3059\u3002<br>\n\u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u5b66\u7fd2\u306b\u306f\u5927\u304d\u304f<strong>\u30d0\u30ae\u30f3\u30b0<\/strong>\u3001<strong>\u30d6\u30fc\u30b9\u30c6\u30a3\u30f3\u30b0<\/strong>\u3001<strong>\u30b9\u30bf\u30c3\u30ad\u30f3\u30b0<\/strong>\u306a\u3069\u3042\u308a\u307e\u3059\u3002<br>\n\u3088\u308a\u8a73\u7d30\u306a\u5185\u5bb9\u306f<a href=\"https:\/\/aiacademy.jp\/texts\/show\/?id=217\">\u3053\u3061\u3089<\/a>\u3092\u3054\u78ba\u8a8d\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2 id=\"i-1\">\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8<\/h2>\n<p>\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306b\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u30e1\u30ea\u30c3\u30c8\u3068\u3057\u3066\u306f\u3001<br>\n<strong>\u25cf \u30c7\u30fc\u30bf\u6570\u304c\u591a\u304f\u3066\u3082\u9ad8\u901f\u306a\u5b66\u7fd2\u3068\u8b58\u5225\u304c\u53ef\u80fd<\/strong><br>\n\u30fc \u30e9\u30f3\u30c0\u30e0\u5b66\u7fd2\u306b\u3088\u308a\u9ad8\u6b21\u5143\u7279\u5fb4\u3067\u3082\u52b9\u7387\u7684\u306a\u5b66\u7fd2\u304c\u53ef\u80fd<br>\n\u30fc \u9078\u629e\u3055\u308c\u305f\u7279\u5fb4\u91cf\u306e\u307f\u3067\u8b58\u5225\u53ef\u80fd<br>\n<strong>\u25cf \u6559\u5e2b\u4fe1\u53f7\u306e\u30ce\u30a4\u30ba\u306b\u5f37\u3044<\/strong><br>\n\u30fc \u5b66\u7fd2\u30c7\u30fc\u30bf\u306e\u30e9\u30f3\u30c0\u30e0\u9078\u629e\u306b\u3088\u308a\u5f71\u97ff\u3092\u6291\u5236\u53ef\u80fd<br>\n<strong>\u25cf \u7279\u5fb4\u91cf\u306e\u6b63\u898f\u5316\u3084\u6a19\u6e96\u5316\u304c\u5fc5\u8981\u306a\u3044<\/strong><\/p>\n<p>\u306a\u3069\u304c\u6319\u3052\u3089\u308c\u3001\u30c7\u30e1\u30ea\u30c3\u30c8\u3068\u3057\u3066\u306f<br>\n<strong>\u25cf \u30aa\u30fc\u30d0\u30fc\u30d5\u30a3\u30c3\u30c6\u30a3\u30f3\u30b0(\u904e\u5b66\u7fd2)\u3057\u3084\u3059\u3044<\/strong><br>\n\u30fc \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u591a\u3044<br>\n\u30fc \u5b66\u7fd2\u30c7\u30fc\u30bf\u304c\u5c11\u306a\u3044\u3068\u3046\u307e\u304f\u5b66\u7fd2\u304c\u3067\u304d\u306a\u3044<br>\n\u306a\u3069\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n<h2 id=\"i-2\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u5206\u985e\u306e\u5b9f\u88c5\u4f8b<\/h2>\n<p>\u5b9f\u88c5\u4f8b\u3068\u3057\u3066\u3001\u6c7a\u5b9a\u6728\u306e\u3068\u304d\u306b\u4f7f\u7528\u3057\u305f\u30bf\u30a4\u30bf\u30cb\u30c3\u30af\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<br>\n<a href=\"?id=35\">\u6c7a\u5b9a\u6728\u306e\u7ae0<\/a>\u3067\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3001\u4ee5\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4ed8\u3051\u8db3\u3057\u3066\u66f8\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n<strong>RandomForestClassifier\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/strong><\/p>\n<pre><code>from sklearn.ensemble import RandomForestClassifier\nclf_rf = RandomForestClassifier(n_estimators=30, random_state=0)\nclf_rf = clf_rf.fit(x_train, y_train)\n<\/code><\/pre>\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8aac\u660e\u3059\u308b\u3068\u3001<br>\nn_estimators \u30fb\u30fb\u30fb\u3000\u4f5c\u6210\u3059\u308b\u6c7a\u5b9a\u6728\u306e\u6570<br>\nrandom_state \uff65\uff65\uff65\u3000\u30e9\u30f3\u30c0\u30e0\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3059\u308b\u3068\u304d\u306e\u30b7\u30fc\u30c9\u5024<br>\n\u3067\u3059\u3002\u305d\u306e\u4ed6\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u3001<a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.ensemble.RandomForestClassifier.html\" rel=\"nofollow noopener\" target=\"_blank\">scikit-learn\u306eHP<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\n\u3067\u306f\u3001\u6b63\u89e3\u7387(ccuracy_score)\u3092\u307f\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u203baccuracy_score\u3084classification_report\u306a\u3069\u306e\u95a2\u6570\u306a\u3069\u306f<a href=\"https:\/\/aiacademy.jp\/texts\/show\/?id=34\">\u8a55\u4fa1\u6307\u6a19<\/a>\u306b\u3066\u8a73\u7d30\u306b\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"python:\"># \u6c7a\u5b9a\u6728\u306e\u7ae0\u3067\u5b9a\u7fa9\u3057\u305f\u95a2\u6570\u3068\u540c\u3058\u95a2\u6570\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\n\nmeasure_performance(x_train, y_train, clf_rf)\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u4e0b\u8a18\u5185\u5bb9\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"shell\">Accuracy:0.874\nClassification report\nprecision    recall  f1-score   support\n0.0       0.86      0.97      0.91       662\n1.0       0.91      0.68      0.78       322\navg \/ total       0.88      0.87      0.87       984\nConfussion matrix\n[[641  21]\n[103 219]]\n<\/code><\/pre>\n<p>\u6c7a\u5b9a\u6728\u3088\u308a\u3082\u9ad8\u3044\u7cbe\u5ea6\u3067\u5206\u985e\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2 id=\"i-3\">\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u56de\u5e30\u306e\u5b9f\u88c5\u4f8b<\/h2>\n<p>\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306f\u56de\u5e30\u3082\u53ef\u80fd\u3067\u3059\u3002<br>\nscikit-learn\u3067\u306f\u3001<strong><a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.ensemble.RandomForestRegressor.html\" rel=\"nofollow noopener\" target=\"_blank\">RandomForestRegressor<\/a><\/strong>\u3092\u4f7f\u3046\u3053\u3068\u3067\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3067\u56de\u5e30\u3092\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<br>\n\u30c7\u30fc\u30bf\u306f\u30dc\u30b9\u30c8\u30f3\u8fd1\u90ca\u306e\u4f4f\u5b85\u60c5\u5831\u4f7f\u7528\u3057\u3066\u30e2\u30c7\u30eb\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"python:\"># \u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pandas import DataFrame\nfrom sklearn.datasets import load_boston\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\n\n# \u30dc\u30b9\u30c8\u30f3\u8fd1\u90ca\u306e\u4f4f\u5b85\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\nboston = load_boston()\ndf = DataFrame(boston.data, columns = boston.feature_names)\ndf['MEDV'] = np.array(boston.target)\n\n# \u8aac\u660e\u5909\u6570\u53ca\u3073\u76ee\u7684\u5909\u6570\nX = df.iloc[:, :-1].values\ny = df.loc[:, 'MEDV'].values\n\n# \u5b66\u7fd2\u7528\u3001\u691c\u8a3c\u7528\u30c7\u30fc\u30bf\u306b\u5206\u5272\n(X_train, X_test, y_train, y_test) = train_test_split(X, y, test_size = 0.3, random_state = 0)\n\n# \u30e2\u30c7\u30eb\u69cb\u7bc9\nforest = RandomForestRegressor()\nforest.fit(X_train, y_train)\n\n# \u4e88\u6e2c\u5024\u3092\u8a08\u7b97\ny_train_pred = forest.predict(X_train)\ny_test_pred = forest.predict(X_test)\n\n# MSE\u306e\u8a08\u7b97\nfrom sklearn.metrics import mean_squared_error\nprint('MSE train : %.3f, test : %.3f' % (mean_squared_error(y_train, y_train_pred), mean_squared_error(y_test, y_test_pred)) )\n\n# R^2\u306e\u8a08\u7b97\nfrom sklearn.metrics import r2_score\nprint('MSE train : %.3f, test : %.3f' % (r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred)) )\n\n# \u6b8b\u5dee\u30d7\u30ed\u30c3\u30c8\n# %matplotlib inline\nplt.figure(figsize = (10, 7))\nplt.scatter(y_train_pred, y_train_pred - y_train, c = 'green', marker = 'o', s = 35, alpha = 0.5, label = 'Training data')\nplt.scatter(y_test_pred, y_test_pred - y_test, c = 'blue', marker = 's', s = 35, alpha = 0.7, label = 'Test data')\nplt.xlabel('Predicted values')\nplt.ylabel('Residuals')\nplt.legend(loc = 'upper left')\nplt.hlines(y = 0, xmin = -10, xmax = 50, lw = 2, color = 'red')\nplt.xlim([-10, 50])\nplt.show()\n<\/code><\/pre>\n<p><strong>\u6a2a\u8ef8\u306b\u4e88\u6e2c\u5024\uff08\u307e\u305f\u306f\u8aac\u660e\u5909\u6570\uff09\u3092\u3068\u308a\u3001\u7e26\u8ef8\u306b\u56de\u5e30\u6b8b\u5dee\u3092\u3068\u3063\u3066\u30d7\u30ed\u30c3\u30c8\u3057\u305f\u6b8b\u5dee\u30d7\u30ed\u30c3\u30c8<\/strong>\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\n\u6b8b\u5dee\u30d7\u30ed\u30c3\u30c8\u3092\u898b\u308b\u3068\u3001\u691c\u8a3c\u7528\u306e\u30c7\u30fc\u30bf\u3088\u308a\u5b66\u7fd2\u30c7\u30fc\u30bf\u306b\u3088\u308a\u9069\u5408\u3057\u305f\u30e2\u30c7\u30eb\u3067\u3042\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/aiacademy.jp\/assets\/images\/plot.png\" alt=\"\"><\/p>\n<h2 id=\"i-4\">\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0<\/h2>\n<p><strong>\u5206\u985e\u306e\u30d7\u30ed\u30b0\u30e9\u30e0<\/strong><\/p>\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\n# \u4e0a\u8a18\u30b3\u30fc\u30c9\u3067ValueError: could not convert string to float:\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\n# mean_age = np.mean(titanic_x[ages != '',1].astype(float))\n\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\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=0)\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\nmeasure_performance(x_train, y_train, clf)\n\n\nfrom sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier(n_estimators=30, random_state=0)\nclf = clf.fit(x_train, y_train)\n\n#\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u30e2\u30c7\u30eb\u306e\u8a55\u4fa1\nmeasure_performance(x_train, y_train, clf)\n<\/code><\/pre>\n<p><strong>\u56de\u5e30\u306e\u30d7\u30ed\u30b0\u30e9\u30e0<\/strong><\/p>\n<pre><code class=\"python:\"># ======= \u56de\u5e30 =======\n# \u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pandas import DataFrame\nfrom sklearn.datasets import load_boston\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\n\n# \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\nboston = load_boston()\ndf = DataFrame(boston.data, columns = boston.feature_names)\ndf['MEDV'] = np.array(boston.target)\n\n# \u8aac\u660e\u5909\u6570\u53ca\u3073\u76ee\u7684\u5909\u6570\nX = df.iloc[:, :-1].values\ny = df.loc[:, 'MEDV'].values\n\n# \u5b66\u7fd2\u7528\u3001\u691c\u8a3c\u7528\u30c7\u30fc\u30bf\u306b\u5206\u5272\n(X_train, X_test, y_train, y_test) = train_test_split(X, y, test_size = 0.3, random_state = 0)\n\n# \u30e2\u30c7\u30eb\u69cb\u7bc9\nforest = RandomForestRegressor()\nforest.fit(X_train, y_train)\n\n# \u4e88\u6e2c\u5024\u3092\u8a08\u7b97\ny_train_pred = forest.predict(X_train)\ny_test_pred = forest.predict(X_test)\n\n# MSE\u306e\u8a08\u7b97\nfrom sklearn.metrics import mean_squared_error\nprint('MSE train : %.3f, test : %.3f' % (mean_squared_error(y_train, y_train_pred), mean_squared_error(y_test, y_test_pred)) )\n\n# R^2\u306e\u8a08\u7b97\nfrom sklearn.metrics import r2_score\nprint('MSE train : %.3f, test : %.3f' % (r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred)) )\n\n# \u6b8b\u5dee\u30d7\u30ed\u30c3\u30c8\n# %matplotlib inline\nplt.figure(figsize = (10, 7))\nplt.scatter(y_train_pred, y_train_pred - y_train, c = 'green', marker = 'o', s = 35, alpha = 0.5, label = 'Training data')\nplt.scatter(y_test_pred, y_test_pred - y_test, c = 'blue', marker = 's', s = 35, alpha = 0.7, label = 'Test data')\nplt.xlabel('Predicted values')\nplt.ylabel('Residuals')\nplt.legend(loc = 'upper left')\nplt.hlines(y = 0, xmin = -10, xmax = 50, lw = 2, color = 'red')\nplt.xlim([-10, 50])\nplt.show()\n<\/code><\/pre>\n<h2 id=\"i-5\">\u307e\u3068\u3081<\/h2>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3092\u5b66\u7fd2\u3057\u307e\u3057\u305f\u3002<br>\n<strong>\u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306f\u3001\u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u5b66\u7fd2\u306e\u4e00\u7a2e\u3067\u8907\u6570\u306e\u6c7a\u5b9a\u6728\u3092\u7528\u3044\u3066\u5b66\u7fd2\u3057\u307e\u3059\u3002<\/strong><br>\n\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u3057\u3066\u306e\u7279\u5fb4\u3092\u6291\u3048\u3066\u3001\u4ed6\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u4f7f\u3044\u5206\u3051\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h1 id=\"i-6\">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 \u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3068\u306f \u30e1\u30ea\u30c3\u30c8\u3068\u30c7\u30e1\u30ea\u30c3\u30c8 \u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u5206\u985e\u306e\u5b9f\u88c5\u4f8b \u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u306e\u56de\u5e30\u306e\u5b9f\u88c5\u4f8b \u4eca\u56de\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0 \u307e\u3068\u3081 Python\u3084\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\uff1f \u30e9\u30f3\u30c0\u30e0\u30d5\u30a9\u30ec\u30b9\u30c8\u3068\u306f  &#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,130],"tags":[],"class_list":{"0":"post-252","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\/252","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=252"}],"version-history":[{"count":4,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":4830,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions\/4830"}],"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=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}