{"id":2126,"date":"2022-02-16T09:10:44","date_gmt":"2022-02-16T00:10:44","guid":{"rendered":"https:\/\/aiacademy.jp\/media\/?p=2126"},"modified":"2024-08-08T15:39:11","modified_gmt":"2024-08-08T06:39:11","slug":"%e3%80%90python%e3%80%91sklearn-fit%e6%99%82%e3%81%ab%e7%99%ba%e7%94%9f%e3%81%99%e3%82%8b%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ae%e5%8e%9f%e5%9b%a0%e3%81%a8%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/aiacademy.jp\/media\/?p=2126","title":{"rendered":"\u3010Python\u3011sklearn fit()\u6642\u306b\u767a\u751f\u3059\u308b\u30a8\u30e9\u30fc\u306e\u539f\u56e0\u3068\u89e3\u6c7a\u65b9\u6cd5"},"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 Python\u30b3\u30fc\u30b9 \u25b6 \u53d7\u8b1b\u5272\u5f15\u30af\u30fc\u30dd\u30f3 \u7121\u6599\u52d5\u753b<\/a><\/div>\n<\/div>\n<\/div>\n\n\n<h1 class=\"wp-block-heading\" id=\"i-0\">ValueError: Expected 2D array, got 1D array instead<\/h1>\n\n\n\n<p>sklearn\u306e\u30e2\u30c7\u30eb\u3092\u5b66\u7fd2\u3059\u308b\u969b\u306b\u3001fit()\u3092\u5b9f\u884c\u3059\u308b\u3068\u300cValueError: Expected 2D array, got 1D array instead\u300d\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u306f.reshape(1,-1)\u3092\u4f7f\u3046\u3088\u3046\u306b\u66f8\u3044\u3066\u3042\u308a\u3001.reshape(1,-1)\u3092\u4f7f\u3048\u3070\u30a8\u30e9\u30fc\u56de\u907f\u51fa\u6765\u307e\u3059\u304c\u3001\u672c\u8cea\u7684\u306b\u7406\u89e3\u3067\u304d\u3066\u3044\u306a\u3044\u30fb\u30fb\u30fb\u3068\u3044\u3046\u65b9\u3082\u3044\u3089\u3063\u3057\u3083\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u300c<meta charset=\"utf-8\">ValueError: Expected 2D array, got 1D array instead\u300d\u306e\u539f\u56e0\u3068\u30a8\u30e9\u30fc\u306e\u89e3\u6c7a\u65b9\u6cd5\u3092\u89e3\u8aac\u81f4\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-1\">\u30a8\u30e9\u30fc\u306e\u539f\u56e0\u3068\u89e3\u6c7a\u7b56<\/h1>\n\n\n\n<p>\u30a8\u30e9\u30fc\u306e\u539f\u56e0\u3067\u3059\u304c\u3001sklearn\u306efit()\u3067\u306f\u3001\u7b2c\u4e00\u5f15\u6570\u306eX\u306b\u306f\u3001DataFrame\u578b\u3084numpy\u306e\u884c\u5217\u5f62\u5f0f\uff08\u7e26\u30d9\u30af\u30c8\u30eb\uff09\u306e\u307f\u3092\u53d7\u3051\u4ed8\u3051\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u305d\u308c\u4ee5\u5916\u306e\u901a\u5e38\u306e\u30ea\u30b9\u30c8\u578b\u306a\u3069\u3092\u5f15\u6570\u306b\u6e21\u3057\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u30c7\u30fc\u30bf\u306e\u5f62\u72b6\u304c1D\u914d\u5217\u306e\u5834\u5408\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\uff08\u901a\u5e38\u306e\u30ea\u30b9\u30c8\u3084Serieis\u578b\u3092\u6e21\u3059\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u767a\u751f\uff09<\/p>\n\n\n\n<p><strong>ValueError: Expected 2D array, got 1D array instead: array=[12 16 20 28 36]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.<\/strong><\/p>\n\n\n\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f\u3001X\u306e\u5f62\u72b6\u304c2D\u914d\u5217\u3067\u306f\u306a\u304f\u30011D\u914d\u5217\uff08\u901a\u5e38\u306e\u30ea\u30b9\u30c8\u7b49\uff09\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002&nbsp;<\/p>\n\n\n\n<p><strong>\u30fb\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u4f8b\u3067\u3059<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u4f8b\nfrom sklearn.linear_model import LinearRegression\n\nx = [12,16,20,28,36]\ny = [700,900,1300,1750,1800]\n\nmodel = LinearRegression()\nmodel.fit(x,y)<\/code><\/pre>\n\n\n\n<p>\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u3001fit()\u3092\u6b63\u5e38\u306b\u51e6\u7406\u3055\u305b\u308b\u306b\u306f\u3001&nbsp;<strong>\u4e8c\u91cd\u30ab\u30c3\u30b3<\/strong>&nbsp;\u3082\u3057\u304f\u306f\u3001\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u8a18\u8f09\u304c\u3042\u308b\u3088\u3046\u306b<code><strong>reshape(-1,1\uff09<\/strong><\/code>\u3092\u7528\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.linear_model import LinearRegression\n\nX = [[12],[16],[20],[28],[36]]\ny = [[700],[900],[1300],[1750],[1800]]\n\nmodel = LinearRegression()\nmodel.fit(X,y)<\/code><\/pre>\n\n\n\n<p><strong>reshape(-1, 1)<\/strong>&nbsp;\u3092\u7528\u3044\u308b\u5834\u5408<\/p>\n\n\n\n<p><a href=\"https:\/\/aiacademy.jp\/texts\/show\/?id=589&amp;context=subject-python4\">\u95a2\u9023\u30c6\u30ad\u30b9\u30c8\uff1aNumPy reshape\u306e-1\u306b\u95a2\u3057\u3066 | AI Academy<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nfrom sklearn.linear_model import LinearRegression\n\nx = [12,16,20,28,36]\ny = [700,900,1300,1750,1800]\n\nX = np.array(x).reshape(-1,1)\n\nmodel = LinearRegression()\nmodel.fit(X,y)<\/code><\/pre>\n\n\n\n<p>reshape()\u306fnumpy array\u306b\u5bfe\u3057\u3066\u5229\u7528\u3067\u304d\u308b\u305f\u3081\u3001\u30ea\u30b9\u30c8\u578b\u306ex\u3092numpy array\u306b\u3057\u3066\u304b\u3089\u3001reshape()\u3092\u5b9f\u65bd\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u753b\u50cf\u306e\u30011\u3064\u76ee\u306ex\u306f\u901a\u5e38\u306e\u30ea\u30b9\u30c8\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u3066\u304a\u308a\u30012\u3064\u76ee\u306ex\u306fnumpy array\u3068\u3057\u3066\u306ex\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2022-01-31-16.06.36.png\" alt=\"\" class=\"wp-image-1719\"><\/figure>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001sklearn\u306efit()\u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u72b6\u3092\u53d7\u3051\u4ed8\u3051\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array([[12],\n       [16],\n       [20],\n       [28],\n       [36]])<\/code><\/pre>\n\n\n\n<p>fit()\u306b\u6e21\u3059X\u306e\u5f62\u72b6\u304c\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u884c\u5217\u5f62\u5f0f\u3067\u3042\u308c\u3070\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(X.shape) # (5,1)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-2\">\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\uff1f<\/h1>\n\n\n\n<p><meta charset=\"utf-8\">\u6a5f\u68b0\u5b66\u7fd2\u3092\u52b9\u7387\u3088\u304f\u5b66\u3076\u306b\u306f\u3001\u666e\u6bb5\u304b\u3089<meta charset=\"utf-8\">\u6a5f\u68b0\u5b66\u7fd2\u3092\u6271\u3063\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\u72ec\u5b66\u3067\u52c9\u5f37\u3057\u3066\u3044\u308b\u3068\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u308a\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u7591\u554f\u70b9\u3082\u89e3\u6c7a\u3057\u306a\u304f\u3066\u3082\u3001\u6b21\u306b\u9032\u3081\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u3053\u306e\u52c9\u5f37\u65b9\u6cd5\u3060\u3068\u52b9\u7387\u3082\u60aa\u3044\u3067\u3059\u3002<br>\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\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/aiacademy.jp\/bootcamp\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"341\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-1024x341.png\" alt=\"\" class=\"wp-image-691\" 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\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ValueError: Expected 2D array, got 1D array instead sklearn\u306e\u30e2\u30c7\u30eb\u3092\u5b66\u7fd2\u3059\u308b\u969b\u306b\u3001fit()\u3092\u5b9f\u884c\u3059\u308b\u3068\u300cValueError: Expected 2D arr &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2127,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[165],"tags":[],"class_list":{"0":"post-2126","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-165"},"_links":{"self":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/2126","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=2126"}],"version-history":[{"count":2,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/2126\/revisions"}],"predecessor-version":[{"id":4836,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/2126\/revisions\/4836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/media\/2127"}],"wp:attachment":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}