你的位置:滚球app官网 > 新闻资讯 > 开云kaiyun中国官方网站在万般函数中增添了“use_cache”变量-滚球app官网

新闻资讯
开云kaiyun中国官方网站在万般函数中增添了“use_cache”变量-滚球app官网
发布日期:2025-01-20 06:18    点击次数:51

开云kaiyun中国官方网站在万般函数中增添了“use_cache”变量-滚球app官网

图片

写在前边的话:本文手把手教养群众使用 Python 和 AI 进行股票走动展望。来源先容了不同的展望法子,稀薄是 LSTM 处理序列展望的才略。然后提供了主张考据体式,包括装配、创建边幅等,还展示代码设置,如导入库、用函数磨真金不怕火测试模子,终末还评估了模子的性能。

咱们探寻了多种展望股价的步地,像 Facebook 的 Prophet 等展望器用、SARIMA 模子等统计技能、多项式追忆等机器学习政策,还有基于东谈主工智能的轮回神经收罗(RNN)。在广阔东谈主工智能模子与技巧里,咱们发现短永劫驰念(LSTM)模子能带来最理念念的收尾。

LSTM 模子是递归神经收罗架构的一种变形,擅所长理序列展望艰巨。它与传统的前馈神经收罗不同,具有类似驰念的结构,能在大皆序列中保留陡立文数据。这一特质使其荒谬符合时辰序列展望、当然话语处理以过甚他依赖序列数据的任务。它通过缓解消成仇梯度爆炸问题,处置了圭臬 RNN 的基本颓势,从而升迁了模子识别数据集内永恒依赖干系的才略。因此,LSTM 已成为需要永劫辰潜入领悟数据的复杂任务的首选。

为了考据其有用性,咱们开垦了一个主张考据。

一、准备使命

你需要在你的诡计机中(或接受使用 VSCode 会愈加便捷)装配最新版块的 Python 和 PIP。(https://code.visualstudio.com/)

创建一个带有 “main.py “文献的 Python 边幅。

在边幅中添加 “data”目次。

设置并激活假造环境。

trading-ai-lstm $ python3 -m venv venvtrading-ai-lstm $ source venv/.bin/activate(venv) trading-ai-lstm $

创建一个 “requirements.txt “文献。

pandasnumpyscikit-learnscipymatplotlibtensorfloweodhdpython-dotenv

确保已在假造环境中升级 PIP 并装配依赖项。

(venv) trading-ai-lstm $ pip install --upgrade pip(venv) trading-ai-lstm $ python3 -m pip install -r requirements.txt

需要在”.env “文献中加入了 EODHD API 的 API 密钥。

API_TOKEN=<YOUR_API_KEY_GOES_HERE>

一切就绪。如果你正在使用  VSCode ,并但愿使用与咱们沟通的”.vscode/settings.json “文献,请点击 Fork 本边幅 GitHub 仓库(https://github.com/alexyu2013/trading-ai-lstm),以备预加防备。

{
  'python.formatting.provider': 'none',  'python.formatting.blackArgs': ['--line-length', '160'],  'python.linting.flake8Args': [    '--max-line-length=160',    '--ignore=E203,E266,E501,W503,F403,F401,C901'  ],  'python.analysis.diagnosticSeverityOverrides': {    'reportUnusedImport': 'information',    'reportMissingImports': 'none'  },  '[python]': {    'editor.defaultFormatter': 'ms-python.black-formatter'  }}
二、代码构建

第一步是导入必要的库。

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'import pickleimport pandas as pdimport numpy as npfrom dotenv import load_dotenvfrom sklearn.metrics import mean_squared_error, mean_absolute_errorfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import LSTM, Dense, Dropoutfrom tensorflow.keras.models import load_modelfrom sklearn.preprocessing import MinMaxScalerimport matplotlib.pyplot as pltfrom eodhd import APIClient

TensorFlow 每每会自动生成诸多告诫与调试信息。而咱们更倾向于简陋明了的输出,故而对这些奉告进行了收尾。这不错在导入“os”模块后,借助 os.environ 来结束。

机器学习和东谈主工智能模子的磨真金不怕火过程需要大皆的微调,主若是通过所谓的超参数(hyperparameters)进行照应。这个问题混淆视听,掌持它需要不休学习和耐烦,最好超参数的接受受到各式身分的影响。字据咱们通过 EODHD API (https://eodhd.com/)取得的圭臬普尔 500 指数逐日数据,咱们来源使用了一些广为招供的设置。咱们饱读舞您修改这些设置以提高收尾。咫尺,提议将序列长度保持在 20。

# Configurable hyperparametersseq_length = 20batch_size = 64lstm_units = 50epochs = 100

下一步是从咱们的”.env “文献中取得 EODHD API 的 API_TOKEN。

# Load environment variables from the .env fileload_dotenv()# Retrieve the API keyAPI_TOKEN = os.getenv('API_TOKEN')if API_TOKEN is not None:    print(f'API key loaded: {API_TOKEN[:4]}********')else:    raise LookupError('Failed to load API key.')

需要确保领有有用的 EODHD API 的 API_TOKEN 本事生效探员数据。

咱们仍是设置了几个可类似使用的函数,并将鄙人文中详备先容它们的功能。我把这些函数进行了代码注目,以走漏其操作。

def get_ohlc_data(use_cache: bool = False) -> pd.DataFrame:    ohlcv_file = 'data/ohlcv.csv'    if use_cache:        if os.path.exists(ohlcv_file):            return pd.read_csv(ohlcv_file, index_col=None)        else:            api = APIClient(API_TOKEN)            df = api.get_historical_data(                symbol='HSPX.LSE',                interval='d',                iso8601_start='2010-05-17',                iso8601_end='2023-10-04',            )            df.to_csv(ohlcv_file, index=False)            return df    else:        api = APIClient(API_TOKEN)        return api.get_historical_data(            symbol='HSPX.LSE',            interval='d',            iso8601_start='2010-05-17',            iso8601_end='2023-10-04',        )def create_sequences(data, seq_length):    x, y = [], []    for i in range(len(data) - seq_length):        x.append(data[i : i + seq_length])        y.append(data[i + seq_length, 3])  # The prediction target 'close' is the 4th column (index 3)    return np.array(x), np.array(y)def get_features(df: pd.DataFrame = None, feature_columns: list = ['open', 'high', 'low', 'close', 'volume']) -> list:    return df[feature_columns].valuesdef get_target(df: pd.DataFrame = None, target_column: str = 'close') -> list:    return df[target_column].valuesdef get_scaler(use_cache: bool = True) -> MinMaxScaler:    scaler_file = 'data/scaler.pkl'    if use_cache:        if os.path.exists(scaler_file):            # Load the scaler            with open(scaler_file, 'rb') as f:                return pickle.load(f)        else:            scaler = MinMaxScaler(feature_range=(0, 1))            with open(scaler_file, 'wb') as f:                pickle.dump(scaler, f)            return scaler    else:        return MinMaxScaler(feature_range=(0, 1))def scale_features(scaler: MinMaxScaler = None, features: list = []):    return scaler.fit_transform(features)def get_lstm_model(use_cache: bool = False) -> Sequential:    model_file = 'data/lstm_model.h5'    if use_cache:        if os.path.exists(model_file):            # Load the model            return load_model(model_file)        else:            # Train the LSTM model and save it            model = Sequential()            model.add(LSTM(units=lstm_units, activation='tanh', input_shape=(seq_length, 5)))            model.add(Dropout(0.2))            model.add(Dense(units=1))            model.compile(optimizer='adam', loss='mean_squared_error')            model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test))            # Save the entire model to a HDF5 file            model.save(model_file)            return model    else:        # Train the LSTM model        model = Sequential()        model.add(LSTM(units=lstm_units, activation='tanh', input_shape=(seq_length, 5)))        model.add(Dropout(0.2))        model.add(Dense(units=1))        model.compile(optimizer='adam', loss='mean_squared_error')        model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test))        return modeldef get_predicted_x_test_prices(x_test: np.ndarray = None):    predicted = model.predict(x_test)    # Create a zero-filled matrix to aid in inverse transformation    zero_filled_matrix = np.zeros((predicted.shape[0], 5))    # Replace the 'close' column of zero_filled_matrix with the predicted values    zero_filled_matrix[:, 3] = np.squeeze(predicted)    # Perform inverse transformation    return scaler.inverse_transform(zero_filled_matrix)[:, 3]def plot_x_test_actual_vs_predicted(actual_close_prices: list = [], predicted_x_test_close_prices = []) -> None:    # Plotting the actual and predicted close prices    plt.figure(figsize=(14, 7))    plt.plot(actual_close_prices, label='Actual Close Prices', color='blue')    plt.plot(predicted_x_test_close_prices, label='Predicted Close Prices', color='red')    plt.title('Actual vs Predicted Close Prices')    plt.xlabel('Time')    plt.ylabel('Price')    plt.legend()    plt.show()def predict_next_close(df: pd.DataFrame = None, scaler: MinMaxScaler = None) -> float:    # Take the last X days of data and scale it    last_x_days = df.iloc[-seq_length:][['open', 'high', 'low', 'close', 'volume']].values    last_x_days_scaled = scaler.transform(last_x_days)    # Reshape this data to be a single sequence and make the prediction    last_x_days_scaled = np.reshape(last_x_days_scaled, (1, seq_length, 5))    # Predict the future close price    future_close_price = model.predict(last_x_days_scaled)    # Create a zero-filled matrix for the inverse transformation    zero_filled_matrix = np.zeros((1, 5))    # Put the predicted value in the 'close' column (index 3)    zero_filled_matrix[0, 3] = np.squeeze(future_close_price)    # Perform the inverse transformation to get the future price on the original scale    return scaler.inverse_transform(zero_filled_matrix)[0, 3]def evaluate_model(x_test: list = []) -> None:    # Evaluate the model    y_pred = model.predict(x_test)    mse = mean_squared_error(y_test, y_pred)    mae = mean_absolute_error(y_test, y_pred)    rmse = np.sqrt(mse)    print(f'Mean Squared Error: {mse}')    print(f'Mean Absolute Error: {mae}')    print(f'Root Mean Squared Error: {rmse}')

咱们需刺眼指出的是,在万般函数中增添了“use_cache”变量。此政策意在镌汰对 EODHD 应用程序接口的冗余 API 调用,堤防诓骗沟通的逐日数据对模子进行类似的再行磨真金不怕火。激活“use_cache”变量会将数据存储至“data/”目次下的文献里。若数据不存在,则会创建;若已存在,则会加载。当屡次运行剧本时,此法子能显耀升迁服从。若要在每次运行时取得新数据,只需在调用函数时禁用“use_cache”选项或清空“data/”目次中的文献,就能得到沟通的收尾。

咫尺干预代码的中枢部分…

if __name__ == '__main__':    # Retrieve 3369 days of S&P 500 data    df = get_ohlc_data(use_cache=True)    print(df)

来源,咱们从 EODHD API  取得 OHLCV 数据,并将其存入名为 “df “的 Pandas DataFrame。OHLCV 暗示开盘价、最高价、最廉价、收盘价和成交量,是走动烛炬图数据的圭臬属性。如前所述,咱们启用了缓存以简化经过。咱们还不错接受在屏幕上表示这些数据。

图片

咱们将一次性先容以下代码块…

    features = get_features(df)    target = get_target(df)    scaler = get_scaler(use_cache=True)    scaled_features = scale_features(scaler, features)    x, y = create_sequences(scaled_features, seq_length)    train_size = int(0.8 * len(x))  # Create a train/test split of 80/20%    x_train, x_test = x[:train_size], x[train_size:]    y_train, y_test = y[:train_size], y[train_size:]    # Re-shape input to fit lstm layer    x_train = np.reshape(x_train, (x_train.shape[0], seq_length, 5))  # 5 features    x_test = np.reshape(x_test, (x_test.shape[0], seq_length, 5))  # 5 features

“features” 包括咱们将用来展望方向(即 “close”)的一系列输入。

“target” 包含一个方向值列表,如 “close“。

“scaler”代表一种用于将数字圭臬化的法子,使它们具有可比性。举例,咱们的数据集启动时的接近值可能是 784,终末可能是 3538。终末一瞥的数字越高,并不虞味着展望的兴致兴致越大。归一化可确保可比性。

“scaled_features” 是缩放过程的收尾,咱们将用它来磨真金不怕火东谈主工智能模子。

“x_train” and “x_test” 分离暗示咱们将用于磨真金不怕火和测试东谈主工智能模子的数据集,常常的作念法是 80/20 分拨。这意味着 80% 的走动数据用于磨真金不怕火,20% 用于测试模子。x “暗示这些特征或输入。

“y_train” and “y_test” 的功能类似,但只包含方向值,如 “close”。

终末,必须对数据进行重塑,以忻悦 LSTM 层的条件。

咱们开垦了一种功能,既能对模子进行再行磨真金不怕火,又能载入之前已磨真金不怕火好的模子。

model = get_lstm_model(use_cache=True)

图片

从表示的图片中不错一窥磨真金不怕火序列。你会发现,领先, “loss”和 “val_loss” 主张可能并不完全一致。不外,跟着磨真金不怕火的进行,这些数据有望趋于一致,这标明磨真金不怕火取得了进展。

Loss: 这是在磨真金不怕火数据集上诡计的均方误差(MSE)。它反馈了每个磨真金不怕火期展望标签和竟然标签之间的“cost” 或 “error” 。咱们的方向是通过运动的历时来减少这一数字。

Val_loss: 这个均方误差是在考据数据集上深信的,用于臆想模子在磨真金不怕火过程中未遭受的数据上的发扬。它是模子泛化到新的未见数据才略的主张。

检验测试集的展望收盘价列表,不错使用此代码。

    predicted_x_test_close_prices = get_predicted_x_test_prices(x_test)    print('Predicted close prices:', predicted_x_test_close_prices)

单看这些数据,可能并不稀薄具有启发性或直不雅。不外,通过绘画本体收盘价与展望收盘价的对比图(请刺眼,这只占总共这个词数据集的 20%),咱们不错得到更明晰的图像,如下图所示。

# Plot the actual and predicted close prices for the test data    plot_x_test_actual_vs_predicted(df['close'].tail(len(predicted_x_test_close_prices)).values, predicted_x_test_close_prices)

图片

收尾标明,在测试阶段,该模子在展望收盘价方面发扬出色。

咫尺,咱们来望望最令东谈主期待的方面:咱们能深信来日的展望收盘价吗?

   # Predict the next close price    predicted_next_close =  predict_next_close(df, scaler)    print('Predicted next close price:', predicted_next_close)Predicted next close price: 3536.906685638428

这是一个用于进修主张的基本示例,只是是一个启动。从这里启动,您不错计议加入更多的磨真金不怕火数据,调遣超参数,或将模子应用于不同的阛阓和时辰区间。如果您念念对模子进行评估,不错将其包括在内。

 # Evaluate the model    evaluate_model(x_test)

在咱们的决策中的输出情况是

Mean Squared Error: 0.00021641664334765608Mean Absolute Error: 0.01157513692221611Root Mean Squared Error: 0.014711106122506767

“平均平日误差”(mean_squared_error)和 “平均完满误差”(mean_absolute_error)函数来自 scikit-learn 的度量模块,分离用于诡计平均平日误差(MSE)和平均完满误差(MAE)。均方根误差 (RMSE) 是通过对 MSE 取平日根得出的。

这些主张为模子的准确性提供了数字化的评估,也为模子的性能进行了定量的分析,而图形化的展示则更有益于直不雅地对比展望值与本体数值,以及直不雅地比拟展望值和本体值。

三、总结

在本文中我详备先容了用 Python 和 AI 作念走动展望的经过。来源是各式展望办法,像 Facebook 的 Prophet、SARIMA 模子、多项式追忆,还有基于东谈主工智能的轮回神经收罗(RNN),这内部我合计 LSTM 模子最横暴。LSTM 模子是种特殊的递归神经收罗,能处理序列展望问题,还处置了圭臬 RNN 的消成仇梯度爆炸问题,符合时辰序列展望和当然话语处理这些任务。

接下来,我给群众提供了一个主张考据的准备体式,包括装配Python和PIP、创建边幅和文献、设置假造环境以及创建requirements.txt文献。还包括 VSCode的设置文献示例,以及本边幅的 GitHub 代码仓库。

而在设置代码的部分,我详备走漏了怎样导入必要的库和调用 EODHD API’s,并先容了一系列可重用的函数,这些函数用于取得数据、创建序列、取得特征和方向值、缩放特征、取得LSTM模子、进行展望以及评估模子。此外,咱们还商议了怎样使用缓存来减少无用要的API调用和数据类似加载。

终末,本文展示了怎样使用这些函数来磨真金不怕火和测试LSTM模子,并展示了怎样展望下一个往过去的收盘价。通过比拟本体收盘价和展望收盘价的图表,以及诡计均方误差(MSE)、均方根误差(RMSE)和均完满误差(MAE)等主张,来评估模子的性能。节略总结起来便是底下6句话:

LSTM模子在走动展望中的恶果优于其他法子,因为它简略更好地处理永恒依赖问题。

使用缓存机制不错提高数据处理的服从,幸免类似的API调用和模子磨真金不怕火。

通过可视化本体和展望的收盘价,以及诡计有计划的误差主张,不错直不雅地评估模子的展望准确性。

模子的磨真金不怕火和测试应该使用不同的数据集,以确保模子的泛化才略。

调遣超参数和使用额外的磨真金不怕火数据不错进一步提高模子的性能。

模子的展望收尾不错看成走动决策的参考,但应严慎使用,因为展望并不老是准确的。

本文内容只是是技巧探讨和学习,并不组成任何投资提议。

转发请注明原作家和出处开云kaiyun中国官方网站。

本站仅提供存储工作,总共内容均由用户发布,如发现存害或侵权内容,请点击举报。

Powered by 滚球app官网 @2013-2022 RSS地图 HTML地图