{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "2526/2526 [==============================] - 2s 757us/step - loss: 0.6998 - val_loss: 0.9270\n", "Epoch 2/10\n", "2526/2526 [==============================] - 2s 745us/step - loss: 0.7014 - val_loss: 0.9254\n", "Epoch 3/10\n", "2526/2526 [==============================] - 2s 647us/step - loss: 0.6917 - val_loss: 0.9253\n", "Epoch 4/10\n", "2526/2526 [==============================] - 2s 657us/step - loss: 0.6858 - val_loss: 0.9270\n", "Epoch 5/10\n", "2526/2526 [==============================] - 2s 718us/step - loss: 0.6889 - val_loss: 0.9264\n", "Epoch 6/10\n", "2526/2526 [==============================] - 2s 730us/step - loss: 0.6945 - val_loss: 0.9289\n", "Epoch 7/10\n", "2526/2526 [==============================] - 2s 651us/step - loss: 0.6867 - val_loss: 0.9274\n", "Epoch 8/10\n", "2526/2526 [==============================] - 2s 672us/step - loss: 0.6895 - val_loss: 0.9267\n", "Epoch 9/10\n", "2526/2526 [==============================] - 2s 656us/step - loss: 0.6938 - val_loss: 0.9278\n", "Epoch 10/10\n", "2526/2526 [==============================] - 2s 634us/step - loss: 0.6906 - val_loss: 0.9264\n", "2539/2539 [==============================] - 1s 334us/step - loss: 0.9264\n", "Test Loss: 0.9263737201690674\n" ] } ], "source": [ "# 读取数据\n", "train_data = pd.read_csv('../generate_data/FG/2023-06-01.csv')\n", "test_data = pd.read_csv('../generate_data/FG/2023-12-18.csv')\n", "\n", "# 选择因子\n", "selected_factors = ['MidPxChange_1', 'MidPxVolRatio_1']\n", "X_train = train_data[selected_factors] # 选择你认为合适的因子列\n", "y_train = train_data['label1']\n", "X_test = test_data[selected_factors]\n", "y_test = test_data['label1']\n", "\n", "\n", "# 数据标准化\n", "scaler = StandardScaler()\n", "X_train_scaled = scaler.fit_transform(X_train)\n", "X_test_scaled = scaler.transform(X_test)\n", "\n", "# 构建神经网络模型\n", "model = Sequential([\n", " Dense(64, activation='relu', input_shape=(X_train_scaled.shape[1],)),\n", " Dense(32, activation='relu'),\n", " Dense(1) # 输出层,因为是回归问题,所以没有激活函数\n", "])\n", "\n", "# 编译模型\n", "model.compile(optimizer='adam', loss='mean_squared_error')\n", "\n", "# 训练模型\n", "model.fit(X_train_scaled, y_train, epochs=10, batch_size=32, validation_data=(X_test_scaled, y_test))\n", "\n", "# 评估模型\n", "loss = model.evaluate(X_test_scaled, y_test)\n", "print('Test Loss:', loss)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "predictions = model.predict(X_test)\n", "test_data['predict_0226'] = predictions" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "test_data.to_csv('/Users/saidcalculationboy/Coding/GenWealth/data/res/FG/2023-12-18.csv')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Unnamed: 0 | \n", "MidPxChange_1 | \n", "MidPxChange_5 | \n", "MidPxChange_10 | \n", "MidPxChange_20 | \n", "MidPxChange_50 | \n", "MidPxChange_100 | \n", "MidPxChange_200 | \n", "MidPxVolRatio_1 | \n", "MidPxVolRatio_5 | \n", "... | \n", "label2 | \n", "last | \n", "bid | \n", "ask | \n", "bid_vol | \n", "ask_vol | \n", "volume | \n", "turnover | \n", "predict | \n", "predict_0226 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "2023-12-17 21:00:00.250 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-0.001403 | \n", "-0.001403 | \n", "... | \n", "8.1 | \n", "1807.0 | \n", "1807.0 | \n", "1808.0 | \n", "75.0 | \n", "47.0 | \n", "4007.0 | \n", "7245815.0 | \n", "0.269611 | \n", "0.190654 | \n", "
1 | \n", "2023-12-17 21:00:00.500 | \n", "0.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "-2.0 | \n", "0.000000 | \n", "-0.001218 | \n", "... | \n", "8.1 | \n", "1807.0 | \n", "1807.0 | \n", "1808.0 | \n", "25.0 | \n", "12.0 | \n", "4223.0 | \n", "7636347.0 | \n", "0.889756 | \n", "0.027161 | \n", "
2 rows × 128 columns
\n", "