{"id":4472,"date":"2024-02-13T12:21:04","date_gmt":"2024-02-13T03:21:04","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4472"},"modified":"2024-02-13T12:21:04","modified_gmt":"2024-02-13T03:21:04","slug":"%e5%8f%8c%e6%96%b9%e5%90%91lstm%e3%81%ab%e3%82%88%e3%82%8b%e6%99%82%e7%b3%bb%e5%88%97%e4%ba%88%e6%b8%ac%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ae%e6%a7%8b%e7%af%89","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/%e5%8f%8c%e6%96%b9%e5%90%91lstm%e3%81%ab%e3%82%88%e3%82%8b%e6%99%82%e7%b3%bb%e5%88%97%e4%ba%88%e6%b8%ac%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ae%e6%a7%8b%e7%af%89\/","title":{"rendered":"\u53cc\u65b9\u5411LSTM\u306b\u3088\u308b\u6642\u7cfb\u5217\u4e88\u6e2c\u30e2\u30c7\u30eb\u306e\u69cb\u7bc9"},"content":{"rendered":"\n<p>\u6642\u7cfb\u5217\u4e88\u6e2c\u306f\u3001\u904e\u53bb\u306e\u30c7\u30fc\u30bf\u3092\u57fa\u306b\u672a\u6765\u306e\u5024\u3092\u4e88\u6e2c\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3067\u3059\u3002\u53cc\u65b9\u5411LSTM\uff08Long Short-Term Memory\uff09\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u3001\u3053\u306e\u30bf\u30a4\u30d7\u306e\u554f\u984c\u306b\u7279\u306b\u6709\u52b9\u3067\u3042\u308a\u3001\u904e\u53bb\u306e\u60c5\u5831\u3060\u3051\u3067\u306a\u304f\u3001\u672a\u6765\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\uff08\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306e\u5834\u5408\u306f\u3001\u524d\u5f8c\u306e\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\uff09\u3092\u8003\u616e\u3059\u308b\u80fd\u529b\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n<h2>\u6982\u8981<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u6642\u7cfb\u5217\u4e88\u6e2c\u306f\u3001\u904e\u53bb\u306e\u30c7\u30fc\u30bf\u3092\u57fa\u306b\u672a\u6765\u306e\u5024\u3092\u4e88\u6e2c\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3067\u3059\u3002\u53cc\u65b9\u5411LSTM\uff08Long Short-Term Memory\uff09\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u3001\u3053\u306e\u30bf\u30a4\u30d7\u306e\u554f\u984c\u306b\u7279\u306b\u6709\u52b9\u3067\u3042\u308a\u3001\u904e\u53bb\u306e\u60c5\u5831\u3060\u3051\u3067\u306a\u304f\u3001\u672a\u6765\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\uff08\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306e\u5834\u5408\u306f\u3001\u524d\u5f8c\u306e\u30c7\u30fc\u30bf\u30dd\u30a4\u30f3\u30c8\uff09\u3092\u8003\u616e<span data-token-index=\"1\" class=\"discussion-level-1 discussion-id-d55f2912-7100-4873-bbe5-4365fd30c705 notion-enable-hover\">\u3059\u308b\u80fd\u529b\u304c\u3042\u308a\u307e\u3059\u3002<\/span><\/span><!-- notionvc: b048e8f9-ddce-4b62-b036-78c9c4aab508 --><\/p>\n\n<h2>\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3068\u30c7\u30fc\u30bf\u306e\u6e96\u5099<\/h2>\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn.preprocessing import StandardScaler\r\nfrom tensorflow.keras.models import Sequential \r\nfrom tensorflow.keras.layers import Dense, LSTM, Bidirectional, Dropout \r\nfrom tensorflow.keras.optimizers import Adam \r\nimport numpy as np \r\nimport pandas as pd<\/code><\/pre>\r\n<\/div>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u30c7\u30fc\u30bf\u306e\u6e96\u5099<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306f\u3001\u7279\u5fb4\u91cf\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u5909\u6570\u3092\u542b\u3080CSV\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002\u7279\u5fb4\u91cf\u306f\u30e2\u30c7\u30eb\u306e\u5165\u529b\u3068\u3057\u3066\u3001\u30bf\u30fc\u30b2\u30c3\u30c8\u5909\u6570\u306f\u4e88\u6e2c\u5bfe\u8c61\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>train_df = pd.read_csv(\"train.csv\")\r\ntest_df = pd.read_csv(\"test.csv\")<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: 58a391cb-4913-4cbf-8726-807eeac8d9a1 --><\/p>\n\n<h2>\u7279\u5fb4\u91cf\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u9078\u629e<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u30bb\u30c3\u30c8\u3068\u30c6\u30b9\u30c8\u30bb\u30c3\u30c8\u306b\u5171\u901a\u3059\u308b\u7279\u5fb4\u91cf\u306e\u307f\u3092\u9078\u629e\u3057\u3001\u4e88\u6e2c\u30e2\u30c7\u30eb\u306e\u8a13\u7df4\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002\u30bf\u30fc\u30b2\u30c3\u30c8\u5909\u6570\u306f\u3001\u4e88\u6e2c\u3057\u305f\u3044\u6570\u5024\u3067\u3059\u3002<\/span><!-- notionvc: 095bc250-d16b-4f62-b800-00e606280747 --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u7279\u5fb4\u91cf\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u5909\u6570\u3092\u5b9a\u7fa9\r\ncommon_features = set(train_df.columns).intersection(test_df.columns) - {'TARGET'}\r\nX_train = train_df[list(common_features)].copy() \r\ny_train = train_df['TARGET'] \r\nX_test = test_df[list(common_features)].copy()<\/code><\/pre>\r\n<\/div>\r\n<p><br \/>\r\n<!-- notionvc: 96b1097b-424a-461f-9eba-bc646101a7a5 --><\/p>\n\n<h2>\u30c7\u30fc\u30bf\u306e\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u3068\u6574\u5f62<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u53cc\u65b9\u5411LSTM\u30e2\u30c7\u30eb\u306f\u3001\u3059\u3079\u3066\u306e\u5165\u529b\u7279\u5fb4\u91cf\u304c\u540c\u3058\u30b9\u30b1\u30fc\u30eb\u306b\u3042\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002<strong><code>StandardScaler<\/code><\/strong>\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u3092\u6b63\u898f\u5316\u3057\u3001\u9069\u5207\u306a\u5f62\u72b6\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30c7\u30fc\u30bf\u306e\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0 \r\nscaler = StandardScaler() \r\nX_train_scaled = scaler.fit_transform(X_train) \r\nX_test_scaled = scaler.transform(X_test) \r\n# LSTM\u306b\u9069\u3057\u305f\u5f62\u72b6\u306b\u30c7\u30fc\u30bf\u3092\u6574\u5f62 \r\nX_train_scaled = X_train_scaled.reshape((X_train_scaled.shape[0], 1, X_train_scaled.shape[1]))\r\nX_test_scaled = X_test_scaled.reshape((X_test_scaled.shape[0], 1, X_test_scaled.shape[1]))<\/code><\/pre>\r\n<\/div>\r\n<p><br \/>\r\n<!-- notionvc: c2cff38b-ffed-43eb-b6e4-6873bc1c2cdc --><\/p>\r\n<p><!-- notionvc: 8a92bc56-5f5b-4d74-8806-89f98166802b --><\/p>\n\n<h2>\u30e2\u30c7\u30eb\u306e\u8a2d\u8a08<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u53cc\u65b9\u5411LSTM\u5c64\u3092\u542b\u3080\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u30012\u3064\u306e\u53cc\u65b9\u5411LSTM\u5c64\u30682\u3064\u306e\u30c9\u30ed\u30c3\u30d7\u30a2\u30a6\u30c8\u5c64\u3092\u4f7f\u7528\u3057\u3066\u3001\u904e\u5b66\u7fd2\u3092\u9632\u304e\u307e\u3059\u3002<!-- notionvc: 36824698-8d8b-4064-b0ec-581fd2a9f4ac --><\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model = Sequential([ \r\n\u3000\u3000\u3000Bidirectional(LSTM(50, return_sequences=True), input_shape=(X_train_scaled.shape[1], X_train_scaled.shape[2])), \r\n\u3000\u3000\u3000Dropout(0.2),\r\n\u3000\u3000\u3000Bidirectional(LSTM(50)), \r\n\u3000\u3000\u3000Dropout(0.2),\r\n\u3000\u3000\u3000Dense(1)\r\n ])<!-- notionvc: ab2830ad-9b6e-427f-9114-341acb2e3e1e -->\r\n<\/code><\/pre>\r\n<\/div>\r\n<p><br \/>\r\n<!-- notionvc: 3411b4d6-5d3a-477d-8057-d33153135da0 --><\/p>\n\n<h2>\u30e2\u30c7\u30eb\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3068\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><span data-token-index=\"0\" class=\"notion-enable-hover\">Adam<\/span>\u30aa\u30d7\u30c6\u30a3\u30de\u30a4\u30b6\u30fc\u3068\u5e73\u5747\u4e8c\u4e57\u8aa4\u5dee\u3092\u640d\u5931\u95a2\u6570\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u30e2\u30c7\u30eb\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001\u30e2\u30c7\u30eb\u3092\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u30c7\u30fc\u30bf\u306b\u30d5\u30a3\u30c3\u30c8\u3055\u305b\u307e\u3059<\/span><!-- notionvc: e09d44eb-54b6-4677-a7b4-25e0583108e5 --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model.compile(optimizer=Adam(learning_rate=0.001),loss='mean_squared_error') \r\nhistory = model.fit(X_train_scaled, y_train, epochs=3, batch_size=32, validation_split=0.2, verbose=1)<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42.png\" alt=\"\" width=\"1644\" height=\"176\" class=\"alignnone size-full wp-image-4473\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42.png 1644w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42-300x32.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42-1024x110.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42-768x82.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2024-02-06-11.56.42-1536x164.png 1536w\" sizes=\"(max-width: 1644px) 100vw, 1644px\" \/><br \/>\r\n<!-- notionvc: a1806f63-9c0a-47b9-adc5-30b028012ba8 --><\/p>\n\n<h2>\u4e88\u6e2c\u306e\u5b9f\u884c\u3068\u7d50\u679c\u306e\u89e3\u6790<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u6700\u5f8c\u306b\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u4e88\u6e2c\u3092\u884c\u3044\u3001\u7d50\u679c\u3092\u89e3\u6790\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>predicted = model.predict(X_test_scaled) \r\ntest_df['prediction'] = predicted.flatten()<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: cf7b615e-2906-47a6-b2c4-ee01e032477d --><\/p>\n\n<h2>\u307e\u3068\u3081<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u901a\u3058\u3066\u3001\u53cc\u65b9\u5411LSTM\u3092\u4f7f\u7528\u3057\u305f\u6642\u7cfb\u5217\u4e88\u6e2c\u30e2\u30c7\u30eb\u306e\u69cb\u7bc9\u65b9\u6cd5\u3092\u8a73\u7d30\u306b\u8aac\u660e\u3057\u307e\u3057\u305f\u3002\u53cc\u65b9\u5411LSTM\u306f\u3001\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306e\u4e21\u65b9\u306e\u65b9\u5411\u304b\u3089\u306e\u60c5\u5831\u3092\u5229\u7528\u3057\u3066\u4e88\u6e2c\u7cbe\u5ea6\u3092\u9ad8\u3081\u308b\u305f\u3081\u306b\u7279\u306b\u6709\u52b9\u306a\u30e2\u30c7\u30eb\u3067\u3059\u3002\u3053\u306e\u30e2\u30c7\u30eb\u306f\u591a\u69d8\u306a\u6642\u7cfb\u5217\u4e88\u6e2c\u30bf\u30b9\u30af\u306b\u5fdc\u7528\u53ef\u80fd\u3067\u3042\u308a\u3001\u5b9f\u969b\u306e\u30d3\u30b8\u30cd\u30b9\u30b7\u30ca\u30ea\u30aa\u3084\u7814\u7a76\u3067\u6709\u7528\u306a\u6d1e\u5bdf\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002<\/span><!-- notionvc: bea14dfe-87ae-4ffe-8a43-a0d328b66be7 --><\/p>","protected":false},"excerpt":{"rendered":"<p>\u6642\u7cfb\u5217\u4e88\u6e2c\u306f\u3001\u904e\u53bb\u306e\u30c7\u30fc\u30bf\u3092\u57fa\u306b\u672a\u6765\u306e\u5024\u3092\u4e88\u6e2c\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3067\u3059\u3002\u53cc\u65b9\u5411LSTM\uff08Long Short-Term Memory\uff09\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u3001\u3053\u306e\u30bf\u30a4\u30d7\u306e\u554f\u984c\u306b\u7279\u306b\u6709\u52b9\u3067\u3042\u308a\u3001\u904e\u53bb\u306e\u60c5\u5831\u3060\u3051\u3067\u306a\u304f\u3001\u672a\u6765\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\uff08\u6642 [&hellip;]<\/p>\n","protected":false},"author":87,"featured_media":4474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1246],"tags":[331,484,551,552,553],"class_list":["post-4472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-infrastructure","tag-python","tag-484","tag-lstm","tag-552","tag-553"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4472"}],"version-history":[{"count":1,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4472\/revisions"}],"predecessor-version":[{"id":4475,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4472\/revisions\/4475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/4474"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}