{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0MidPxChange_1MidPxChange_5MidPxChange_10MidPxChange_20MidPxChange_50MidPxChange_100MidPxChange_200MidPxVolRatio_1MidPxVolRatio_5...label2lastbidaskbid_volask_volvolumeturnoverpredictpredict_0226
02023-12-17 21:00:00.250-2.0-2.0-2.0-2.0-2.0-2.0-2.0-0.001403-0.001403...8.11807.01807.01808.075.047.04007.07245815.00.2696110.190654
12023-12-17 21:00:00.5000.0-2.0-2.0-2.0-2.0-2.0-2.00.000000-0.001218...8.11807.01807.01808.025.012.04223.07636347.00.8897560.027161
\n", "

2 rows × 128 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 MidPxChange_1 MidPxChange_5 MidPxChange_10 \\\n", "0 2023-12-17 21:00:00.250 -2.0 -2.0 -2.0 \n", "1 2023-12-17 21:00:00.500 0.0 -2.0 -2.0 \n", "\n", " MidPxChange_20 MidPxChange_50 MidPxChange_100 MidPxChange_200 \\\n", "0 -2.0 -2.0 -2.0 -2.0 \n", "1 -2.0 -2.0 -2.0 -2.0 \n", "\n", " MidPxVolRatio_1 MidPxVolRatio_5 ... label2 last bid ask \\\n", "0 -0.001403 -0.001403 ... 8.1 1807.0 1807.0 1808.0 \n", "1 0.000000 -0.001218 ... 8.1 1807.0 1807.0 1808.0 \n", "\n", " bid_vol ask_vol volume turnover predict predict_0226 \n", "0 75.0 47.0 4007.0 7245815.0 0.269611 0.190654 \n", "1 25.0 12.0 4223.0 7636347.0 0.889756 0.027161 \n", "\n", "[2 rows x 128 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_data.head(2)" ] } ], "metadata": { "kernelspec": { "display_name": "pytorch", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 2 }