[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-JordiCorbilla--stock-prediction-deep-neural-learning":3,"tool-JordiCorbilla--stock-prediction-deep-neural-learning":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",158594,2,"2026-04-16T23:34:05",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[52,13,15,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":32,"last_commit_at":59,"category_tags":60,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":77,"owner_email":78,"owner_twitter":79,"owner_website":80,"owner_url":81,"languages":82,"stars":91,"forks":92,"last_commit_at":93,"license":94,"difficulty_score":32,"env_os":95,"env_gpu":96,"env_ram":97,"env_deps":98,"category_tags":105,"github_topics":106,"view_count":32,"oss_zip_url":78,"oss_zip_packed_at":78,"status":17,"created_at":124,"updated_at":125,"faqs":126,"releases":161},8296,"JordiCorbilla\u002Fstock-prediction-deep-neural-learning","stock-prediction-deep-neural-learning","Predicting stock prices using a TensorFlow LSTM (long short-term memory) neural network for times series forecasting","stock-prediction-deep-neural-learning 是一个基于深度学习的开源项目，旨在利用人工智能技术预测股票价格走势。面对股市受多重因素影响、波动难以捉摸的挑战，该项目通过机器学习自动识别历史数据中的潜在规律，为投资者提供辅助决策参考。\n\n它主要解决了传统分析方法难以处理复杂时间序列数据的痛点。项目核心采用了 TensorFlow 框架下的 LSTM（长短期记忆）神经网络。作为一种特殊的循环神经网络（RNN），LSTM 具备独特的“记忆”机制，能够长时间保留关键信息，非常适合处理像股价这样具有前后依赖关系的时间序列数据，从而提升预测的准确性。此外，项目集成了 yFinance 库，可便捷地从雅虎财经获取实时市场数据，降低了数据收集的门槛。\n\n这款工具特别适合具备一定 Python 基础的开发者、量化交易研究人员以及对 AI 金融应用感兴趣的技术爱好者使用。用户只需配置好 Conda 或 pip 环境，即可快速复现模型并进行实验。虽然它能挖掘数据模式并带来潜在的投资洞察，但使用者仍需注意股市风险，将其作为研究辅助而非绝对的盈利保证。","# Stock prediction using deep neural learning\n\nPredicting stock prices can be a challenging task as it often does not follow any specific pattern. However, deep neural learning can be used to identify patterns through machine learning. One of the most effective techniques for series forecasting is using LSTM (long short-term memory) networks, which are a type of recurrent neural network (RNN) capable of remembering information over a long period of time. This makes them extremely useful for predicting stock prices.\n\nThis TensorFlow implementation of an LSTM neural network can be used for time series forecasting. **Successful prediction of a stock's future price can yield significant profits for investors**.\n\n## Quickstart (Conda)\n\nThis project uses Python 3.12 and TensorFlow 2.18.1.\n\n```cmd\nconda env create -f environment.yml\nconda activate stock-prediction\n```\n\nIf you prefer pip inside an existing env:\n\n```cmd\npip install -r requirements.txt\n```\n\nJupyter users should select the `stock-prediction` kernel. If it does not appear:\n\n```cmd\npython -m ipykernel install --user --name stock-prediction --display-name \"stock-prediction\"\n```\n\n## 1) Introduction\n\nPredicting stock prices is a complex task, as it is influenced by various factors such as market trends, political events, and economic indicators. The fluctuations in the stock prices are driven by the forces of supply and demand, which can be unpredictable at times. To identify patterns and trends in stock prices, deep learning techniques can be used for machine learning. Long short-term memory ([LSTM](https:\u002F\u002Fwww.researchgate.net\u002Fpublication\u002F13853244_Long_Short-term_Memory)) is a type of recurrent neural network (RNN) that is specifically designed for sequence modeling and prediction. LSTM is capable of retaining information over an extended period of time, making it an ideal approach for predicting stock prices. As a result, RNNs are well-suited to time series data, where they process data step-by-step, maintaining an internal state where they store the information they have seen so far in a compressed form. Accurate prediction of a stock's future price can provide significant financial gain to investors.\n\n## 2) Stock Market Data\n\nTo gather the necessary market data for our stock prediction model, we will utilize the [yFinance](https:\u002F\u002Faroussi.com\u002Fpost\u002Fpython-yahoo-finance) library in Python. This library is designed specifically for downloading relevant information on a given [ticker symbol](https:\u002F\u002Fwww.investopedia.com\u002Fterms\u002Ft\u002Ftickersymbol.asp) from the [Yahoo Finance](https:\u002F\u002Ffinance.yahoo.com\u002Fquote\u002FGOOG?p=GOOG) Finance webpage. By using yFinance, we can easily access the latest market data and incorporate it into our model.\n\nFor our purposes, we will be using the ticker symbol \"GOOG\", which is a well-known technology company. Here's an example screenshot of the ticker symbol on the Yahoo Finance page:\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_6e0d4b1a5912.png)\n\n### 2.1) Market Info Download\n\nTo download the data info, we will need the `yFinance` library installed and then we will only need to perform the following operation to download all the relevant information of a given Stock using its ticker symbol. Please make sure you use the latest version of the library (`pip install yfinance==0.2.54`) as I have seen errors with previous versions.\n\nBelow is the output from the [download_market_data_info.py] file that is able to download financial data from Yahoo Finance. \n\n```cmd\nC:\\Users\\thund\\Source\\Repos\\stock-prediction-deep-neural-learning>python download_market_data_info.py\nInfo\n{\n    \"52WeekChange\": 0.26037383,\n    \"SandP52WeekChange\": 0.034871936,\n    \"address1\": \"1600 Amphitheatre Parkway\",\n    \"algorithm\": null,\n    \"annualHoldingsTurnover\": null,\n    \"annualReportExpenseRatio\": null,\n    \"ask\": 1432.77,\n    \"askSize\": 1400,\n    \"averageDailyVolume10Day\": 2011171,\n    \"averageVolume\": 1857809,\n    \"averageVolume10days\": 2011171,\n    \"beta\": 1.068946,\n    \"beta3Year\": null,\n    \"bid\": 1432.16,\n    \"bidSize\": 3000,\n    \"bookValue\": 297.759,\n    \"category\": null,\n    \"circulatingSupply\": null,\n    \"city\": \"Mountain View\",\n    \"companyOfficers\": [],\n    \"country\": \"United States\",\n    \"currency\": \"USD\",\n    \"dateShortInterest\": 1592179200,\n    \"dayHigh\": 1441.19,\n    \"dayLow\": 1409.82,\n    \"dividendRate\": null,\n    \"dividendYield\": null,\n    \"earningsQuarterlyGrowth\": 0.027,\n    \"enterpriseToEbitda\": 17.899,\n    \"enterpriseToRevenue\": 5.187,\n    \"enterpriseValue\": 864533741568,\n    \"exDividendDate\": null,\n    \"exchange\": \"NMS\",\n    \"exchangeTimezoneName\": \"America\u002FNew_York\",\n    \"exchangeTimezoneShortName\": \"EDT\",\n    \"expireDate\": null,\n    \"fiftyDayAverage\": 1417.009,\n    \"fiftyTwoWeekHigh\": 1532.106,\n    \"fiftyTwoWeekLow\": 1013.536,\n    \"fiveYearAverageReturn\": null,\n    \"fiveYearAvgDividendYield\": null,\n    \"floatShares\": 613293304,\n    \"forwardEps\": 55.05,\n    \"forwardPE\": 26.028149,\n    \"fromCurrency\": null,\n    \"fullTimeEmployees\": 123048,\n    \"fundFamily\": null,\n    \"fundInceptionDate\": null,\n    \"gmtOffSetMilliseconds\": \"-14400000\",\n    \"heldPercentInsiders\": 0.05746,\n    \"heldPercentInstitutions\": 0.7062,\n    \"industry\": \"Internet Content & Information\",\n    \"isEsgPopulated\": false,\n    \"lastCapGain\": null,\n    \"lastDividendValue\": null,\n    \"lastFiscalYearEnd\": 1577750400,\n    \"lastMarket\": null,\n    \"lastSplitDate\": 1430092800,\n    \"lastSplitFactor\": \"10000000:10000000\",\n    \"legalType\": null,\n    \"longBusinessSummary\": \"Alphabet Inc. provides online advertising services in the United States, Europe, the Middle East, Africa, the Asia-Pacific, Canada, and Latin America. It offers performance and brand advertising services. The company operates through Google and Other Bets segments. The Google segment offers products, such as Ads, Android, Chrome, Google Cloud, Google Maps, Google Play, Hardware, Search, and YouTube, as well as technical infrastructure. It also offers digital content, cloud services, hardware devices, and other miscellaneous products and services. The Other Bets segment includes businesses, including Access, Calico, CapitalG, GV, Verily, Waymo, and X, as well as Internet and television services. Alphabet Inc. was founded in 1998 and is headquartered in Mountain View, California.\",\n    \"longName\": \"Alphabet Inc.\",\n    \"market\": \"us_market\",\n    \"marketCap\": 979650805760,\n    \"maxAge\": 1,\n    \"maxSupply\": null,\n    \"messageBoardId\": \"finmb_29096\",\n    \"morningStarOverallRating\": null,\n    \"morningStarRiskRating\": null,\n    \"mostRecentQuarter\": 1585612800,\n    \"navPrice\": null,\n    \"netIncomeToCommon\": 34522001408,\n    \"nextFiscalYearEnd\": 1640908800,\n    \"open\": 1411.1,\n    \"openInterest\": null,\n    \"payoutRatio\": 0,\n    \"pegRatio\": 4.38,\n    \"phone\": \"650-253-0000\",\n    \"previousClose\": 1413.61,\n    \"priceHint\": 2,\n    \"priceToBook\": 4.812112,\n    \"priceToSalesTrailing12Months\": 5.87754,\n    \"profitMargins\": 0.20712,\n    \"quoteType\": \"EQUITY\",\n    \"regularMarketDayHigh\": 1441.19,\n    \"regularMarketDayLow\": 1409.82,\n    \"regularMarketOpen\": 1411.1,\n    \"regularMarketPreviousClose\": 1413.61,\n    \"regularMarketPrice\": 1411.1,\n    \"regularMarketVolume\": 1084440,\n    \"revenueQuarterlyGrowth\": null,\n    \"sector\": \"Communication Services\",\n    \"sharesOutstanding\": 336161984,\n    \"sharesPercentSharesOut\": 0.0049,\n    \"sharesShort\": 3371476,\n    \"sharesShortPreviousMonthDate\": 1589500800,\n    \"sharesShortPriorMonth\": 3462105,\n    \"shortName\": \"Alphabet Inc.\",\n    \"shortPercentOfFloat\": null,\n    \"shortRatio\": 1.9,\n    \"startDate\": null,\n    \"state\": \"CA\",\n    \"strikePrice\": null,\n    \"symbol\": \"GOOG\",\n    \"threeYearAverageReturn\": null,\n    \"toCurrency\": null,\n    \"totalAssets\": null,\n    \"tradeable\": false,\n    \"trailingAnnualDividendRate\": null,\n    \"trailingAnnualDividendYield\": null,\n    \"trailingEps\": 49.572,\n    \"trailingPE\": 28.904415,\n    \"twoHundredDayAverage\": 1352.9939,\n    \"volume\": 1084440,\n    \"volume24Hr\": null,\n    \"volumeAllCurrencies\": null,\n    \"website\": \"http:\u002F\u002Fwww.abc.xyz\",\n    \"yield\": null,\n    \"ytdReturn\": null,\n    \"zip\": \"94043\"\n}\n\nISIN\n-\n\nMajor Holders\n        0                                      1\n0   5.75%        % of Shares Held by All Insider\n1  70.62%       % of Shares Held by Institutions\n2  74.93%        % of Float Held by Institutions\n3    3304  Number of Institutions Holding Shares\n\nInstitutional Holders\n                            Holder    Shares Date Reported   % Out        Value\n0       Vanguard Group, Inc. (The)  23162950    2020-03-30  0.0687  26934109889\n1                   Blackrock Inc.  20264225    2020-03-30  0.0601  23563443472\n2    Price (T.Rowe) Associates Inc  12520058    2020-03-30  0.0371  14558448642\n3         State Street Corporation  11814026    2020-03-30  0.0350  13737467573\n4                         FMR, LLC   8331868    2020-03-30  0.0247   9688379429\n5  Capital International Investors   4555880    2020-03-30  0.0135   5297622822\n6    Geode Capital Management, LLC   4403934    2020-03-30  0.0131   5120938494\n7       Northern Trust Corporation   4017009    2020-03-30  0.0119   4671018235\n8        JP Morgan Chase & Company   3707376    2020-03-30  0.0110   4310973886\n9          AllianceBernstein, L.P.   3483382    2020-03-30  0.0103   4050511423\n\nDividents\nSeries([], Name: Dividends, dtype: int64)\n\nSplits\nDate\n2014-03-27    2.002\n2015-04-27    1.000\nName: Stock Splits, dtype: float64\n\nActions\n            Dividends  Stock Splits\nDate\n2014-03-27        0.0         2.002\n2015-04-27        0.0         1.000\n\nCalendar\nEmpty DataFrame\nColumns: []\nIndex: [Earnings Date, Earnings Average, Earnings Low, Earnings High, Revenue Average, Revenue Low, Revenue High]\n\nRecommendations\n                                         Firm    To Grade    From Grade Action\nDate\n2012-03-14 15:28:00                Oxen Group        Hold                 init\n2012-03-28 06:29:00                 Citigroup         Buy                 main\n2012-04-03 08:45:00  Global Equities Research  Overweight                 main\n2012-04-05 06:34:00             Deutsche Bank         Buy                 main\n2012-04-09 06:03:00          Pivotal Research         Buy                 main\n2012-04-10 11:32:00                       UBS         Buy                 main\n2012-04-13 06:16:00             Deutsche Bank         Buy                 main\n2012-04-13 06:18:00                 Jefferies         Buy                 main\n2012-04-13 06:37:00              PiperJaffray  Overweight                 main\n2012-04-13 06:38:00             Goldman Sachs     Neutral                 main\n2012-04-13 06:41:00                 JP Morgan  Overweight                 main\n2012-04-13 06:51:00               Oppenheimer  Outperform                 main\n2012-04-13 07:13:00                 Benchmark        Hold                 main\n2012-04-13 08:46:00               BMO Capital  Outperform                 main\n2012-04-16 06:52:00            Hilliard Lyons         Buy                 main\n2012-06-06 06:17:00             Deutsche Bank         Buy                 main\n2012-06-06 06:56:00                 JP Morgan  Overweight                 main\n2012-06-22 06:15:00                 Citigroup         Buy                 main\n2012-07-13 05:57:00                   Wedbush     Neutral                 init\n2012-07-17 09:33:00                            Outperform                 main\n2012-07-20 06:43:00                 Benchmark        Hold                 main\n2012-07-20 06:54:00             Deutsche Bank         Buy                 main\n2012-07-20 06:59:00           Bank of America         Buy                 main\n2012-08-13 05:49:00            Morgan Stanley  Overweight  Equal-Weight     up\n2012-09-17 06:07:00  Global Equities Research  Overweight                 main\n2012-09-21 06:28:00         Cantor Fitzgerald         Buy                 init\n2012-09-24 06:11:00                 Citigroup         Buy                 main\n2012-09-24 09:05:00          Pivotal Research         Buy                 main\n2012-09-25 07:20:00                  Capstone         Buy                 main\n2012-09-26 05:48:00         Canaccord Genuity         Buy                 main\n...                                       ...         ...           ...    ...\n2017-10-27 19:29:31                       UBS         Buy                 main\n2018-02-02 14:04:52              PiperJaffray  Overweight    Overweight   main\n2018-04-24 11:43:49                 JP Morgan  Overweight    Overweight   main\n2018-04-24 12:24:37             Deutsche Bank         Buy           Buy   main\n2018-05-05 14:00:37              B. Riley FBR         Buy                 main\n2018-07-13 13:49:13               Cowen & Co.  Outperform    Outperform   main\n2018-07-24 11:50:55               Cowen & Co.  Outperform    Outperform   main\n2018-07-24 13:33:47             Raymond James  Outperform    Outperform   main\n2018-10-23 11:18:00             Deutsche Bank         Buy           Buy   main\n2018-10-26 15:17:08             Raymond James  Outperform    Outperform   main\n2019-01-23 12:55:04             Deutsche Bank         Buy           Buy   main\n2019-02-05 12:55:12             Deutsche Bank         Buy           Buy   main\n2019-02-05 13:18:47              PiperJaffray  Overweight    Overweight   main\n2019-05-15 12:34:54             Deutsche Bank         Buy                 main\n2019-10-23 12:58:59             Credit Suisse  Outperform                 main\n2019-10-29 11:58:09             Raymond James  Outperform                 main\n2019-10-29 14:15:40             Deutsche Bank         Buy                 main\n2019-10-29 15:48:29                       UBS         Buy                 main\n2020-01-06 11:22:07          Pivotal Research         Buy          Hold     up\n2020-01-17 13:01:48                       UBS         Buy                 main\n2020-02-04 12:26:56             Piper Sandler  Overweight                 main\n2020-02-04 12:41:00             Raymond James  Outperform                 main\n2020-02-04 14:00:36             Deutsche Bank         Buy                 main\n2020-02-06 11:34:20                      CFRA  Strong Buy                 main\n2020-03-18 13:52:51                 JP Morgan  Overweight                 main\n2020-03-30 13:26:16                       UBS         Buy                 main\n2020-04-17 13:01:41               Oppenheimer  Outperform                 main\n2020-04-20 19:29:50             Credit Suisse  Outperform                 main\n2020-04-29 14:01:51                       UBS         Buy                 main\n2020-05-05 12:44:16             Deutsche Bank         Buy                 main\n\n[219 rows x 4 columns]\n\nEarnings\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nQuarterly Earnings\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nFinancials\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nQuarterly Financials\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nBalance Sheet\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nQuarterly Balance Sheet\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nBalancesheet\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nQuarterly Balancesheet\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nCashflow\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nQuarterly Cashflow\nEmpty DataFrame\nColumns: [Open, High, Low, Close, Adj Close, Volume]\nIndex: []\n\nSustainability\nNone\n\nOptions\n('2020-07-02', '2020-07-10', '2020-07-17', '2020-07-24', '2020-07-31', '2020-08-07', '2020-08-21', '2020-09-18', '2020-11-20', '2020-12-01', '2020-12-18', '2021-01-15', '2021-06-18', '2022-01-21', '2022-06-17')\n```\n\nThe data has a JSON document that we could use later on to create our Security Master if we ever wanted to store this data somewhere to keep track of the Securities we are going to trade with. As the data could come with different fields, my suggestion is to store them on a Data Lake so we can build it from multiple sources without having to worry too much about the way the data is structured.\n\n### 2.2) Market Data Download\n\nThe previous step helps us to identify several characteristics of a given ticker symbol so we can use its properties to define some of the charts I'm showing below. Note that the yFinance library only requires the stock to download via ticker symbol, the start date and end date of the period we want to get. Additionally, we can also specify the granularity of the data using the interval parameter. By default, the interval is 1 day and this is the one I will use for my training.\n\nTo download the data we can use the following command:\n\n```python\nstart = pd.to_datetime('2004-08-01')\nstock = ['GOOG']\ndata = yf.download(stock, start=start, end=datetime.date.today())\nprint(data)\n```\n\nAnd the sample output:\n\n```cmd\nC:\\Users\\thund\\Source\\Repos\\stock-prediction-deep-neural-learning>python download_market_data.py\n[*********************100%***********************]  1 of 1 completed\n                   Open         High          Low        Close    Adj Close    Volume\nDate\n2004-08-19    49.813286    51.835709    47.800831    49.982655    49.982655  44871300\n2004-08-20    50.316402    54.336334    50.062355    53.952770    53.952770  22942800\n2004-08-23    55.168217    56.528118    54.321388    54.495735    54.495735  18342800\n2004-08-24    55.412300    55.591629    51.591621    52.239193    52.239193  15319700\n2004-08-25    52.284027    53.798351    51.746044    52.802086    52.802086   9232100\n2004-08-26    52.279045    53.773445    52.134586    53.753517    53.753517   7128600\n2004-08-27    53.848164    54.107193    52.647663    52.876804    52.876804   6241200\n2004-08-30    52.443428    52.548038    50.814533    50.814533    50.814533   5221400\n2004-08-31    50.958992    51.661362    50.889256    50.993862    50.993862   4941200\n2004-09-01    51.158245    51.292744    49.648903    49.937820    49.937820   9181600\n2004-09-02    49.409801    50.993862    49.285267    50.565468    50.565468  15190400\n2004-09-03    50.286514    50.680038    49.474556    49.818268    49.818268   5176800\n2004-09-07    50.316402    50.809555    49.619015    50.600338    50.600338   5875200\n2004-09-08    50.181908    51.322632    50.062355    50.958992    50.958992   5009200\n2004-09-09    51.073563    51.163227    50.311420    50.963974    50.963974   4080900\n2004-09-10    50.610302    53.081039    50.460861    52.468334    52.468334   8740200\n2004-09-13    53.115910    54.002586    53.031227    53.549286    53.549286   7881300\n2004-09-14    53.524376    55.790882    53.195610    55.536835    55.536835  10880300\n2004-09-15    55.073570    56.901718    54.894241    55.790882    55.790882  10763900\n2004-09-16    55.960247    57.683788    55.616535    56.772205    56.772205   9310200\n2004-09-17    56.996365    58.525631    56.562988    58.525631    58.525631   9517400\n2004-09-20    58.256641    60.572956    58.166977    59.457142    59.457142  10679200\n2004-09-21    59.681301    59.985161    58.535595    58.699978    58.699978   7263000\n2004-09-22    58.480801    59.611561    58.186901    58.968971    58.968971   7617100\n2004-09-23    59.198112    61.086033    58.291508    60.184414    60.184414   8576100\n2004-09-24    60.244190    61.818291    59.656395    59.691261    59.691261   9166700\n2004-09-27    59.556767    60.214302    58.680054    58.909195    58.909195   7099600\n2004-09-28    60.423519    63.462128    59.880554    63.193138    63.193138  17009400\n2004-09-29    63.113434    67.257904    62.879314    65.295258    65.295258  30661400\n2004-09-30    64.707458    65.902977    64.259140    64.558022    64.558022  13823300\n...                 ...          ...          ...          ...          ...       ...\n2020-05-19  1386.996948  1392.000000  1373.484985  1373.484985  1373.484985   1280600\n2020-05-20  1389.579956  1410.420044  1387.250000  1406.719971  1406.719971   1655400\n2020-05-21  1408.000000  1415.489990  1393.449951  1402.800049  1402.800049   1385000\n2020-05-22  1396.709961  1412.760010  1391.829956  1410.420044  1410.420044   1309400\n2020-05-26  1437.270020  1441.000000  1412.130005  1417.020020  1417.020020   2060600\n2020-05-27  1417.250000  1421.739990  1391.290039  1417.839966  1417.839966   1685800\n2020-05-28  1396.859985  1440.839966  1396.000000  1416.729980  1416.729980   1692200\n2020-05-29  1416.939941  1432.569946  1413.349976  1428.920044  1428.920044   1838100\n2020-06-01  1418.390015  1437.959961  1418.000000  1431.819946  1431.819946   1217100\n2020-06-02  1430.550049  1439.609985  1418.829956  1439.219971  1439.219971   1278100\n2020-06-03  1438.300049  1446.552002  1429.776978  1436.380005  1436.380005   1256200\n2020-06-04  1430.400024  1438.959961  1404.729980  1412.180054  1412.180054   1484300\n2020-06-05  1413.170044  1445.050049  1406.000000  1438.390015  1438.390015   1734900\n2020-06-08  1422.339966  1447.989990  1422.339966  1446.609985  1446.609985   1404200\n2020-06-09  1445.359985  1468.000000  1443.209961  1456.160034  1456.160034   1409200\n2020-06-10  1459.540039  1474.259033  1456.270020  1465.849976  1465.849976   1525200\n2020-06-11  1442.479980  1454.474976  1402.000000  1403.839966  1403.839966   1991300\n2020-06-12  1428.489990  1437.000000  1386.020020  1413.180054  1413.180054   1944200\n2020-06-15  1390.800049  1424.800049  1387.920044  1419.849976  1419.849976   1503900\n2020-06-16  1445.219971  1455.020020  1425.900024  1442.719971  1442.719971   1709200\n2020-06-17  1447.160034  1460.000000  1431.380005  1451.119995  1451.119995   1548300\n2020-06-18  1449.160034  1451.410034  1427.010010  1435.959961  1435.959961   1581900\n2020-06-19  1444.000000  1447.800049  1421.349976  1431.719971  1431.719971   3157900\n2020-06-22  1429.000000  1452.750000  1423.209961  1451.859985  1451.859985   1542400\n2020-06-23  1455.640015  1475.941040  1445.239990  1464.410034  1464.410034   1429800\n2020-06-24  1461.510010  1475.420044  1429.750000  1431.969971  1431.969971   1756000\n2020-06-25  1429.900024  1442.900024  1420.000000  1441.329956  1441.329956   1230500\n2020-06-26  1431.390015  1433.449951  1351.989990  1359.900024  1359.900024   4267700\n2020-06-29  1358.180054  1395.599976  1347.010010  1394.969971  1394.969971   1810200\n2020-06-30  1390.439941  1418.650024  1383.959961  1413.609985  1413.609985   2041600\n\n[3994 rows x 6 columns]\n```\n\nNote that is important to mention the start date correctly just to ensure we are collecting data. If we don't do that we might end up having some NaN variables that could affect the output of our training.\n\n## 3) Deep Learning Model\n\n### 3.1) Training and Validation Data\n\nNow that we have the data that we want to use, we need to define what defines our traning and validation data. As stocks could vary depending on the dates, the function I have created requires 3 basic arguments:\n- Ticker Symbol: **GOOG**\n- Start Date: Date as to when they started, in this case, it was **2004-Aug-01**.\n- Validation Date: Date as to when we want the validation to be considered. In this case, we specify **2017-01-01** as our data point.\n\nNote that you will need to have configured [TensorFlow](https:\u002F\u002Fwww.tensorflow.org\u002F), Keras ,and a GPU in order to run the samples below.\n\nIn this exercise, I'm only interested in the [closing price](https:\u002F\u002Fwww.investopedia.com\u002Fterms\u002Fc\u002Fclosingprice.asp) which is the standard benchmark regarding stocks or securities.\n\nBelow you can find the chart with the division we will create between Training Data and Validation Data:\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_7fc2302117b4.png)\n\nAlso, the histogram showing the distribution of the prices:\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_4b689d25126b.png)\n\n### 3.2) Data Normalization\n\nIn order to normalise the data, we need to scale it between 0 and 1 so we talk on a common scale. To accomplish this, we can use the preprocessing tool [MinMaxScaler](https:\u002F\u002Fscikit-learn.org\u002Fstable\u002Fmodules\u002Fgenerated\u002Fsklearn.preprocessing.MinMaxScaler.html) as seen below:\n\n```python\n    min_max = MinMaxScaler(feature_range=(0, 1))\n    train_scaled = min_max.fit_transform(training_data)\n```\n\n### 3.3) Adding Timesteps\n\nTo implement the LSTM network for time series forecasting, we have used a [time step](http:\u002F\u002Fcolah.github.io\u002Fposts\u002F2015-08-Understanding-LSTMs\u002F) of 3 days. This technique allows the network to look back at the previous 3 days of data to predict the subsequent day. The figure below illustrates how this concept is used in our implementation, where the first 3 samples for Close price generate the 4th sample and so on. This generates a matrix of shape (3,1), with 3 representing the time steps and 1 representing the number of features (Close price).\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_dfceab9df500.png)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_4eb7d69113f7.png)\n\n```python\n# Define the number of time steps\ntime_steps = 3\n\n# Loop through the data to create partitions\nfor i in range(time_steps, train_scaled.shape[0]):\n    # Create a partition of the previous 3 days' data\n    x_train.append(train_scaled[i - time_steps:i])\n\n    # Append the next day's Close price to the label array\n    y_train.append(train_scaled[i, 0])\n```\n\nUsing a time step of 3 days allows the LSTM network to identify patterns in the data and make predictions based on those patterns. By using a sliding window approach, we can train the network to predict future stock prices based on historical data.\n\n### 3.4) Creation of the deep learning model LSTM\n\nTo create this model, you will need to have **TensorFlow**, **TensorFlow-Gpu** and **Keras** installed in order for this to run. The code for this model can be seen below and the explanation for each layer is also defined below:\n\n```python\ndef create_long_short_term_memory_model(x_train):\n    model = Sequential()\n    # 1st layer with Dropout regularisation\n    # * units = add 100 neurons is the dimensionality of the output space\n    # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input\n    # * input_shape => Shape of the training dataset\n    model.add(LSTM(units=100, return_sequences=True, input_shape=(x_train.shape[1], 1)))\n    # 20% of the layers will be dropped\n    model.add(Dropout(0.2))\n    # 2nd LSTM layer\n    # * units = add 50 neurons is the dimensionality of the output space\n    # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input\n    model.add(LSTM(units=50, return_sequences=True))\n    # 20% of the layers will be dropped\n    model.add(Dropout(0.2))\n    # 3rd LSTM layer\n    # * units = add 50 neurons is the dimensionality of the output space\n    # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input\n    model.add(LSTM(units=50, return_sequences=True))\n    # 50% of the layers will be dropped\n    model.add(Dropout(0.5))\n    # 4th LSTM layer\n    # * units = add 50 neurons is the dimensionality of the output space\n    model.add(LSTM(units=50))\n    # 50% of the layers will be dropped\n    model.add(Dropout(0.5))\n    # Dense layer that specifies an output of one unit\n    model.add(Dense(units=1))\n    model.summary()\n    tf.keras.utils.plot_model(model, to_file=os.path.join(project_folder, 'model_lstm.png'), show_shapes=True,\n                              show_layer_names=True)\n    return model\n```\n\nThe rendered model can be seen in the image below, producing a model with more than 100k trainable parameters.\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_29443eefafce.png)\n\n```cmd\nLayer (type)                 Output Shape              Param #\n=================================================================\nlstm_1 (LSTM)                (None, 60, 100)           40800\n_________________________________________________________________\ndropout_1 (Dropout)          (None, 60, 100)           0\n_________________________________________________________________\nlstm_2 (LSTM)                (None, 60, 50)            30200\n_________________________________________________________________\ndropout_2 (Dropout)          (None, 60, 50)            0\n_________________________________________________________________\nlstm_3 (LSTM)                (None, 60, 50)            20200\n_________________________________________________________________\ndropout_3 (Dropout)          (None, 60, 50)            0\n_________________________________________________________________\nlstm_4 (LSTM)                (None, 50)                20200\n_________________________________________________________________\ndropout_4 (Dropout)          (None, 50)                0\n_________________________________________________________________\ndense_1 (Dense)              (None, 1)                 51\n=================================================================\nTotal params: 111,451\nTrainable params: 111,451\nNon-trainable params: 0\n```\n\nOnce we have defined the model, we need to specify the metrics we want to use to track how well our model is behaving and also the kind of optimizer we want to use for our training. I have also defined the patience I want my model to have and what is the rule defined for it.\n\n```python\n    defined_metrics = [\n        tf.keras.metrics.MeanSquaredError(name='MSE')\n    ]\n\n    callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, mode='min', verbose=1)\n\n    model.compile(optimizer='adam', loss='mean_squared_error', metrics=defined_metrics)\n    history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test),\n                        callbacks=[callback])\n```\n\nThis model is slightly fined tuned to reach the lowest validation loss. In this example, we reach a validation loss of 0.14% with an MSE (Mean Square Error) of  0.14% which is relatively good, providing us with a very accurate result.\n\nThe training result can be seen below:\n\n```cmd\nTrain on 3055 samples, validate on 881 samples\nEpoch 1\u002F100\n2020-07-11 15:15:34.557035: I tensorflow\u002Fstream_executor\u002Fplatform\u002Fdefault\u002Fdso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll\n3112\u002F3112 [==============================] - 19s 6ms\u002Fsample - loss: 0.0451 - MSE: 0.0451 - val_loss: 0.0068 - val_MSE: 0.0068\nEpoch 2\u002F100\n3112\u002F3112 [==============================] - 4s 1ms\u002Fsample - loss: 0.0088 - MSE: 0.0088 - val_loss: 0.0045 - val_MSE: 0.0045\nEpoch 3\u002F100\n3112\u002F3112 [==============================] - 5s 1ms\u002Fsample - loss: 0.0062 - MSE: 0.0062 - val_loss: 0.0032 - val_MSE: 0.0032\nEpoch 4\u002F100\n3112\u002F3112 [==============================] - 5s 1ms\u002Fsample - loss: 0.0051 - MSE: 0.0051 - val_loss: 0.0015 - val_MSE: 0.0015\nEpoch 5\u002F100\n3112\u002F3112 [==============================] - 7s 2ms\u002Fsample - loss: 0.0045 - MSE: 0.0045 - val_loss: 0.0013 - val_MSE: 0.0013\nEpoch 6\u002F100\n3112\u002F3112 [==============================] - 5s 2ms\u002Fsample - loss: 0.0045 - MSE: 0.0045 - val_loss: 0.0013 - val_MSE: 0.0013\nEpoch 7\u002F100\n3112\u002F3112 [==============================] - 5s 2ms\u002Fsample - loss: 0.0045 - MSE: 0.0045 - val_loss: 0.0015 - val_MSE: 0.0015\nEpoch 8\u002F100\n3112\u002F3112 [==============================] - 5s 1ms\u002Fsample - loss: 0.0040 - MSE: 0.0040 - val_loss: 0.0015 - val_MSE: 0.0015\nEpoch 9\u002F100\n3112\u002F3112 [==============================] - 5s 1ms\u002Fsample - loss: 0.0039 - MSE: 0.0039 - val_loss: 0.0014 - val_MSE: 0.0014\nEpoch 00009: early stopping\nsaving weights\nplotting loss\nplotting MSE\ndisplay the content of the model\n886\u002F1 - 0s - loss: 0.0029 - MSE: 0.0014\nloss :  0.0014113364930413916\nMSE :  0.0014113366\n```\n\n### 3.5) Making predictions happen\n\nNow it is time to prepare our testing data and send it through our deep-learning model to obtain the predictions we are trying to get.\n\nFirst, we need to import the test data using the same approach we used for the training data using the time steps:\n\n```python\ndef infer_data():\n    print(tf.version.VERSION)\n    inference_folder = os.path.join(os.getcwd(), RUN_FOLDER)\n    stock = StockPrediction(STOCK_TICKER, STOCK_START_DATE, STOCK_VALIDATION_DATE, inference_folder, GITHUB_URL, EPOCHS, TIME_STEPS, TOKEN, BATCH_SIZE)\n\n    data = StockData(stock)\n\n    (x_train, y_train), (x_test, y_test), (training_data, test_data) = data.download_transform_to_numpy(TIME_STEPS, inference_folder)\n    min_max = data.get_min_max()\n\n    # load future data\n    print('Latest Stock Price')\n    latest_close_price = test_data.Close.iloc[-1]\n    latest_date = test_data[-1:]['Close'].idxmin()\n    print(latest_close_price)\n    print('Latest Date')\n    print(latest_date)\n\n    tomorrow_date = latest_date + timedelta(1)\n    # Specify the next 300 days\n    next_year = latest_date + timedelta(TIME_STEPS * 100)\n\n    print('Future Date')\n    print(tomorrow_date)\n\n    print('Future Timespan Date')\n    print(next_year)\n\n    x_test, y_test, test_data = data.generate_future_data(TIME_STEPS, min_max, tomorrow_date, next_year, latest_close_price)\n\n    # Check if the future data is not empty\n    if x_test.shape[0] > 0:\n        # load the weights from our best model\n        model = tf.keras.models.load_model(os.path.join(inference_folder, 'model_weights.h5'))\n        model.summary()\n\n        # perform a prediction\n        test_predictions_baseline = model.predict(x_test)\n        test_predictions_baseline = min_max.inverse_transform(test_predictions_baseline)\n        test_predictions_baseline = pd.DataFrame(test_predictions_baseline, columns=['Predicted_Price'])\n\n        # Combine the predicted values with dates from the test data\n        predicted_dates = pd.date_range(start=test_data.index[0], periods=len(test_predictions_baseline))\n        test_predictions_baseline['Date'] = predicted_dates\n        \n        # Reset the index for proper concatenation\n        test_data.reset_index(inplace=True)\n        \n        # Concatenate the test_data and predicted data\n        combined_data = pd.concat([test_data, test_predictions_baseline], ignore_index=True)\n        \n        # Plotting predictions\n        plt.figure(figsize=(14, 5))\n        plt.plot(combined_data['Date'], combined_data.Close, color='green', label='Simulated [' + STOCK_TICKER + '] price')\n        plt.plot(combined_data['Date'], combined_data['Predicted_Price'], color='red', label='Predicted [' + STOCK_TICKER + '] price')\n        plt.xlabel('Time')\n        plt.ylabel('Price [USD]')\n        plt.legend()\n        plt.title('Simulated vs Predicted Prices')\n        plt.savefig(os.path.join(inference_folder, STOCK_TICKER + '_future_comparison.png'))\n        plt.show()\n    else:\n        print(\"Error: Future data is empty.\")\n\nstart_date = pd.to_datetime('2017-01-01')\nend_date = datetime.today()\nduration = end_date - start_date\nSTOCK_VALIDATION_DATE = start_date + 0.8 * duration\n```\n\nNow we can call the `infer_data` method which will allow us to generate the stock prediction based on the training done over the training data. As a result, we will generate a csv file that contains the result of the prediction and also a chart that shows what's the real vs the estimation. \n\n\u003Cimg width=\"1111\" height=\"444\" alt=\"image\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_e28a4688d391.png\" \u002F>\n\n# 4) Usage\n\nThis has been built using Python 3.12.\n\nDownload the source code and install the dependencies using one of the following:\n\n```cmd\nconda env create -f environment.yml\nconda activate stock-prediction\n```\n\nOr, with pip inside an existing environment:\n\n```cmd\npip install -r requirements.txt\n```\n\nNote: If you change TensorFlow versions, align NumPy to the version constraints for that release.\n\n### 4.1) Default model (v7)\n\nv7 is the default model version. It trains two heads: direction (up\u002Fdown) and magnitude (absolute move), then reconstructs price deltas.\n\nTraining outputs for v7:\n- `model_direction.keras`\n- `model_magnitude.keras`\n- `model_config.json` (includes `model_version`, `forecast_horizon`, `use_returns`, `use_deltas`, `use_trend_residual`)\n- `min_max_scaler.pkl` and `input_scaler.pkl`\n\n### 4.2) Inference (stochastic trajectories)\n\nInference uses `InferenceRunner` from `stock_prediction_deep_learning_inference.py` and can generate multiple stochastic paths:\n- `STOCHASTIC_PATHS`: number of trajectories\n- `STOCHASTIC_SIGMA_MULT`: noise scale multiplier\n- `STOCHASTIC_LOOKBACK`: lookback window for noise estimation\n- `STOCHASTIC_SEED`: RNG seed for reproducibility\n\nOutput includes `future_predictions.csv` with `Predicted_Price`, `Predicted_Price_Raw`, and percentile bands (`Predicted_Price_P10`, `Predicted_Price_P50`, `Predicted_Price_P90`) when stochastic paths are enabled.\n\nThen edit the file \"stock_prediction_deep_learning.py\" to include the Stock you want to use and the relevant dates and execute:\n\n```cmd\n# all arguments are optional\npython stock_prediction_deep_learning.py -ticker=GOOG -start_date=2017-11-01 -validation_date=2022-09-01 -epochs=150 -batch_size=32 -time_steps=30 -use_returns=false -model_version=v7 -forecast_horizon=1\n```\n\nNote: `model_version` defaults to `v7`, so you can omit it if you want the default behavior.\n\nAlso you can use the `Jupyter notebook` available here:\n\nhttps:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fblob\u002Fmaster\u002Fstock_prediction_lstm.ipynb\n\n# 5) CUDA installation\n\nOptional: only needed if you have an NVIDIA GPU. CPU-only runs work without this.\n\n![image](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_83b506a94fcf.png)\n\nAs I mentioned before, I'm using a GPU to ramp up my testing. As my laptop has a nvidia geforce card, I installed CUDA to make use of its GPU capabilities. Depending on your tensorflow version you'll need a version or another.\nHere is the link: https:\u002F\u002Fdeveloper.nvidia.com\u002Fcuda-11.0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal\n\nYou can do from your conda prompt:\n\n```bash\n(base) >conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0\nCollecting package metadata (current_repodata.json): done\nSolving environment: failed with initial frozen solve. Retrying with flexible solve.\nCollecting package metadata (repodata.json): done\nSolving environment: done\n\n\n==> WARNING: A newer version of conda exists. \u003C==\n  current version: 4.10.3\n  latest version: 4.14.0\n\nPlease update conda by running\n\n    $ conda update -n base -c defaults conda\n\n\n\n## Package Plan ##\n\n  environment location: C:\\Users\\jordi\\anaconda3\n\n  added \u002F updated specs:\n    - cudatoolkit=11.2\n    - cudnn=8.1.0\n\n\nThe following packages will be downloaded:\n\n    package                    |            build\n    ---------------------------|-----------------\n    conda-4.12.0               |   py39hcbf5309_0         1.0 MB  conda-forge\n    cudatoolkit-11.2.2         |      h933977f_10       879.9 MB  conda-forge\n    cudnn-8.1.0.77             |       h3e0f4f4_0       610.8 MB  conda-forge\n    python_abi-3.9             |           2_cp39           4 KB  conda-forge\n    ------------------------------------------------------------\n                                           Total:        1.46 GB\n\nThe following NEW packages will be INSTALLED:\n\n  cudatoolkit        conda-forge\u002Fwin-64::cudatoolkit-11.2.2-h933977f_10\n  cudnn              conda-forge\u002Fwin-64::cudnn-8.1.0.77-h3e0f4f4_0\n  python_abi         conda-forge\u002Fwin-64::python_abi-3.9-2_cp39\n\nThe following packages will be UPDATED:\n\n  conda              pkgs\u002Fmain::conda-4.10.3-py39haa95532_0 --> conda-forge::conda-4.12.0-py39hcbf5309_0\n\n\nProceed ([y]\u002Fn)? y\n\n\nDownloading and Extracting Packages\nconda-4.12.0         | 1.0 MB    | ############################################################################ | 100%\npython_abi-3.9       | 4 KB      | ############################################################################ | 100%\ncudnn-8.1.0.77       | 610.8 MB  | ############################################################################ | 100%\ncudatoolkit-11.2.2   | 879.9 MB  | ############################################################################ | 100%\nPreparing transaction: done\nVerifying transaction: done\nExecuting transaction: \u002F \"By downloading and using the CUDA Toolkit conda packages, you accept the terms and conditions of the CUDA End User License Agreement (EULA): https:\u002F\u002Fdocs.nvidia.com\u002Fcuda\u002Feula\u002Findex.html\"\n\n| \"By downloading and using the cuDNN conda packages, you accept the terms and conditions of the NVIDIA cuDNN EULA - https:\u002F\u002Fdocs.nvidia.com\u002Fdeeplearning\u002Fcudnn\u002Fsla\u002Findex.html\"\n\ndone\n```\n\nIf you run the project after this, the GPU should be correctly picked up:\n\n![image](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_35a02ce3ce46.png)\n\n# 6) Graphviz installation\n\nif you see this message, then you need to install GraphViz library:\n\n```bash\nYou must install pydot (`pip install pydot`) and install graphviz (see instructions at https:\u002F\u002Fgraphviz.gitlab.io\u002Fdownload\u002F) for plot_model\u002Fmodel_to_dot to work.\n```\n\n- https:\u002F\u002Fgraphviz.gitlab.io\u002Fdownload\u002F\n\n## Sponsors\nNo sponsors yet! Will you be the first?\n\n[![PayPayl donate button](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Fpaypal-donate-yellow.svg)](https:\u002F\u002Fwww.paypal.com\u002Fpaypalme\u002Fjordicorbilla)\n\n### Support me\n\n\u003Ca href=\"https:\u002F\u002Fwww.buymeacoffee.com\u002Fjordicorbilla\" target=\"_blank\">\u003Cimg src=\"https:\u002F\u002Fcdn.buymeacoffee.com\u002Fbuttons\u002Fdefault-orange.png\" alt=\"Buy Me A Coffee\" height=\"41\" width=\"174\">\u003C\u002Fa>\n","# 基于深度神经网络的股票预测\n\n预测股票价格是一项极具挑战性的任务，因为股价往往不遵循任何特定的规律。然而，通过深度学习技术，我们可以利用机器学习来识别其中的模式。在时间序列预测中，最有效的技术之一就是使用LSTM（长短期记忆）网络。LSTM是一种特殊的循环神经网络（RNN），能够长期记住信息，因此在股票价格预测中非常有用。\n\n本项目提供了一个基于TensorFlow的LSTM神经网络实现，可用于时间序列预测。**成功预测一只股票的未来价格可以为投资者带来可观的收益**。\n\n## 快速入门（Conda）\n\n该项目使用Python 3.12和TensorFlow 2.18.1。\n\n```cmd\nconda env create -f environment.yml\nconda activate stock-prediction\n```\n\n如果你更倾向于在现有环境中使用pip：\n\n```cmd\npip install -r requirements.txt\n```\n\nJupyter用户应选择`stock-prediction`内核。如果该内核未显示，请执行以下命令：\n\n```cmd\npython -m ipykernel install --user --name stock-prediction --display-name \"stock-prediction\"\n```\n\n## 1) 引言\n\n预测股票价格是一项复杂的任务，因为它受到多种因素的影响，例如市场趋势、政治事件和经济指标等。股票价格的波动主要由供需关系驱动，而这种供需关系有时是难以预测的。为了识别股票价格中的模式和趋势，我们可以借助深度学习技术进行机器学习。长短期记忆网络（[LSTM](https:\u002F\u002Fwww.researchgate.net\u002Fpublication\u002F13853244_Long_Short-term_Memory)）是一种专门用于序列建模和预测的循环神经网络。LSTM能够在较长时间内保留信息，因此非常适合用于股票价格预测。由于RNN擅长处理时间序列数据，它们会逐步处理数据，并在内部状态中以压缩形式存储已见过的信息。准确预测一只股票的未来价格可以为投资者带来显著的财务收益。\n\n## 2) 股票市场数据\n\n为了收集我们股票预测模型所需的市场数据，我们将使用Python中的[yFinance](https:\u002F\u002Faroussi.com\u002Fpost\u002Fpython-yahoo-finance)库。该库专门用于从[Yahoo Finance](https:\u002F\u002Ffinance.yahoo.com\u002Fquote\u002FGOOG?p=GOOG)网站下载指定[股票代码](https:\u002F\u002Fwww.investopedia.com\u002Fterms\u002Ft\u002Ftickersymbol.asp)的相关信息。通过yFinance，我们可以轻松获取最新的市场数据并将其纳入我们的模型中。\n\n在本项目中，我们将使用“GOOG”这一股票代码，它代表一家知名的科技公司。以下是Yahoo Finance页面上该股票代码的示例截图：\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_6e0d4b1a5912.png)\n\n### 2.1) 下载市场信息\n\n要下载市场数据，我们需要先安装`yFinance`库，然后只需执行以下操作即可根据股票代码下载所有相关信息。请确保使用最新版本的库（`pip install yfinance==0.2.54`），因为我曾遇到过旧版本出现错误的情况。\n\n以下是来自[download_market_data_info.py]文件的输出，该文件能够从Yahoo Finance下载金融数据。\n\n```cmd\nC:\\Users\\thund\\Source\\Repos\\stock-prediction-deep-neural-learning>python download_market_data_info.py\n信息\n{\n    \"52周变化\": 0.26037383,\n    \"标普52周变化\": 0.034871936,\n    \"地址1\": \"1600 Amphitheatre Parkway\",\n    \"算法\": null,\n    \"年度持股周转率\": null,\n    \"年度报告费用比率\": null,\n    \"卖价\": 1432.77,\n    \"卖量\": 1400,\n    \"10日平均成交量\": 2011171,\n    \"平均成交量\": 1857809,\n    \"10日平均成交量\": 2011171,\n    \"贝塔系数\": 1.068946,\n    \"3年贝塔系数\": null,\n    \"买价\": 1432.16,\n    \"买量\": 3000,\n    \"账面价值\": 297.759,\n    \"类别\": null,\n    \"流通股本\": null,\n    \"城市\": \"Mountain View\",\n    \"公司高管\": [],\n    \"国家\": \"美国\",\n    \"货币\": \"USD\",\n    \"空头持仓数据日期\": 1592179200,\n    \"当日最高价\": 1441.19,\n    \"当日最低价\": 1409.82,\n    \"股息率\": null,\n    \"股息收益率\": null,\n    \"季度收益增长率\": 0.027,\n    \"企业价值与EBITDA比\": 17.899,\n    \"企业价值与收入比\": 5.187,\n    \"企业价值\": 864533741568,\n    \"除息日\": null,\n    \"交易所\": \"NMS\",\n    \"交易所时区名称\": \"America\u002FNew_York\",\n    \"交易所时区简称\": \"EDT\",\n    \"到期日\": null,\n    \"50日均线\": 1417.009,\n    \"52周最高价\": 1532.106,\n    \"52周最低价\": 1013.536,\n    \"5年平均回报率\": null,\n    \"5年平均股息收益率\": null,\n    \"自由流通股数\": 613293304,\n    \"远期每股收益\": 55.05,\n    \"远期市盈率\": 26.028149,\n    \"来源货币\": null,\n    \"全职员工数\": 123048,\n    \"基金家族\": null,\n    \"基金成立日期\": null,\n    \"GMT偏移毫秒数\": \"-14400000\",\n    \"内部人士持股比例\": 0.05746,\n    \"机构持股比例\": 0.7062,\n    \"行业\": \"互联网内容与信息\",\n    \"是否包含ESG信息\": false,\n    \"最近资本利得\": null,\n    \"最近股息金额\": null,\n    \"最近财政年度结束日期\": 1577750400,\n    \"最近市场\": null,\n    \"最近拆股日期\": 1430092800,\n    \"最近拆股比例\": \"10000000:10000000\",\n    \"法律类型\": null,\n    \"公司业务简介\": \"Alphabet Inc.在美国、欧洲、中东、非洲、亚太地区、加拿大和拉丁美洲提供在线广告服务。该公司提供效果广告和品牌广告服务。公司通过Google和其他业务部门运营。Google部门提供诸如Ads、Android、Chrome、Google Cloud、Google Maps、Google Play、硬件、搜索和YouTube等产品，以及技术基础设施。它还提供数字内容、云服务、硬件设备以及其他杂项产品和服务。其他业务部门包括Access、Calico、CapitalG、GV、Verily、Waymo和X等业务，以及互联网和电视服务。Alphabet Inc.成立于1998年，总部位于加利福尼亚州山景城。\",\n    \"全称\": \"Alphabet Inc.\",\n    \"市场\": \"us_market\",\n    \"市值\": 979650805760,\n    \"最大年龄\": 1,\n    \"最大供应量\": null,\n    \"留言板ID\": \"finmb_29096\",\n    \"晨星总体评级\": null,\n    \"晨星风险评级\": null,\n    \"最近一个季度\": 1585612800,\n    \"净值价格\": null,\n    \"归属于普通股的净利润\": 34522001408,\n    \"下一财政年度结束日期\": 1640908800,\n    \"开盘价\": 1411.1,\n    \"未平仓合约量\": null,\n    \"股息支付比率\": 0,\n    \"PEG比率\": 4.38,\n    \"电话\": \"650-253-0000\",\n    \"前收盘价\": 1413.61,\n    \"价格提示\": 2,\n    \"市净率\": 4.812112,\n    \"过去12个月市销率\": 5.87754,\n    \"利润率\": 0.20712,\n    \"报价类型\": \"EQUITY\",\n    \"常规市场当日最高价\": 1441.19,\n    \"常规市场当日最低价\": 1409.82,\n    \"常规市场开盘价\": 1411.1,\n    \"常规市场前收盘价\": 1413.61,\n    \"常规市场当前价格\": 1411.1,\n    \"常规市场成交量\": 1084440,\n    \"季度营收增长率\": null,\n    \"板块\": \"通信服务\",\n    \"已发行股数\": 336161984,\n    \"已发行股数占总股本比例\": 0.0049,\n    \"空头持仓股数\": 3371476,\n    \"上月空头持仓数据日期\": 1589500800,\n    \"上月空头持仓股数\": 3462105,\n    \"简称\": \"Alphabet Inc.\",\n    \"空头持仓占自由流通股比例\": null,\n    \"空头持仓倍数\": 1.9,\n    \"成立日期\": null,\n    \"州\": \"CA\",\n    \"行权价格\": null,\n    \"股票代码\": \"GOOG\",\n    \"3年平均回报率\": null,\n    \"目标货币\": null,\n    \"总资产\": null,\n    \"可交易\": false,\n    \"过去一年股息率\": null,\n    \"过去一年股息收益率\": null,\n    \"过去一年每股收益\": 49.572,\n    \"过去一年市盈率\": 28.904415,\n    \"200日均线\": 1352.9939,\n    \"成交量\": 1084440,\n    \"24小时成交量\": null,\n    \"所有货币成交量\": null,\n    \"官网\": \"http:\u002F\u002Fwww.abc.xyz\",\n    \"收益率\": null,\n    \"年初至今回报率\": null,\n    \"邮编\": \"94043\"\n}\n\nISIN\n-\n\n主要持有人\n        0                                      1\n0   5.75%        所有内部人士持股比例\n1  70.62%       机构持股比例\n2  74.93%        机构持有自由流通股比例\n3    3304  持有股份的机构数量\n\n机构持有人\n                            持有人    报告日期   股份占比        价值\n0       Vanguard Group, Inc. (The)  23162950    2020-03-30  0.0687  26934109889\n1                   Blackrock Inc.  20264225    2020-03-30  0.0601  23563443472\n2    Price (T.Rowe) Associates Inc  12520058    2020-03-30  0.0371  14558448642\n3         State Street Corporation  11814026    2020-03-30  0.0350  13737467573\n4                         FMR, LLC   8331868    2020-03-30  0.0247   9688379429\n5  Capital International Investors   4555880    2020-03-30  0.0135   5297622822\n6    Geode Capital Management, LLC   4403934    2020-03-30  0.0131   5120938494\n7       Northern Trust Corporation   4017009    2020-03-30  0.0119   4671018235\n8        JP Morgan Chase & Company   3707376    2020-03-30  0.0110   4310973886\n9          AllianceBernstein, L.P.   3483382    2020-03-30  0.0103   4050511423\n\n股息\nSeries([], Name: Dividends, dtype: int64)\n\n拆股\n日期\n2014-03-27    2.002\n2015-04-27    1.000\nName: Stock Splits, dtype: float64\n\n动作\n            股息  股票拆分\n日期\n2014-03-27        0.0         2.002\n2015-04-27        0.0         1.000\n\n日历\n空DataFrame\n列: []\n索引: [财报公布日期、财报平均值、财报低点、财报高点、营收平均值、营收低点、营收高点]\n\n推荐意见\n                                         公司    评级    起始评级   行动\n日期\n2012-03-14 15:28:00                Oxen Group        持有                 初始\n2012-03-28 06:29:00                 花旗集团         买入                 主要\n2012-04-03 08:45:00  Global Equities Research  增持                 主要\n2012-04-05 06:34:00             德意志银行         买入                 主要\n2012-04-09 06:03:00          Pivotal Research         买入                 主要\n2012-04-10 11:32:00                       瑞银集团         买入                 主要\n2012-04-13 06:16:00             德意志银行         买入                 主要\n2012-04-13 06:18:00                 Jefferies         买入                 主要\n2012-04-13 06:37:00              PiperJaffray  增持                 主要\n2012-04-13 06:38:00             高盛公司     中性                 主要\n2012-04-13 06:41:00                 摩根大通  增持                 主要\n2012-04-13 06:51:00               Oppenheimer  跑赢大盘                 主要\n2012-04-13 07:13:00                 Benchmark        持有                 主要\n2012-04-13 08:46:00               BMO Capital  跑赢大盘                 主要\n2012-04-16 06:52:00            Hilliard Lyons         买入                 主要\n2012-06-06 06:17:00             德意志银行         买入                 主要\n2012-06-06 06:56:00                 摩根大通  增持                 主要\n2012-06-22 06:15:00                 花旗集团         买入                 主要\n2012-07-13 05:57:00                   Wedbush     中性                 初始\n2012-07-17 09:33:00                            跑赢大盘                 主要\n2012-07-20 06:43:00                 Benchmark        持有                 主要\n2012-07-20 06:54:00             德意志银行         买入                 主要\n2012-07-20 06:59:00           美国银行         买入                 主要\n2012-08-13 05:49:00            摩根士丹利  增持  平衡权重     上调\n2012-09-17 06:07:00  Global Equities Research  增持                 主要\n2012-09-21 06:28:00         Cantor Fitzgerald         买入                 初始\n2012-09-24 06:11:00                 花旗集团         买入                 主要\n2012-09-24 09:05:00          Pivotal Research         买入                 主要\n2012-09-25 07:20:00                  Capstone         买入                 主要\n2012-09-26 05:48:00         Canaccord Genuity         买入                 主要\n...                                       ...         ...           ...    ...\n2017-10-27 19:29:31                       瑞银集团         买入                 主要\n2018-02-02 14:04:52              PiperJaffray  增持    增持   主要\n2018-04-24 11:43:49                 摩根大通  增持    增持   主要\n2018-04-24 12:24:37             德意志银行         买入           买入   主要\n2018-05-05 14:00:37              B. Riley FBR         买入                 主要\n2018-07-13 13:49:13               Cowen & Co.  优于大盘    优于大盘   主要\n2018-07-24 11:50:55               Cowen & Co.  优于大盘    优于大盘   主要\n2018-07-24 13:33:47             Raymond James  优于大盘    优于大盘   主要\n2018-10-23 11:18:00             德意志银行         买入           买入   主要\n2018-10-26 15:17:08             Raymond James  优于大盘    优于大盘   主要\n2019-01-23 12:55:04             德意志银行         买入           买入   主要\n2019-02-05 12:55:12             德意志银行         买入           买入   主要\n2019-02-05 13:18:47              PiperJaffray  增持    增持   主要\n2019-05-15 12:34:54             德意志银行         买入                 主要\n2019-10-23 12:58:59             瑞信集团  优于大盘                 主要\n2019-10-29 11:58:09             Raymond James  优于大盘                 主要\n2019-10-29 14:15:40             德意志银行         买入                 主要\n2019-10-29 15:48:29                       瑞银集团         买入                 主要\n2020-01-06 11:22:07          Pivotal Research         买入          持有     上调\n2020-01-17 13:01:48                       瑞银集团         买入                 主要\n2020-02-04 12:26:56             Piper Sandler  增持                 主要\n2020-02-04 12:41:00             Raymond James  优于大盘                 主要\n2020-02-04 14:00:36             德意志银行         买入                 主要\n2020-02-06 11:34:20                      CFRA  强烈买入                 主要\n2020-03-18 13:52:51                 摩根大通  增持                 主要\n2020-03-30 13:26:16                       瑞银集团         买入                 主要\n2020-04-17 13:01:41               Oppenheimer  优于大盘                 主要\n2020-04-20 19:29:50             瑞信集团  优于大盘                 主要\n2020-04-29 14:01:51                       瑞银集团         买入                 主要\n2020-05-05 12:44:16             德意志银行         买入                 主要\n\n[219行 x 4列]\n\n收益\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n季度收益\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n财务报表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n季度财务报表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n资产负债表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n季度资产负债表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n资产负债表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n季度资产负债表\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n现金流量\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n季度现金流量\n空DataFrame\n列：[开盘价, 最高价, 最低价, 收盘价, 调整后收盘价, 成交量]\n索引：[]\n\n可持续性\n无\n\n选项\n('2020-07-02', '2020-07-10', '2020-07-17', '2020-07-24', '2020-07-31', '2020-08-07', '2020-08-21', '2020-09-18', '2020-11-20', '2020-12-01', '2020-12-18', '2021-01-15', '2021-06-18', '2022-01-21', '2022-06-17')\n```\n\n数据中包含一个JSON文档，我们可以在以后使用它来创建我们的证券主数据表，如果我们希望将这些数据存储起来以跟踪我们将进行交易的证券的话。由于数据可能包含不同的字段，我的建议是将它们存储在数据湖中，这样我们可以从多个来源构建数据，而无需过多担心数据的结构方式。\n\n### 2.2) 市场数据下载\n\n上一步帮助我们识别给定股票代码的若干特征，从而可以利用这些属性来定义我接下来展示的一些图表。需要注意的是，yFinance库仅需通过股票代码、所需时间段的起始日期和结束日期即可完成数据下载。此外，我们还可以使用interval参数指定数据的时间粒度。默认情况下，时间粒度为1天，这也是我在本次训练中将要使用的设置。\n\n要下载数据，我们可以使用以下命令：\n\n```python\nstart = pd.to_datetime('2004-08-01')\nstock = ['GOOG']\ndata = yf.download(stock, start=start, end=datetime.date.today())\nprint(data)\n```\n\n示例输出如下：\n\n```cmd\nC:\\Users\\thund\\Source\\Repos\\stock-prediction-deep-neural-learning>python download_market_data.py\n[*********************100%***********************]  1 of 1 completed\n                   Open         High          Low        Close    Adj Close    Volume\nDate\n2004-08-19    49.813286    51.835709    47.800831    49.982655    49.982655  44871300\n2004-08-20    50.316402    54.336334    50.062355    53.952770    53.952770  22942800\n2004-08-23    55.168217    56.528118    54.321388    54.495735    54.495735  18342800\n2004-08-24    55.412300    55.591629    51.591621    52.239193    52.239193  15319700\n2004-08-25    52.284027    53.798351    51.746044    52.802086    52.802086   9232100\n2004-08-26    52.279045    53.773445    52.134586    53.753517    53.753517   7128600\n2004-08-27    53.848164    54.107193    52.647663    52.876804    52.876804   6241200\n2004-08-30    52.443428    52.548038    50.814533    50.814533    50.814533   5221400\n2004-08-31    50.958992    51.661362    50.889256    50.993862    50.993862   4941200\n2004-09-01    51.158245    51.292744    49.648903    49.937820    49.937820   9181600\n2004-09-02    49.409801    50.993862    49.285267    50.565468    50.565468  15190400\n2004-09-03    50.286514    50.680038    49.474556    49.818268    49.818268   5176800\n2004-09-07    50.316402    50.809555    49.619015    50.600338    50.600338   5875200\n2004-09-08    50.181908    51.322632    50.062355    50.958992    50.958992   5009200\n2004-09-09    51.073563    51.163227    50.311420    50.963974    50.96...                 ...          ...          ...          ...          ...       ...\n2020-05-19  1386.996948  1392.000000  1373.484985  1373.484985  1373.484985   1280600\n2020-05-20  1389.579956  1410.420044  1387.250000  1406.719971  1406.71997ì   1655400\n2020-05-21  1408.000000  1415.489990  1393.449951  1402.800049  1402.80004ì   1385000\n2020-05-22  1396.70996ì  1412.760010  1391.82995ì  1410.42004ì   130940ì\n2020-05-26  1437.27002ì  1441.00000ì  1412.130005  1417.02002ì   20606ì\n2020-05-27  1417.25000ì  1421.73999ì  1391.29003ì  1417.83996ì   16858ì\n2020-05-28  1396.85998ì  1440.83996ì  1396.0000ì  1416.72998ì   16922ì\n2020-05-29  1416.93994ì  1432.56994ì  1413.34997ì  1428.92004ì   18381ì\n2020-06-01  1418.39001ì  1437.95996ì  1418.0000ì  1431.81994ì   12171ì\n2020-06-02  1430.55004ì  1439.60998ì  1418.82995ì  1439.21997ì   12781ì\n2020-06-03  1438.30004ì  1446.55200ì  1429.77697ì  1436.38000ì   15483ì\n2020-06-04  1430.40002ì  1438.95996ì  1412.18005ì   1412.18005ì   14843ì\n2020-06-05  1413.17004ì  1445.05004ì  1413.83996ì   1413.83996ì   17349ì\n2020-06-08  1422.33996ì  1447.98999ì  1422.33996ì  1446.60998ì   14466ì\n2020-06-09  1445.35998ì  1468.0000ì  1443.20996ì  1456.16003ì   1456.16003ì   14092ì\n2020-06-10  1459.54003ì  1474.25903ì  1465.84997ì   1465.84997ì   15252ì\n2020-06-11  1442.47998ì  1454.47497ì  1413.83996ì   1413.83996ì   19913ì\n2020-06-12  1428.48999ì  1437.0000ì  1386.02002ì  1413.83996ì   1413.83996ì   19442ì\n2020-06-15  1390.80004ì  1424.80004ì  1387.92004ì  1419.84997ì   15039ì\n2020-06-16  1445.21997ì  1455.02002ì  1425.90002ì   1442.71997ì   17092ì\n2020-06-17  1447.16003ì  1460.0000ì  1431.38000ì   1451.11997ì   15483ì\n2020-06-18  1449.16003ì  1445.41003ì  1427.01001ì   1435.95996ì   15183ì\n2020-06-19  1444.0000ì  1447.80004ì  1413.83996ì   1413.83996ì   31579ì\n2020-06-22  1429.0000ì  1452.75000ì  1423.20996ì  1451.85998ì   15424ì\n2020-06-23  1455.64001ì  1475.94104ì  1445.23999ì  1464.41003ì   14… [3994 rows x 6 columns]\n```\n\n需要注意的是，正确设置起始日期非常重要，以确保我们收集到完整有效的数据。如果未正确设置起始日期，可能会导致出现一些NaN值，从而影响后续训练的结果。\n\n## 3) 深度学习模型\n\n### 3.1) 训练与验证数据\n\n现在我们已经有了要使用的数据，接下来需要定义训练数据和验证数据的划分标准。由于股票价格会随日期变化，我编写的函数需要三个基本参数：\n- 股票代码：**GOOG**\n- 开始日期：数据起始日期，在本例中为 **2004年8月1日**。\n- 验证日期：用于确定验证集的时间点，在本例中我们指定 **2017年1月1日** 作为验证数据点。\n\n请注意，您需要先配置好 [TensorFlow](https:\u002F\u002Fwww.tensorflow.org\u002F)、Keras 以及 GPU，才能运行下面的示例代码。\n\n在本练习中，我只关注 [收盘价](https:\u002F\u002Fwww.investopedia.com\u002Fterms\u002Fc\u002Fclosingprice.asp)，这是衡量股票或证券表现的标准指标。\n\n以下是我们将创建的训练数据与验证数据的划分图表：\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_7fc2302117b4.png)\n\n此外，还有显示价格分布的直方图：\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_4b689d25126b.png)\n\n### 3.2) 数据归一化\n\n为了对数据进行归一化处理，我们需要将其缩放到 0 到 1 的范围内，以便在统一的尺度上进行分析。为此，我们可以使用预处理工具 [MinMaxScaler](https:\u002F\u002Fscikit-learn.org\u002Fstable\u002Fmodules\u002Fgenerated\u002Fsklearn.preprocessing.MinMaxScaler.html)，具体如下：\n\n```python\n    min_max = MinMaxScaler(feature_range=(0, 1))\n    train_scaled = min_max.fit_transform(training_data)\n```\n\n### 3.3) 添加时间步长\n\n为了实现用于时间序列预测的 LSTM 网络，我们采用了 3 天的时间步长。这种技术使网络能够回顾过去 3 天的数据，从而预测下一天的价格。下图展示了这一概念在我们的实现中的应用：前 3 个收盘价样本用于生成第 4 个样本，以此类推。这样就生成了一个形状为 (3,1) 的矩阵，其中 3 表示时间步长，1 表示特征数量（即收盘价）。\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_dfceab9df500.png)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_4eb7d69113f7.png)\n\n```python\n# 定义时间步长\ntime_steps = 3\n\n# 遍历数据以创建分区\nfor i in range(time_steps, train_scaled.shape[0]):\n    # 创建过去 3 天的数据分区\n    x_train.append(train_scaled[i - time_steps:i])\n\n    # 将下一天的收盘价添加到标签数组\n    y_train.append(train_scaled[i, 0])\n```\n\n采用 3 天的时间步长可以让 LSTM 网络识别数据中的模式，并基于这些模式做出预测。通过滑动窗口的方法，我们可以训练网络根据历史数据来预测未来的股票价格。\n\n### 3.4) 长短期记忆网络深度学习模型的构建\n\n为了创建该模型，您需要安装 **TensorFlow**、**TensorFlow-GPU** 和 **Keras** 才能正常运行。该模型的代码如下所示，每层的解释也在下方给出：\n\n```python\ndef create_long_short_term_memory_model(x_train):\n    model = Sequential()\n    # 第1层，使用Dropout正则化\n    # * units=100：设置输出空间的维度为100个神经元\n    # * return_sequences=True：堆叠LSTM层，使下一层能够接收三维序列输入\n    # * input_shape：训练数据集的形状\n    model.add(LSTM(units=100, return_sequences=True, input_shape=(x_train.shape[1], 1)))\n    # 20%的神经元将被丢弃\n    model.add(Dropout(0.2))\n    # 第2层LSTM\n    # * units=50：设置输出空间的维度为50个神经元\n    # * return_sequences=True：堆叠LSTM层，使下一层能够接收三维序列输入\n    model.add(LSTM(units=50, return_sequences=True))\n    # 20%的神经元将被丢弃\n    model.add(Dropout(0.2))\n    # 第3层LSTM\n    # * units=50：设置输出空间的维度为50个神经元\n    # * return_sequences=True：堆叠LSTM层，使下一层能够接收三维序列输入\n    model.add(LSTM(units=50, return_sequences=True))\n    # 50%的神经元将被丢弃\n    model.add(Dropout(0.5))\n    # 第4层LSTM\n    # * units=50：设置输出空间的维度为50个神经元\n    model.add(LSTM(units=50))\n    # 50%的神经元将被丢弃\n    model.add(Dropout(0.5))\n    # 全连接层，指定输出为1个单元\n    model.add(Dense(units=1))\n    model.summary()\n    tf.keras.utils.plot_model(model, to_file=os.path.join(project_folder, 'model_lstm.png'), show_shapes=True,\n                              show_layer_names=True)\n    return model\n```\n\n渲染后的模型如下图所示，该模型具有超过10万个可训练参数。\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_29443eefafce.png)\n\n```cmd\nLayer (type)                 Output Shape              Param #\n=================================================================\nlstm_1 (LSTM)                (None, 60, 100)           40800\n_________________________________________________________________\ndropout_1 (Dropout)          (None, 60, 100)           0\n_________________________________________________________________\nlstm_2 (LSTM)                (None, 60, 50)            30200\n_________________________________________________________________\ndropout_2 (Dropout)          (None, 60, 50)            0\n_________________________________________________________________\nlstm_3 (LSTM)                (None, 60, 50)            20200\n_________________________________________________________________\ndropout_3 (Dropout)          (None, 60, 50)            0\n_________________________________________________________________\nlstm_4 (LSTM)                (None, 50)                20200\n_________________________________________________________________\ndropout_4 (Dropout)          (None, 50)                0\n_________________________________________________________________\ndense_1 (Dense)              (None, 1)                 51\n=================================================================\nTotal params: 111,451\nTrainable params: 111,451\nNon-trainable params: 0\n```\n\n定义好模型后，我们需要指定用于跟踪模型表现的指标，以及用于训练的优化器类型。我还设置了模型的耐心值及其停止规则。\n\n```python\n    defined_metrics = [\n        tf.keras.metrics.MeanSquaredError(name='MSE')\n    ]\n\n    callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, mode='min', verbose=1)\n\n    model.compile(optimizer='adam', loss='mean_squared_error', metrics=defined_metrics)\n    history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test),\n                        callbacks=[callback])\n```\n\n该模型经过微调，以达到最低的验证损失。在本例中，验证损失为0.14%，均方误差（MSE）也为0.14%，表现相对较好，能够提供非常准确的结果。\n\n训练结果如下：\n\n```cmd\n在3055个样本上训练，在881个样本上验证\n第1轮\u002F100轮\n2020-07-11 15:15:34.557035: I tensorflow\u002Fstream_executor\u002Fplatform\u002Fdefault\u002Fdso_loader.cc:44] 成功加载动态库cublas64_100.dll\n3112\u002F3112 [==============================] - 19秒\u002F样本 - 损失: 0.0451 - MSE: 0.0451 - 验证损失: 0.0068 - 验证MSE: 0.0068\n第2轮\u002F100轮\n3112\u002F3112 [==============================] - 4秒\u002F样本 - 损失: 0.0088 - MSE: 0.0088 - 验证损失: 0.0045 - 验证MSE: 0.0045\n第3轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0062 - MSE: 0.0062 - 验证损失: 0.0032 - 验证MSE: 0.0032\n第4轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0051 - MSE: 0.0051 - 验证损失: 0.0015 - 验证MSE: 0.0015\n第5轮\u002F100轮\n3112\u002F3112 [==============================] - 7秒\u002F样本 - 损失: 0.0045 - MSE: 0.0045 - 验证损失: 0.0013 - 验证MSE: 0.0013\n第6轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0045 - MSE: 0.0045 - 验证损失: 0.0013 - 验证MSE: 0.0013\n第7轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0045 - MSE: 0.0045 - 验证损失: 0.0015 - 验证MSE: 0.0015\n第8轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0040 - MSE: 0.0040 - 验证损失: 0.0015 - 验证MSE: 0.0015\n第9轮\u002F100轮\n3112\u002F3112 [==============================] - 5秒\u002F样本 - 损失: 0.0039 - MSE: 0.0039 - 验证损失: 0.0014 - 验证MSE: 0.0014\n第00009轮：提前停止\n保存权重\n绘制损失曲线\n绘制MSE曲线\n显示模型内容\n886\u002F1 - 0秒 - 损失: 0.0029 - MSE: 0.0014\n损失：0.0014113364930413916\nMSE：0.0014113366\n```\n\n### 3.5) 进行预测\n\n现在是时候准备我们的测试数据，并将其输入到深度学习模型中，以获得我们想要的预测结果了。\n\n首先，我们需要使用与处理训练数据相同的时间步长方法来导入测试数据：\n\n```python\ndef infer_data():\n    print(tf.version.VERSION)\n    inference_folder = os.path.join(os.getcwd(), RUN_FOLDER)\n    stock = StockPrediction(STOCK_TICKER, STOCK_START_DATE, STOCK_VALIDATION_DATE, inference_folder, GITHUB_URL, EPOCHS, TIME_STEPS, TOKEN, BATCH_SIZE)\n\n    data = StockData(stock)\n\n    (x_train, y_train), (x_test, y_test), (training_data, test_data) = data.download_transform_to_numpy(TIME_STEPS, inference_folder)\n    min_max = data.get_min_max()\n\n    # 加载未来数据\n    print('最新股票价格')\n    latest_close_price = test_data.Close.iloc[-1]\n    latest_date = test_data[-1:]['Close'].idxmin()\n    print(latest_close_price)\n    print('最新日期')\n    print(latest_date)\n\n    tomorrow_date = latest_date + timedelta(1)\n    # 指定接下来的300天\n    next_year = latest_date + timedelta(TIME_STEPS * 100)\n\n    print('未来日期')\n    print(tomorrow_date)\n\n    print('未来时间跨度日期')\n    print(next_year)\n\n    x_test, y_test, test_data = data.generate_future_data(TIME_STEPS, min_max, tomorrow_date, next_year, latest_close_price)\n\n    # 检查未来数据是否为空\n    if x_test.shape[0] > 0:\n        # 加载我们最佳模型的权重\n        model = tf.keras.models.load_model(os.path.join(inference_folder, 'model_weights.h5'))\n        model.summary()\n\n        # 进行预测\n        test_predictions_baseline = model.predict(x_test)\n        test_predictions_baseline = min_max.inverse_transform(test_predictions_baseline)\n        test_predictions_baseline = pd.DataFrame(test_predictions_baseline, columns=['Predicted_Price'])\n\n        # 将预测值与测试数据中的日期结合\n        predicted_dates = pd.date_range(start=test_data.index[0], periods=len(test_predictions_baseline))\n        test_predictions_baseline['Date'] = predicted_dates\n        \n        # 重置索引以便正确拼接\n        test_data.reset_index(inplace=True)\n        \n        # 拼接测试数据和预测数据\n        combined_data = pd.concat([test_data, test_predictions_baseline], ignore_index=True)\n        \n        # 绘制预测图\n        plt.figure(figsize=(14, 5))\n        plt.plot(combined_data['Date'], combined_data.Close, color='green', label='模拟[' + STOCK_TICKER + ']价格')\n        plt.plot(combined_data['Date'], combined_data['Predicted_Price'], color='red', label='预测[' + STOCK_TICKER + ']价格')\n        plt.xlabel('时间')\n        plt.ylabel('价格 [USD]')\n        plt.legend()\n        plt.title('模拟与预测价格对比')\n        plt.savefig(os.path.join(inference_folder, STOCK_TICKER + '_future_comparison.png'))\n        plt.show()\n    else:\n        print(\"错误：未来数据为空。\")\n\nstart_date = pd.to_datetime('2017-01-01')\nend_date = datetime.today()\nduration = end_date - start_date\nSTOCK_VALIDATION_DATE = start_date + 0.8 * duration\n```\n\n现在我们可以调用 `infer_data` 方法，它将根据我们在训练数据上进行的训练生成股票预测。最终，我们将生成一个包含预测结果的 CSV 文件，以及一张显示实际价格与预测价格对比的图表。\n\n\u003Cimg width=\"1111\" height=\"444\" alt=\"image\" src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_e28a4688d391.png\" \u002F>\n\n# 4) 使用说明\n\n本项目使用 Python 3.12 构建。\n\n下载源代码并使用以下任一方式安装依赖项：\n\n```cmd\nconda env create -f environment.yml\nconda activate stock-prediction\n```\n\n或者，在现有环境中使用 pip 安装：\n\n```cmd\npip install -r requirements.txt\n```\n\n注意：如果您更改 TensorFlow 版本，请确保 NumPy 的版本与该版本的约束条件相匹配。\n\n### 4.1) 默认模型 (v7)\n\nv7 是默认模型版本。它训练两个分支：方向（上涨\u002F下跌）和幅度（绝对变动），然后重构价格变化量。\n\nv7 的训练输出包括：\n- `model_direction.keras`\n- `model_magnitude.keras`\n- `model_config.json`（包含 `model_version`、`forecast_horizon`、`use_returns`、`use_deltas`、`use_trend_residual`）\n- `min_max_scaler.pkl` 和 `input_scaler.pkl`\n\n### 4.2) 推理（随机轨迹）\n\n推理使用 `stock_prediction_deep_learning_inference.py` 中的 `InferenceRunner`，可以生成多条随机路径：\n- `STOCHASTIC_PATHS`：轨迹数量\n- `STOCHASTIC_SIGMA_MULT`：噪声尺度倍数\n- `STOCHASTIC_LOOKBACK`：用于估计噪声的回溯窗口\n- `STOCHASTIC_SEED`：用于可重复性的随机数种子\n\n输出包括 `future_predictions.csv` 文件，其中包含 `Predicted_Price`、`Predicted_Price_Raw` 以及百分位区间（`Predicted_Price_P10`、`Predicted_Price_P50`、`Predicted_Price_P90`），当启用随机路径时。\n\n然后编辑文件“stock_prediction_deep_learning.py”，填写您想要使用的股票代码及相关日期，并执行以下命令：\n\n```cmd\n# 所有参数均为可选\npython stock_prediction_deep_learning.py -ticker=GOOG -start_date=2017-11-01 -validation_date=2022-09-01 -epochs=150 -batch_size=32 -time_steps=30 -use_returns=false -model_version=v7 -forecast_horizon=1\n```\n\n注意：`model_version` 默认为 `v7`，因此如果您希望使用默认设置，可以省略此参数。\n\n此外，您还可以使用此处提供的 Jupyter Notebook：\n\nhttps:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fblob\u002Fmaster\u002Fstock_prediction_lstm.ipynb\n\n# 5) CUDA 安装\n\n可选：仅在您拥有 NVIDIA GPU 时需要安装。仅使用 CPU 的情况下无需安装。\n\n![image](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_83b506a94fcf.png)\n\n正如我之前提到的，我使用 GPU 来加速测试过程。由于我的笔记本电脑配备了 NVIDIA GeForce 显卡，我安装了 CUDA 以充分利用其 GPU 性能。根据您使用的 TensorFlow 版本，您可能需要不同版本的 CUDA。\n以下是链接：https:\u002F\u002Fdeveloper.nvidia.com\u002Fcuda-11.0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal\n\n您可以在 Conda 提示符下执行以下操作：\n\n```bash\n(base) >conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0\nCollecting package metadata (current_repodata.json): done\nSolving environment: failed with initial frozen solve. Retrying with flexible solve.\nCollecting package metadata (repodata.json): done\nSolving environment: done\n\n\n==> 警告：存在更新版本的 conda。 \u003C==\n  当前版本：4.10.3\n  最新版本：4.14.0\n\n请通过运行以下命令更新 conda：\n\n    $ conda update -n base -c defaults conda\n\n## 软件包计划 ##\n\n  环境位置：C:\\Users\\jordi\\anaconda3\n\n  添加\u002F更新的规格：\n    - cudatoolkit=11.2\n    - cudnn=8.1.0\n\n\n以下软件包将被下载：\n\n    软件包                    |            构建\n    ---------------------------|-----------------\n    conda-4.12.0               |   py39hcbf5309_0         1.0 MB  conda-forge\n    cudatoolkit-11.2.2         |      h933977f_10       879.9 MB  conda-forge\n    cudnn-8.1.0.77             |       h3e0f4f4_0       610.8 MB  conda-forge\n    python_abi-3.9             |           2_cp39           4 KB  conda-forge\n    ------------------------------------------------------------\n                                           总计:        1.46 GB\n\n以下新软件包将被安装：\n\n  cudatoolkit        conda-forge\u002Fwin-64::cudatoolkit-11.2.2-h933977f_10\n  cudnn              conda-forge\u002Fwin-64::cudnn-8.1.0.77-h3e0f4f4_0\n  python_abi         conda-forge\u002Fwin-64::python_abi-3.9-2_cp39\n\n以下软件包将被更新：\n\n  conda              pkgs\u002Fmain::conda-4.10.3-py39haa95532_0 --> conda-forge::conda-4.12.0-py39hcbf5309_0\n\n\n是否继续（[y]\u002Fn）？ y\n\n\n正在下载和解压软件包\nconda-4.12.0         | 1.0 MB    | ############################################################################ | 100%\npython_abi-3.9       | 4 KB      | ############################################################################ | 100%\ncudnn-8.1.0.77       | 610.8 MB  | ############################################################################ | 100%\ncudatoolkit-11.2.2   | 879.9 MB  | ############################################################################ | 100%\n准备事务：完成\n验证事务：完成\n执行事务： \u002F “通过下载和使用 CUDA 工具包的 conda 软件包，即表示您接受 CUDA 最终用户许可协议 (EULA) 的条款和条件：https:\u002F\u002Fdocs.nvidia.com\u002Fcuda\u002Feula\u002Findex.html”\n\n| “通过下载和使用 cuDNN 的 conda 软件包，即表示您接受 NVIDIA cuDNN EULA 的条款和条件 - https:\u002F\u002Fdocs.nvidia.com\u002Fdeeplearning\u002Fcudnn\u002Fsla\u002Findex.html”\n\n已完成\n```\n\n在此之后运行项目，GPU 应该能够被正确识别：\n\n![image](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_readme_35a02ce3ce46.png)\n\n# 6) Graphviz 安装\n\n如果您看到此消息，则需要安装 GraphViz 库：\n\n```bash\n您必须安装 pydot（`pip install pydot`）并安装 graphviz（请参阅 https:\u002F\u002Fgraphviz.gitlab.io\u002Fdownload\u002F 上的说明），以便 plot_model\u002Fmodel_to_dot 正常工作。\n```\n\n- https:\u002F\u002Fgraphviz.gitlab.io\u002Fdownload\u002F\n\n## 赞助商\n目前尚无赞助商！您愿意成为第一位吗？\n\n[![PayPal 捐赠按钮](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Fpaypal-donate-yellow.svg)](https:\u002F\u002Fwww.paypal.com\u002Fpaypalme\u002Fjordicorbilla)\n\n### 支持我\n\n\u003Ca href=\"https:\u002F\u002Fwww.buymeacoffee.com\u002Fjordicorbilla\" target=\"_blank\">\u003Cimg src=\"https:\u002F\u002Fcdn.buymeacoffee.com\u002Fbuttons\u002Fdefault-orange.png\" alt=\"Buy Me A Coffee\" height=\"41\" width=\"174\">\u003C\u002Fa>","# stock-prediction-deep-neural-learning 快速上手指南\n\n本项目基于深度学习（LSTM 长短期记忆网络）和 TensorFlow，用于股票价格的时间序列预测。以下是在本地环境快速运行该项目的步骤。\n\n## 1. 环境准备\n\n在开始之前，请确保您的系统满足以下要求：\n\n*   **操作系统**：Windows, macOS 或 Linux\n*   **Python 版本**：3.12\n*   **核心框架**：TensorFlow 2.18.1\n*   **数据源库**：yFinance (用于从 Yahoo Finance 获取数据)\n*   **包管理工具**：推荐安装 [Conda](https:\u002F\u002Fdocs.conda.io\u002F) (Miniconda 或 Anaconda)，也可使用 pip。\n\n> **提示**：国内用户若下载 Conda 包或 PyPI 包速度较慢，可配置清华源或阿里源加速。\n\n## 2. 安装步骤\n\n### 方式一：使用 Conda（推荐）\n\n这是最简便的方式，可自动处理所有依赖关系。\n\n1.  克隆或下载项目代码后，进入项目目录。\n2.  创建并激活虚拟环境：\n\n```cmd\nconda env create -f environment.yml\nconda activate stock-prediction\n```\n\n*(注：若 `environment.yml` 中源地址在国外导致下载失败，可手动编辑该文件添加国内镜像源，或在创建后使用 `conda config --add channels https:\u002F\u002Fmirrors.tuna.tsinghua.edu.cn\u002Fanaconda\u002Fpkgs\u002Fmain\u002F`)*\n\n### 方式二：使用 Pip\n\n如果您已有 Python 3.12 环境，可直接使用 pip 安装依赖：\n\n```cmd\npip install -r requirements.txt\n```\n\n**重要提示**：请确保 `yfinance` 库为最新版本，以避免数据抓取错误：\n\n```cmd\npip install yfinance==0.2.54\n```\n\n### 配置 Jupyter Kernel (可选)\n\n如果您计划使用 Jupyter Notebook 进行开发，请选择 `stock-prediction` 内核。如果内核未显示，请运行以下命令注册：\n\n```cmd\npython -m ipykernel install --user --name stock-prediction --display-name \"stock-prediction\"\n```\n\n## 3. 基本使用\n\n本项目默认以谷歌公司（股票代码：**GOOG**）为例进行演示。\n\n### 获取市场数据\n\n运行以下脚本下载指定股票的财务数据和市场信息。该脚本会使用 `yFinance` 拉取最新数据并打印详细信息（如市值、市盈率、机构持仓等）。\n\n```cmd\npython download_market_data_info.py\n```\n\n**预期输出示例：**\n成功运行后，终端将输出类似以下的 JSON 格式数据和统计表格：\n\n```text\nInfo\n{\n    \"symbol\": \"GOOG\",\n    \"longName\": \"Alphabet Inc.\",\n    \"regularMarketPrice\": 1411.1,\n    \"marketCap\": 979650805760,\n    ...\n}\n\nMajor Holders\n        0                                      1\n0   5.75%        % of Shares Held by All Insider\n1  70.62%       % of Shares Held by Institutions\n...\n\nInstitutional Holders\n                            Holder    Shares Date Reported   % Out        Value\n0       Vanguard Group, Inc. (The)  23162950    2020-03-30  0.0687  26934109889\n...\n```\n\n数据下载完成后，即可在项目提供的 Notebook 文件中加载这些数据，构建 LSTM 模型进行训练和股价预测。","某量化交易团队的初级分析师正在尝试为谷歌（GOOG）股票构建短期价格预测模型，以辅助制定下周的交易策略。\n\n### 没有 stock-prediction-deep-neural-learning 时\n- **数据获取繁琐**：需要手动编写复杂的爬虫脚本或从多个金融终端导出 CSV 文件，难以保证数据的实时性和完整性。\n- **建模门槛极高**：从零搭建 LSTM（长短期记忆网络）架构需要深厚的深度学习功底，处理时间序列的“长期依赖”问题极易出错。\n- **环境配置混乱**：手动安装 TensorFlow、Python 及相关依赖库时，常因版本冲突导致代码无法运行，调试耗时数天。\n- **预测缺乏依据**：仅凭简单的移动平均线或主观经验判断趋势，无法捕捉股价背后复杂的非线性波动规律，投资风险不可控。\n\n### 使用 stock-prediction-deep-neural-learning 后\n- **一键获取数据**：内置 yFinance 模块，只需输入\"GOOG\"代号即可自动下载并清洗最新的雅虎财经历史数据，大幅缩短准备时间。\n- **开箱即用的模型**：直接调用预置的 TensorFlow LSTM 神经网络，自动识别股价序列中的长期模式，无需从头推导数学公式。\n- **标准化运行环境**：通过 Conda 环境文件一键部署 Python 3.12 与 TensorFlow 2.18.1，彻底解决依赖冲突，几分钟内即可启动训练。\n- **科学辅助决策**：基于深度学习的时序预测输出具体的未来价格走势，为团队提供数据支撑的交易信号，显著提升策略胜率。\n\nstock-prediction-deep-neural-learning 将高深的深度学习技术转化为可执行的自动化流程，让普通分析师也能利用前沿 AI 模型挖掘股市价值。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FJordiCorbilla_stock-prediction-deep-neural-learning_7fc23021.png","JordiCorbilla","Jordi Corbilla","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002FJordiCorbilla_6d4b12a1.jpg","Senior Full-Stack & Quantitative Engineer | Trading, Risk, Compliance & AI Systems","Global Multi-Strategy Hedge Fund","London",null,"thunderjordi","https:\u002F\u002Fjordicorbilla.github.io\u002Fresume\u002F","https:\u002F\u002Fgithub.com\u002FJordiCorbilla",[83,87],{"name":84,"color":85,"percentage":86},"Jupyter Notebook","#DA5B0B",92.6,{"name":88,"color":89,"percentage":90},"Python","#3572A5",7.4,674,125,"2026-04-10T08:37:28","CC0-1.0","Windows, Linux, macOS","未说明（基于 TensorFlow 2.18.1，可选支持 CUDA）","未说明",{"notes":99,"python":100,"dependencies":101},"项目明确指定使用 Python 3.12 和 TensorFlow 2.18.1。推荐使用 Conda 创建环境（environment.yml），若使用 pip 需安装 requirements.txt。Jupyter 用户需手动注册 'stock-prediction' 内核。数据源依赖 Yahoo Finance (yfinance)，务必使用 0.2.54 版本以避免错误。README 未明确提及 GPU 强制需求或具体显存要求，但深度学习训练通常建议配备 NVIDIA GPU。","3.12",[102,103,104],"tensorflow==2.18.1","yfinance==0.2.54","ipykernel",[16,14],[107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123],"machine-learning","deep-learning","deep-neural-networks","yfinance","lstm-neural-networks","lstm-model","recurrent-neural-networks","stock-prediction","stock-prices","market-data","finance","cryptocurrency","crypto","lstm","stock","time-series","jupyter-notebook","2026-03-27T02:49:30.150509","2026-04-17T09:55:56.349351",[127,132,137,142,147,152,156],{"id":128,"question_zh":129,"answer_zh":130,"source_url":131},37145,"运行股票预测代码时出现 'TypeError: __init__() missing required argument' 错误怎么办？","该错误通常是因为调用类初始化时缺少必要的参数。根据维护者回复，运行推理脚本（如 stock_prediction_deep_learning_inference.py）或实例化 StockPrediction 类时，必须显式传递以下参数：'github_url', 'epochs', 'time_steps', 'token', 和 'batch_size'。请检查你的调用代码，确保在初始化时填入了这些必填项。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F8",{"id":133,"question_zh":134,"answer_zh":135,"source_url":136},37146,"如何加载训练好的模型并预测未来（例如明天）的股价？","要预测未来数据，需要加载保存的模型 (.h5)，读取最新的数据点，进行与训练时相同的预处理（归一化\u002F标准化），然后输入模型。维护者提供了以下 Python 代码示例：\n1. 导入库：from keras.models import load_model\n2. 加载模型：model = load_model('model.h5')\n3. 读取最新数据：data = pd.read_csv('data.csv'); latest_data = data.tail(1)\n4. 预处理数据（需与训练时一致）\n5. 创建输入张量并预测：prediction = model.predict(input_tensor)\n注意：LSTM 模型通常输出下一时间步的预测值，需循环反馈或使用滑动窗口来预测更远的未来。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F12",{"id":138,"question_zh":139,"answer_zh":140,"source_url":141},37147,"能否在图表中显示未来一个月或更长时间的预测结果？","在时间序列预测场景中，直接在图表上向前延伸一个月来评估准确性“本质上没有意义”。LSTM 模型的准确性通常基于其对未来每个时间步（默认通常是未来 3 天）的预测能力来评估。虽然可以更新 Notebook 以包含推理部分来查看短期预测，但长期预测会因误差累积而变得不可靠，因此项目默认侧重于短期步进预测。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F13",{"id":143,"question_zh":144,"answer_zh":145,"source_url":146},37148,"如何将此代码应用于自己的数据集（CSV 文件）？","你可以直接使用自己的数据集。维护者确认只需将你的数据集（.csv 格式）放入项目中，代码应当能够运行。如果遇到问题，通常需要检查 CSV 文件的列名和格式是否与代码中数据加载和预处理部分的预期一致（例如日期列、收盘价列的名称），并确保数据清洗逻辑适配你的数据源。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F1",{"id":148,"question_zh":149,"answer_zh":150,"source_url":151},37149,"为什么项目中只使用收盘价进行预测，而不结合最高价、最低价等其他指标？","当前框架主要使用收盘价进行决策和预测演示。不过，维护者明确表示这并非限制，完全支持结合多种价格（如开盘价、最高价、最低价）或多维技术指标（如 RSI, MACD 等）进行预测。用户可以修改数据输入部分，构建多维数组（例如 n 列 x 8 个时间窗口的数组），并利用 TensorFlow 的 Conv1D 或 LSTM 层来处理这些多维特征以提升模型性能。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F4",{"id":153,"question_zh":154,"answer_zh":155,"source_url":136},37150,"如何在 predictions.csv 文件中按日期显示逐日的预测结果？","默认的 predictions.csv 可能只包含数值。要实现按日期显示，你需要在生成预测后，将预测数组与对应的日期索引重新组合。具体做法是：在代码中获取用于预测的时间序列对应的日期列表，将模型输出的预测值（可能需要反归一化）与该日期列表合并，然后导出为包含 'Date', 'Actual', 'Predicted' 列的 CSV 文件。对于未来日期的预测，需基于最后已知日期向后推算日期序列。",{"id":157,"question_zh":158,"answer_zh":159,"source_url":160},37151,"为什么 README 中没有提供具体的回测结果或交易策略收益分析？","这是一个广泛的话题，项目作者倾向于让用户自行探索这部分内容。作者表示他获得了满足其研究需求的结果，但并未将具体的回测收益或实盘应用结果写入 README。用户被建议生成模型后，自行喂入新数据来获取未来数据的预测结果，并根据实际需求开发相应的评估指标（如夏普比率、最大回撤等），因为单纯的“实际值 vs 预测值”图表并不足以衡量真实世界的应用效果。","https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fissues\u002F10",[162,167,172,176,180,185],{"id":163,"version":164,"summary_zh":165,"released_at":166},297652,"v1.0","### 新增\n- v7 默认模型：方向 + 大小头，带残差重建。\n- 在 `future_predictions.csv` 中添加了带有百分位带的随机推理轨迹。\n- 随机控制的推理运行参数。\n\n### 变更\n- 默认模型版本现已更新为 `v7`。\n- 更新了文档，以反映 v7 的输出和推理行为。\n\n## 变更内容\n* 由 @JordiCorbilla 在 https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fpull\u002F23 中完成的推理修复及版本测试。\n* 由 @JordiCorbilla 在 https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fpull\u002F24 中更新的 README.md。\n\n\n**完整变更日志**: https:\u002F\u002Fgithub.com\u002FJordiCorbilla\u002Fstock-prediction-deep-neural-learning\u002Fcompare\u002Fv0.5...v1.0","2025-12-28T21:04:07",{"id":168,"version":169,"summary_zh":170,"released_at":171},297653,"v0.5","版本05\r\nLSTM模型","2025-12-28T20:58:16",{"id":173,"version":174,"summary_zh":78,"released_at":175},297654,"v0.4","2024-01-03T19:18:42",{"id":177,"version":178,"summary_zh":78,"released_at":179},297655,"v0.3","2022-09-18T19:45:47",{"id":181,"version":182,"summary_zh":183,"released_at":184},297656,"v0.2","本版本包含一个示例文件 weight.h5，可用于预测GOOG的未来价格。该模型使用LSTM深度学习网络进行训练，其评估指标如下：\n\n损失值：0.0014113364930413916  \n均方误差：0.0014113366","2020-07-12T07:43:23",{"id":186,"version":187,"summary_zh":188,"released_at":189},297657,"v0.1","本版本包含一个示例文件 weight.h5，可用于预测 GOOG 的未来价格。该模型使用 LSTM 深度学习网络进行训练，其评估指标如下：\n\n损失值：0.002031383795714456  \n均方误差：0.0029848262201994658","2020-07-05T19:03:10"]