{"id":1175080,"date":"2025-01-15T17:26:54","date_gmt":"2025-01-15T09:26:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175080.html"},"modified":"2025-01-15T17:26:57","modified_gmt":"2025-01-15T09:26:57","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97%e8%82%a1%e7%a5%a8%e5%b9%b4%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175080.html","title":{"rendered":"python\u5982\u4f55\u83b7\u5f97\u80a1\u7968\u5e74\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110639\/e2fe56be-a747-468e-a265-f3911ce7ad64.webp\" alt=\"python\u5982\u4f55\u83b7\u5f97\u80a1\u7968\u5e74\u7ebf\" \/><\/p>\n<p><p> \u8981\u83b7\u5f97\u80a1\u7968\u7684\u5e74\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a<strong>\u4f7f\u7528\u7b2c\u4e09\u65b9API\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001\u4f7f\u7528Python\u5e93\u5982pandas\u548cmatplotlib\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\u3001\u5229\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u5206\u6790\u6280\u672f\u8fdb\u884c\u9884\u6d4b\u548c\u5206\u6790<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9API\u83b7\u53d6\u80a1\u7968\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9API\u83b7\u53d6\u80a1\u7968\u6570\u636e\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5f88\u591a\u5e73\u53f0\u63d0\u4f9b\u4e86\u514d\u8d39\u7684API\u63a5\u53e3\uff0c\u901a\u8fc7\u8fd9\u4e9b\u63a5\u53e3\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5386\u53f2\u80a1\u4ef7\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406\u3002\u5e38\u7528\u7684API\u5305\u62ecAlpha Vantage\u3001Yahoo Finance\u3001\u548cIEX Cloud\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. Alpha Vantage API<\/h4>\n<\/p>\n<p><p>Alpha Vantage \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u80a1\u7968\u6570\u636eAPI\uff0c\u901a\u8fc7\u7b80\u5355\u7684HTTP\u8bf7\u6c42\u5373\u53ef\u83b7\u53d6\u6570\u636e\u3002\u9996\u5148\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u514d\u8d39\u7684API Key\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import pandas as pd<\/p>\n<p>def get_stock_data(symbol, api_key):<\/p>\n<p>    url = f&#39;https:\/\/www.alphavantage.co\/query?function=TIME_SERIES_D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>LY&amp;symbol={symbol}&amp;apikey={api_key}&amp;outputsize=full&amp;datatype=csv&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    with open(&#39;stock_data.csv&#39;, &#39;w&#39;) as f:<\/p>\n<p>        f.write(response.text)<\/p>\n<p>    df = pd.read_csv(&#39;stock_data.csv&#39;)<\/p>\n<p>    return df<\/p>\n<p>api_key = &#39;your_api_key&#39;<\/p>\n<p>symbol = &#39;AAPL&#39;<\/p>\n<p>stock_data = get_stock_data(symbol, api_key)<\/p>\n<p>print(stock_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Yahoo Finance API<\/h4>\n<\/p>\n<p><p>Yahoo Finance \u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u4fbf\u7684\u80a1\u7968\u6570\u636e\u83b7\u53d6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5229\u7528 yfinance \u5e93\u6765\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>def get_stock_data(symbol):<\/p>\n<p>    stock = yf.Ticker(symbol)<\/p>\n<p>    data = stock.history(period=&quot;1y&quot;)<\/p>\n<p>    return data<\/p>\n<p>symbol = &#39;AAPL&#39;<\/p>\n<p>stock_data = get_stock_data(symbol)<\/p>\n<p>print(stock_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u80a1\u7968\u6570\u636e\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u4e3b\u8981\u4f7f\u7528\u7684\u5e93\u5305\u62ecpandas\u3001numpy\u548cmatplotlib\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5904\u7406\u548c\u8ba1\u7b97\u5e74\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def calculate_yearly_moving_average(data, window=250):<\/p>\n<p>    data[&#39;Yearly_MA&#39;] = data[&#39;Close&#39;].rolling(window=window).mean()<\/p>\n<p>    return data<\/p>\n<p>stock_data = calculate_yearly_moving_average(stock_data)<\/p>\n<p>print(stock_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u7ed8\u5236\u80a1\u7968\u5e74\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>def plot_stock_data(data, symbol):<\/p>\n<p>    plt.figure(figsize=(14,7))<\/p>\n<p>    plt.plot(data[&#39;Close&#39;], label=&#39;Close Price&#39;)<\/p>\n<p>    plt.plot(data[&#39;Yearly_MA&#39;], label=&#39;Yearly Moving Average&#39;, color=&#39;orange&#39;)<\/p>\n<p>    plt.title(f&#39;{symbol} Stock Price and Yearly Moving Average&#39;)<\/p>\n<p>    plt.xlabel(&#39;Date&#39;)<\/p>\n<p>    plt.ylabel(&#39;Price&#39;)<\/p>\n<p>    plt.legend()<\/p>\n<p>    plt.show()<\/p>\n<p>plot_stock_data(stock_data, symbol)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u673a\u5668\u5b66\u4e60\u548c\u6570\u636e\u5206\u6790\u6280\u672f\u8fdb\u884c\u9884\u6d4b\u548c\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u9664\u57fa\u672c\u7684\u5e74\u7ebf\u8ba1\u7b97\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u8fdb\u884c\u80a1\u7968\u4ef7\u683c\u9884\u6d4b\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u548c\u7279\u5f81\u5de5\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<p>def preprocess_data(data):<\/p>\n<p>    data = data[[&#39;Close&#39;]].dropna()<\/p>\n<p>    scaler = StandardScaler()<\/p>\n<p>    data[&#39;Close&#39;] = scaler.fit_transform(data[[&#39;Close&#39;]])<\/p>\n<p>    return data, scaler<\/p>\n<p>processed_data, scaler = preprocess_data(stock_data)<\/p>\n<p>print(processed_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5efa\u7acb\u9884\u6d4b\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5229\u7528LSTM\uff08\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc\uff09\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import tensorflow as tf<\/p>\n<p>from tensorflow.keras.models import Sequential<\/p>\n<p>from tensorflow.keras.layers import LSTM, Dense<\/p>\n<p>def create_dataset(data, time_step=1):<\/p>\n<p>    X, y = [], []<\/p>\n<p>    for i in range(len(data)-time_step-1):<\/p>\n<p>        X.append(data[i:(i+time_step), 0])<\/p>\n<p>        y.append(data[i + time_step, 0])<\/p>\n<p>    return np.array(X), np.array(y)<\/p>\n<p>def build_lstm_model(input_shape):<\/p>\n<p>    model = Sequential()<\/p>\n<p>    model.add(LSTM(50, return_sequences=True, input_shape=input_shape))<\/p>\n<p>    model.add(LSTM(50, return_sequences=False))<\/p>\n<p>    model.add(Dense(1))<\/p>\n<p>    model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;)<\/p>\n<p>    return model<\/p>\n<p>time_step = 60<\/p>\n<p>X, y = create_dataset(processed_data.values, time_step)<\/p>\n<p>X = X.reshape(X.shape[0], X.shape[1], 1)<\/p>\n<p>model = build_lstm_model((time_step, 1))<\/p>\n<p>model.fit(X, y, epochs=10, batch_size=32, verbose=1)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(X)<\/p>\n<p>predictions = scaler.inverse_transform(predictions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u9884\u6d4b\u7ed3\u679c\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5c06\u9884\u6d4b\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(14,7))<\/p>\n<p>plt.plot(stock_data[&#39;Close&#39;], label=&#39;Actual Stock Price&#39;)<\/p>\n<p>plt.plot(stock_data.index[time_step+1:], predictions, label=&#39;Predicted Stock Price&#39;, color=&#39;orange&#39;)<\/p>\n<p>plt.title(f&#39;{symbol} Stock Price Prediction&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Price&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<strong>\u7b2c\u4e09\u65b9API\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001\u4f7f\u7528Python\u5e93\u5982pandas\u548cmatplotlib\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\u3001\u5229\u7528\u673a\u5668\u5b66\u4e60\u548c\u6570\u636e\u5206\u6790\u6280\u672f\u8fdb\u884c\u9884\u6d4b\u548c\u5206\u6790<\/strong>\u7b49\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u548c\u5206\u6790\u80a1\u7968\u5e74\u7ebf\u6570\u636e\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6211\u4eec\u7684\u6570\u636e\u5206\u6790\u6548\u7387\u548c\u9884\u6d4b\u51c6\u786e\u6027\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u5b9e\u73b0\u80a1\u7968\u6570\u636e\u7684\u5206\u6790\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u80a1\u7968\u5e74\u7ebf\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u91d1\u878d\u6570\u636e\u83b7\u53d6\u5e93\uff0c\u5982<code>yfinance<\/code>\u6216<code>pandas_datareader<\/code>\uff0c\u6765\u83b7\u53d6\u80a1\u7968\u7684\u5e74\u7ebf\u6570\u636e\u3002\u60a8\u53ea\u9700\u5bfc\u5165\u76f8\u5173\u5e93\uff0c\u8f93\u5165\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4\uff0c\u5373\u53ef\u8f7b\u677e\u63d0\u53d6\u51fa\u6240\u9700\u7684\u80a1\u7968\u5e74\u7ebf\u6570\u636e\u3002<\/p>\n<p><strong>\u83b7\u53d6\u80a1\u7968\u5e74\u7ebf\u6570\u636e\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u80a1\u7968\u5e74\u7ebf\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u65f6\u95f4\u8303\u56f4\u8db3\u591f\u957f\uff0c\u4ee5\u6db5\u76d6\u81f3\u5c11\u4e00\u5e74\u7684\u6570\u636e\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u6570\u636e\u6e90\u7684\u53ef\u9760\u6027\u548c\u51c6\u786e\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u4f7f\u7528\u77e5\u540d\u7684\u91d1\u878d\u6570\u636e\u6e90\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u5f97\u66f4\u9ad8\u8d28\u91cf\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u80a1\u7968\u7684\u5e74\u7ebf\u56fe\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u6216<code>seaborn<\/code>\u7b49\u53ef\u89c6\u5316\u5e93\u6765\u7ed8\u5236\u80a1\u7968\u7684\u5e74\u7ebf\u56fe\u3002\u63d0\u53d6\u5230\u5e74\u7ebf\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u4f20\u5165\u53ef\u89c6\u5316\u51fd\u6570\u4e2d\uff0c\u8bbe\u7f6e\u9002\u5f53\u7684\u56fe\u8868\u6837\u5f0f\u548c\u6807\u7b7e\uff0c\u4ece\u800c\u751f\u6210\u76f4\u89c2\u7684\u80a1\u7968\u5e74\u7ebf\u56fe\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u80a1\u7968\u8d70\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u5f97\u80a1\u7968\u7684\u5e74\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9API\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001\u4f7f\u7528Python\u5e93\u5982pandas\u548cmat [&hellip;]","protected":false},"author":3,"featured_media":1175082,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175080"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1175080"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175080\/revisions"}],"predecessor-version":[{"id":1175083,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175080\/revisions\/1175083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1175082"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}