{"type":"doc","content":[{"type":"heading","attrs":{"id":"eb93bc73-34b6-43e0-b107-1b8d4167106b","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"当人工智能遇见金融市场"}]},{"type":"paragraph","attrs":{"id":"73666db1-82c7-4932-95d6-c98e5597dc62","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"股票市场如同一个巨大的信息海洋,无数投资者在其中寻找规律。传统分析方法依赖技术指标与基本面数据,但随着人工智能的发展,一种名为 "},{"type":"text","marks":[{"type":"bold"}],"text":"LSTM(长短期记忆网络) "},{"type":"text","text":"的深度学习模型,正在为预测股票价格提供新的可能性。本文将以苹果公司(AAPL)的股价为例,手把手教你如何在JupyterLab中搭建LSTM模型,并解释其背后的科学逻辑。即使你没有任何编程经验,也能跟随这篇教程理解核心思想。"}]},{"type":"heading","attrs":{"id":"188eeed3-34d1-41ff-86c5-a6e4544bca62","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"LSTM为什么适合预测股价?"}]},{"type":"heading","attrs":{"id":"18f409f7-d073-40e1-b5bf-c36ed83855fa","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"1.1 时间序列的\"记忆困境\""}]},{"type":"paragraph","attrs":{"id":"da5d7e6b-458c-497b-8363-9a99c11bc293","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"假设你每天记录气温,想预测明天的温度。最直接的方法是参考最近几天的数据,但极端天气(如寒潮)可能与一个月前的天气模式相关。传统模型难以捕捉这种长期依赖关系,而LSTM通过独特的\"门控机制\"(输入门、遗忘门、输出门),能自主决定记住或遗忘哪些信息。"}]},{"type":"heading","attrs":{"id":"babc63eb-cb0c-4058-ac84-4c0a7cba7b70","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"1.2 股价预测的特殊挑战"}]},{"type":"paragraph","attrs":{"id":"31e6d999-0784-4a2c-ae4f-551a600fa99b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"股票价格具有"},{"type":"text","marks":[{"type":"bold"}],"text":"高噪声"},{"type":"text","text":"(受突发事件影响)、"},{"type":"text","marks":[{"type":"bold"}],"text":"非平稳性"},{"type":"text","text":"(统计特性随时间变化)和"},{"type":"text","marks":[{"type":"bold"}],"text":"非线性"},{"type":"text","text":"(多个因素复杂交互)的特点。LSTM的优势在于:"}]},{"type":"bulletList","attrs":{"id":"c8b71a5d-7760-40d2-92d5-4e00a49b1365","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"cc784fe3-b0f0-4b43-a38a-f05ba5f5e5b3"},"content":[{"type":"paragraph","attrs":{"id":"7539163c-a1fb-4d5d-a0a1-7685cfab242a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"能处理时间跨度长的依赖关系"}]}]},{"type":"listItem","attrs":{"id":"934a7c0f-c017-4caa-92bd-5a57ff51434b"},"content":[{"type":"paragraph","attrs":{"id":"74d4fb8b-e481-41e3-a40c-689d52e54a04","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"对噪声数据有一定鲁棒性"}]}]},{"type":"listItem","attrs":{"id":"411f5761-4e90-464b-9f1b-ac6f934e6591"},"content":[{"type":"paragraph","attrs":{"id":"356d4da7-57af-47ae-8866-876c7af95d3a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"可自动提取时间序列中的深层特征"}]}]}]},{"type":"heading","attrs":{"id":"35d3a5ae-4ae7-4c58-b83e-7e907c769717","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"1.3 环境准备"}]},{"type":"paragraph","attrs":{"id":"9774fc35-e5d5-4226-afbd-94738743e34c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"首先,确保你已经安装了必要的Python库。你可以使用以下命令来安装所需的库:"}]},{"type":"codeBlock","attrs":{"id":"9a8a57f3-7825-4967-ad0d-32e5878f2d7f","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"pip install numpy pandas matplotlib tensorflow scikit-learn yfinance"}]},{"type":"bulletList","attrs":{"id":"d9e02d7d-eb8e-44e6-8f6f-3fe6c84aa00f","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"dbd3e714-7cb0-4d16-9597-b54f107c6ae4"},"content":[{"type":"paragraph","attrs":{"id":"67808f54-81dc-434d-b6b1-30832bab4ac3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"numpy"},{"type":"text","text":" 和 "},{"type":"text","marks":[{"type":"code"}],"text":"pandas"},{"type":"text","text":" 用于数据处理。"}]}]},{"type":"listItem","attrs":{"id":"e548dc3b-8a80-4cff-a648-2d477b8a0b42"},"content":[{"type":"paragraph","attrs":{"id":"d0c93968-a299-4b04-8751-8c60770f3180","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"matplotlib"},{"type":"text","text":" 用于数据可视化。"}]}]},{"type":"listItem","attrs":{"id":"d9f25a8d-1334-4084-8e3d-345ea9957962"},"content":[{"type":"paragraph","attrs":{"id":"81c1e90c-f360-42de-b316-6c64c1373007","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"tensorflow"},{"type":"text","text":" 是深度学习框架,包含LSTM模型。"}]}]},{"type":"listItem","attrs":{"id":"e9a15847-3c47-44ff-b189-32401311d2ee"},"content":[{"type":"paragraph","attrs":{"id":"e4a50642-4edd-46e2-b1ef-6a4146c519fe","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"scikit-learn"},{"type":"text","text":" 用于数据预处理。"}]}]},{"type":"listItem","attrs":{"id":"ee267744-b551-464e-a32c-182e9d4970bc"},"content":[{"type":"paragraph","attrs":{"id":"d314887d-0960-4f86-ad0d-a4c106d92278","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"yfinance"},{"type":"text","text":" 用于获取股票数据。"}]}]}]},{"type":"heading","attrs":{"id":"abe1b00d-b4af-4178-9713-8970b71debf8","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"数据准备"}]},{"type":"heading","attrs":{"id":"39b84937-f4fb-4b62-9754-6743408f350c","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"2.1 获取股票数据"}]},{"type":"paragraph","attrs":{"id":"98d41c0e-0202-403a-8684-33d33a539681","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"我们使用"},{"type":"text","marks":[{"type":"code"}],"text":"yfinance"},{"type":"text","text":"库获取苹果公司2010-2025年的历史数据。选择收盘价(Close)作为核心指标,因为它反映每日交易最终共识。"}]},{"type":"codeBlock","attrs":{"id":"cd8738f2-012d-4c9a-a080-45e5bb8a5ae6","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"import yfinance as yf\ndata = yf.download('AAPL', start='2010-01-01', end='2025-01-01')"}]},{"type":"image","attrs":{"id":"98e079c3-f388-4558-b6a0-c660cc07cd5e","src":"https://developer.qcloudimg.com/editor/image/5421023/20250324-86b503e1.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":1100,"aspectRatio":"2.289026","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"paragraph","attrs":{"id":"2b65b59f-bc18-4866-a163-3d68ed0ddb71","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"f2d20984-9268-47fb-b56d-4c7cc450ec80","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"2.2 数据预处理三部曲"}]},{"type":"image","attrs":{"id":"01798ead-f502-40bf-8359-99b1c20a18f5","src":"https://developer.qcloudimg.com/editor/image/5421023/20250324-b6dd519f.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":1100,"aspectRatio":"3.874680","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"orderedList","attrs":{"id":"7b24843d-e0ac-4866-afaf-767bbc1e4071","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"2d5c503b-8e57-401e-ae7f-957b515f0532"},"content":[{"type":"paragraph","attrs":{"id":"3ba7ab55-de74-4070-a472-cb0ee0e961d7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"归一化处理"},{"type":"text","text":":将价格缩放到0-1区间。这能加速模型收敛,避免大数值主导计算。"}]},{"type":"codeBlock","attrs":{"id":"650f7e06-e7cd-40a4-9ac5-5a2e7fa7abff","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler()\nscaled_data = scaler.fit_transform(data[['Close']])"}]}]},{"type":"listItem","attrs":{"id":"68de8292-4cd2-443d-a0c3-3c962751d7ea"},"content":[{"type":"paragraph","attrs":{"id":"3afc9b50-050e-41ed-be6a-243a8cccd090","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"时间窗口构建"},{"type":"text","text":":用过去60天的数据预测第61天。这相当于教模型识别\"价格走势模式\"。"}]},{"type":"codeBlock","attrs":{"id":"68f32cc6-c0c5-48af-8f20-2b400ef9dfee","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"def create_dataset(data, window=60):\n X, y = [], []\n for i in range(len(data)-window-1):\n X.append(data[i:i+window, 0])\n y.append(data[i+window, 0])\n return np.array(X), np.array(y)"}]}]},{"type":"listItem","attrs":{"id":"fe7a5a36-3882-4e3a-aa7c-31095c1bd566"},"content":[{"type":"paragraph","attrs":{"id":"43804cb4-d1a3-497d-849e-21b6a5d4af7e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"维度调整"},{"type":"text","text":":将数据重塑为LSTM需要的三维格式(样本数, 时间步长, 特征数)。"}]},{"type":"codeBlock","attrs":{"id":"576b7df1-666c-4545-92aa-5da8b8eba73d","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"X_train = X_train.reshape(X_train.shape[0], 60, 1)"}]}]}]},{"type":"heading","attrs":{"id":"efd34a3f-52aa-4017-814b-eeb134fc4572","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"构建LSTM模型"}]},{"type":"heading","attrs":{"id":"939091b7-dab1-4fde-9513-67afd0a48909","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"3.1 模型架构解析"}]},{"type":"bulletList","attrs":{"id":"d5721e42-05fa-4ee9-ac84-7e9537bb4c68","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"6892d570-5cb1-4f72-a78b-b63034c2b87d"},"content":[{"type":"paragraph","attrs":{"id":"ccab5e1a-c306-4a6f-a052-6453a2235266","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"第一层LSTM"},{"type":"text","text":":50个神经元,返回完整序列供下一层使用"}]}]},{"type":"listItem","attrs":{"id":"abae6d9b-4424-4dd8-ad81-daac5c0c090b"},"content":[{"type":"paragraph","attrs":{"id":"8802b140-5d30-4672-9e9b-a4f228d35892","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"第二层LSTM"},{"type":"text","text":":进一步提取特征,仅返回最终输出"}]}]},{"type":"listItem","attrs":{"id":"7aef7a74-2d93-4121-ba41-59c26635e6f6"},"content":[{"type":"paragraph","attrs":{"id":"2e65531f-fc8c-4d45-bb1f-ffa411e656f2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"全连接层"},{"type":"text","text":":将输出映射到预测值"}]}]}]},{"type":"codeBlock","attrs":{"id":"1bcfea77-f8be-4c5e-ae51-32dad8302004","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"model = Sequential()\nmodel.add(LSTM(50, return_sequences=True, input_shape=(60, 1)))\nmodel.add(LSTM(50))\nmodel.add(Dense(1))"}]},{"type":"heading","attrs":{"id":"3b2aa91c-0fb0-4f35-b2ba-b52047398b90","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"3.2 模型训练要点"}]},{"type":"paragraph","attrs":{"id":"1d624acf-e3e0-4bc6-a1fa-1bbd912b84c3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"使用Adam优化器和均方误差损失函数:"}]},{"type":"bulletList","attrs":{"id":"c888c362-ada8-4e92-ae1a-5f185b9ba1c5","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"43325a80-36d9-470b-a866-a072a4bf55b7"},"content":[{"type":"paragraph","attrs":{"id":"76ef499d-369c-4a89-9851-edd046960cbb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Epochs"},{"type":"text","text":":遍历数据集的次数,并非越大越好(可能过拟合)"}]}]},{"type":"listItem","attrs":{"id":"12004e36-4292-4925-b1df-030787031091"},"content":[{"type":"paragraph","attrs":{"id":"3376d123-535d-429d-8c37-ad3a47651ab8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Batch Size"},{"type":"text","text":":每次参数更新使用的样本数,影响训练速度和内存占用"}]}]}]},{"type":"codeBlock","attrs":{"id":"7af587a4-b2bb-4042-ab7c-ce7de15b3058","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"model.compile(optimizer='adam', loss='mse')\nmodel.fit(X_train, y_train, epochs=20, batch_size=64)"}]},{"type":"heading","attrs":{"id":"426de64e-7253-4e27-b162-47998a2b3ea4","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"四、预测效果可视化:眼见为实"}]},{"type":"heading","attrs":{"id":"616c0bd3-aaf1-4044-8bd2-dd11d034d75b","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"4.1 训练集拟合效果"}]},{"type":"image","attrs":{"id":"e2a651e2-5399-46f3-855c-b4d1ac8fc148","src":"https://developer.qcloudimg.com/editor/image/5421023/20250324-e27ec2dc.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":1100,"aspectRatio":"2.094595","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"paragraph","attrs":{"id":"8a68a0c5-446d-4413-8699-fb71e09eee5f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"虽然预测曲线(橙色)能紧跟实际价格(蓝色),但要警惕"},{"type":"text","marks":[{"type":"bold"}],"text":"过拟合"},{"type":"text","text":"——模型可能只是记住了训练数据,而非学习到通用规律。"}]},{"type":"heading","attrs":{"id":"25c9b1a8-8301-48a4-98f0-7f06dc1a0e26","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"4.2 未来30天预测"}]},{"type":"paragraph","attrs":{"id":"6e38508a-ece5-4d42-a83c-8ed3794501e8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"预测结果呈现平缓上升趋势,这反映模型的两个特性:"}]},{"type":"orderedList","attrs":{"id":"e5ef1d32-ef0c-41d6-9c46-a0a1069df83a","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"d69deb16-9ba8-4fce-a09f-7c09a3ea7509"},"content":[{"type":"paragraph","attrs":{"id":"863f5fbc-6ac6-49d5-8bb6-b8887a2c3eea","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"对近期趋势的延续性判断"}]}]},{"type":"listItem","attrs":{"id":"5db9e093-7c78-4aae-b42a-5db4b49b8577"},"content":[{"type":"paragraph","attrs":{"id":"7b20bd90-26a5-4f98-b620-47609b2e9bd3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"对噪声的平滑处理能力"}]}]}]},{"type":"codeBlock","attrs":{"id":"a5cc4d03-5888-4c58-8805-9be73f030fa8","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"# 预测未来30天价格\nfuture_steps = 30\nlast_60_days = scaled_data[-time_step:] # 获取最后60天的数据\n\n# 初始化输入(三维结构)\nX_test = last_60_days.reshape(1, time_step, 1) # 形状 (1, 60, 1)\n\nfuture_predictions = []\n\n\n\nfor _ in range(future_steps):\n # 预测下一个时间步\n pred = model.predict(X_test) # 输出形状 (1, 1)\n future_predictions.append(pred[0, 0])\n \n # 调整预测值维度并拼接到输入序列\n pred_3d = pred.reshape(1, 1, 1) # 调整为 (1, 1, 1)\n X_test = np.concatenate([X_test[:, 1:, :], pred_3d], axis=1) # 形状保持 (1, 60, 1)"}]},{"type":"image","attrs":{"id":"029e3b32-64c4-49d5-a480-d352cc5256ea","src":"https://developer.qcloudimg.com/editor/image/5421023/20250324-9007ea4f.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":1100,"aspectRatio":"2.039735","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"d7e8aac7-386c-44e5-a494-de78c7e4e2fd","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"五、理想与现实的差距"}]},{"type":"heading","attrs":{"id":"70172d17-7509-4703-a09d-6f83470f57e2","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"5.1 模型的局限性"}]},{"type":"bulletList","attrs":{"id":"f7dbc546-cab6-473c-948a-75fe4686e4f2","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"2258a3b5-c312-4bb0-90b1-05643b3b498b"},"content":[{"type":"paragraph","attrs":{"id":"0a5b88b3-c64c-431a-bc4b-5ae02028f3dc","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"黑天鹅事件"},{"type":"text","text":":新冠疫情等突发事件无法预测"}]}]},{"type":"listItem","attrs":{"id":"2bf562a4-68cb-466e-a6b1-8e28d0ea52ba"},"content":[{"type":"paragraph","attrs":{"id":"96521b06-4f43-4b52-a30c-967c01fe32ca","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"市场有效性"},{"type":"text","text":":公开信息已反映在股价中"}]}]},{"type":"listItem","attrs":{"id":"39fe406e-8424-462b-94a9-a129a47c3fec"},"content":[{"type":"paragraph","attrs":{"id":"f783d627-9b8a-45db-9511-cbe3f6d71082","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"交易成本"},{"type":"text","text":":预测准确率需超过手续费才有利可图"}]}]}]},{"type":"heading","attrs":{"id":"72724d2c-0ae6-4ba8-8c02-3595f47dae86","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"预测未来,不如理解当下"}]},{"type":"paragraph","attrs":{"id":"aa52f08e-9b2b-461c-9528-429a5365ee86","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"通过实践,我们不仅学会了一个LSTM模型的搭建方法,更重要的是理解到:在金融市场中,预测的终极价值不在于精准猜中价格,而是通过建模过程洞察市场运作的规律。当你在JupyterLab中看到那条蜿蜒的预测曲线时,它不仅是代码运行的产物,更是人类智慧与机器学习的交响乐章。或许真正的投资智慧,就藏在这种人机协作的探索过程中。"}]},{"type":"paragraph","attrs":{"id":"b626b0c5-84d5-4892-8b2d-fd48329ab046","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"(提示:完整代码如下:)"}]},{"type":"codeBlock","attrs":{"id":"60f7e7ae-b2b6-4b3f-947d-441afe9f07c7","language":"python","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"python"},"content":[{"type":"text","text":"import numpy as np\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport yfinance as yf\nfrom sklearn.preprocessing import MinMaxScaler\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, LSTM\n\n# 获取股票数据\nticker = 'AAPL'\ndata = yf.download(ticker, start='2010-01-01', end='2025-01-01')\n\n# 查看数据\nprint(data.head())\n\n# 选择收盘价列\ndf = data[['Close']]\n\n# 可视化收盘价\nplt.figure(figsize=(14,5))\n\nplt.plot(df['Close'])\n\nplt.title(f'{ticker} Stock Price')\nplt.xlabel('Date')\nplt.ylabel('Price (USD)')\nplt.show()\n\n# 归一化\nscaler = MinMaxScaler(feature_range=(0, 1))\nscaled_data = scaler.fit_transform(df)\n\n# 查看归一化后的数据\nprint(scaled_data[:5])\n\n# 创建训练数据集\ndef create_dataset(data, time_step=60):\n X, y = [], []\n for i in range(len(data) - time_step - 1):\n X.append(data[i:(i + time_step), 0])\n\n y.append(data[i + time_step, 0])\n return np.array(X), np.array(y)\n\ntime_step = 60\nX_train, y_train = create_dataset(scaled_data, time_step)\n\n\n# 重塑输入数据为 [samples, time steps, features]\nX_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)\n\nprint(X_train.shape, y_train.shape)\n\n# 构建LSTM模型\nmodel = Sequential()\nmodel.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1)))\nmodel.add(LSTM(50, return_sequences=False))\nmodel.add(Dense(25))\nmodel.add(Dense(1))\n\n# 编译模型\nmodel.compile(optimizer='adam', loss='mean_squared_error')\n\n# 查看模型结构\nmodel.summary()\n\n\n# 训练模型\n\nmodel.fit(X_train, y_train, batch_size=64, epochs=20)\n\n# 预测训练集\ntrain_predict = model.predict(X_train)\n\n# 反归一化\ntrain_predict = scaler.inverse_transform(train_predict)\n\ny_train = scaler.inverse_transform(y_train.reshape(-1, 1))\n\n# 可视化预测结果\nplt.figure(figsize=(14,5))\nplt.plot(y_train, label='Actual Price')\nplt.plot(train_predict, label='Predicted Price')\nplt.title(f'{ticker} Stock Price Prediction')\nplt.xlabel('Time')\nplt.ylabel('Price (USD)')\nplt.legend()\nplt.show()\n\n# 预测未来30天价格\nfuture_steps = 30\nlast_60_days = scaled_data[-time_step:] # 获取最后60天的数据\n\n# 初始化输入(三维结构)\nX_test = last_60_days.reshape(1, time_step, 1) # 形状 (1, 60, 1)\n\nfuture_predictions = []\n\nfor _ in range(future_steps):\n # 预测下一个时间步\n pred = model.predict(X_test) # 输出形状 (1, 1)\n future_predictions.append(pred[0, 0])\n \n # 调整预测值维度并拼接到输入序列\n pred_3d = pred.reshape(1, 1, 1) # 调整为 (1, 1, 1)\n X_test = np.concatenate([X_test[:, 1:, :], pred_3d], axis=1) # 形状保持 (1, 60, 1)\n\n\n# 反归一化\nfuture_predictions = scaler.inverse_transform(np.array(future_predictions).reshape(-1, 1))\n\n\n\n# 可视化\nplt.figure(figsize=(14,5))\nplt.plot(future_predictions, label='Future Predictions')\nplt.title(f'{ticker} Future Stock Price Prediction')\nplt.xlabel('Time')\nplt.ylabel('Price (USD)')\nplt.legend()\nplt.show()"}]},{"type":"paragraph","attrs":{"id":"18e21c93-2b8d-4b51-a44d-4f6665361ae4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}}]}