{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\alex\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\utils\\validation.py:1183: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", " y = column_or_1d(y, warn=True)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Mean Squared Error: 14.500409172440818\n", "R-squared: 0.001036444647256718\n", "Feature: CL=F, Importance: 0.008524010146332462\n", "Feature: 000001.SS, Importance: 0.004995977996405195\n", "Feature: 000001.SS_ch, Importance: 0.0036955582990245224\n", "Feature: ^N225_ch, Importance: 0.0034735870214927596\n", "Feature: GC=F, Importance: 0.0031737626273991458\n", "Feature: volume_ch, Importance: 0.00313858730134845\n", "Feature: ^N225, Importance: 0.0023746946720944093\n", "Feature: ^N100, Importance: 0.0020009681023409388\n", "Feature: GC=F_ch, Importance: 0.0009988611986898302\n", "Feature: open_ch, Importance: 0.0005357030536394802\n", "Feature: ^DJI, Importance: 0.00038253958034100886\n", "Feature: volume, Importance: 0.00035850468199169667\n", "Feature: HG=F_ch, Importance: -3.7204663828149796e-05\n", "Feature: AAPL_ch, Importance: -0.0010938202194113419\n", "Feature: NVDA_ch, Importance: -0.0020599321131733314\n" ] } ], "source": [ "from sklearn.svm import SVR\n", "from sklearn.metrics import mean_squared_error\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.inspection import permutation_importance\n", "import pandas as pd\n", "\n", "# Load your data\n", "X = pd.read_csv(\"data/train_features.csv\", index_col=0)\n", "y = pd.read_csv(\"data/train_target.csv\", index_col=0)\n", "\n", "# Split the data into a training set and a test set\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n", "# Train your SVR model with the RBF kernel\n", "model = SVR(kernel='rbf', C=1.0, epsilon=0.1)\n", "model.fit(X_train, y_train)\n", "\n", "# Calculate and print the R-squared score\n", "y_pred = model.predict(X_test)\n", "mse = mean_squared_error(y_test, y_pred)\n", "r2 = model.score(X_test, y_test)\n", "print(f\"Mean Squared Error: {mse}\")\n", "print(f\"R-squared: {r2}\")\n", "\n", "# Calculate feature importances using permutation importance\n", "perm_importance = permutation_importance(model, X_test, y_test, n_repeats=30, random_state=42)\n", "\n", "# Get feature importances\n", "importances = perm_importance.importances_mean\n", "\n", "# Pair feature names with their importances\n", "feature_names = X.columns\n", "feature_importances = list(zip(feature_names, importances))\n", "\n", "# Sort feature importances in descending order\n", "sorted_feature_importances = sorted(feature_importances, key=lambda x: x[1], reverse=True)\n", "\n", "# Print the most important features\n", "for feature, importance in sorted_feature_importances:\n", " print(f\"Feature: {feature}, Importance: {importance}\")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | CL=F | \n", "^N100 | \n", "000001.SS_ch | \n", "000001.SS | \n", "GC=F | \n", "volume_ch | \n", "HG=F_ch | \n", "GC=F_ch | \n", "^N225 | \n", "open_ch | \n", "volume | \n", "^DJI | \n", "AAPL_ch | \n", "NVDA_ch | \n", "^N225_ch | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2014-09-19 | \n", "1.727323 | \n", "-1.324818 | \n", "0.522846 | \n", "-2.378455 | \n", "-0.952315 | \n", "0.126741 | \n", "-0.122044 | \n", "-1.121153 | \n", "-1.461648 | \n", "-1.984711 | \n", "-0.853183 | \n", "-1.362974 | \n", "-0.582350 | \n", "-0.804450 | \n", "1.499617 | \n", "
| 2014-09-20 | \n", "1.727323 | \n", "-1.324818 | \n", "-0.013193 | \n", "-2.378455 | \n", "-0.952315 | \n", "-0.217110 | \n", "-0.009643 | \n", "-0.022263 | \n", "-1.461648 | \n", "-1.922540 | \n", "-0.853238 | \n", "-1.362974 | \n", "-0.046092 | \n", "-0.066117 | \n", "-0.025283 | \n", "
| 2014-09-21 | \n", "1.727323 | \n", "-1.324818 | \n", "-0.013193 | \n", "-2.378455 | \n", "-0.952315 | \n", "-0.894279 | \n", "-0.009643 | \n", "-0.022263 | \n", "-1.461648 | \n", "0.863609 | \n", "-0.853772 | \n", "-1.362974 | \n", "-0.046092 | \n", "-0.066117 | \n", "-0.025283 | \n", "
| 2014-09-22 | \n", "1.678999 | \n", "-1.348030 | \n", "-1.573203 | \n", "-2.487532 | \n", "-0.947202 | \n", "-0.390825 | \n", "-1.430568 | \n", "0.137589 | \n", "-1.487321 | \n", "-0.645680 | \n", "-0.853899 | \n", "-1.380079 | \n", "0.019047 | \n", "-0.463145 | \n", "-0.707631 | \n", "
| 2014-09-23 | \n", "1.681171 | \n", "-1.426269 | \n", "0.782683 | \n", "-2.432829 | \n", "-0.932886 | \n", "2.201991 | \n", "-0.109766 | \n", "0.424764 | \n", "-1.487321 | \n", "0.149778 | \n", "-0.852810 | \n", "-1.398742 | \n", "0.982109 | \n", "-0.234968 | \n", "-0.025283 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 2023-07-16 | \n", "0.804830 | \n", "1.943364 | \n", "-0.013193 | \n", "0.124238 | \n", "1.586411 | \n", "0.027942 | \n", "-0.009643 | \n", "-0.022263 | \n", "2.117498 | \n", "-0.082762 | \n", "-0.412722 | \n", "1.389752 | \n", "-0.046092 | \n", "-0.066117 | \n", "-0.025283 | \n", "
| 2023-07-17 | \n", "0.735874 | \n", "1.874161 | \n", "-0.809091 | \n", "0.046890 | \n", "1.560165 | \n", "1.353683 | \n", "-2.003815 | \n", "-0.531030 | \n", "2.117498 | \n", "-0.094837 | \n", "-0.167334 | \n", "1.401945 | \n", "1.092023 | \n", "0.803726 | \n", "-0.025283 | \n", "
| 2023-07-18 | \n", "0.822748 | \n", "1.902077 | \n", "-0.350977 | \n", "0.014348 | \n", "1.644698 | \n", "-0.162629 | \n", "-0.372490 | \n", "1.622831 | \n", "2.140356 | \n", "-0.143650 | \n", "-0.172595 | \n", "1.460514 | \n", "-0.134239 | \n", "0.820340 | \n", "0.280838 | \n", "
| 2023-07-19 | \n", "0.801029 | \n", "1.920276 | \n", "0.016089 | \n", "0.017159 | \n", "1.645721 | \n", "-0.349363 | \n", "-0.385397 | \n", "-0.002609 | \n", "2.229922 | \n", "-0.307087 | \n", "-0.225079 | \n", "1.477974 | \n", "0.418987 | \n", "-0.416178 | \n", "1.170409 | \n", "
| 2023-07-20 | \n", "0.816232 | \n", "1.944849 | \n", "-0.854633 | \n", "-0.063633 | \n", "1.614362 | \n", "0.419767 | \n", "0.402054 | \n", "-0.624792 | \n", "2.139606 | \n", "-0.004257 | \n", "-0.093823 | \n", "1.504171 | \n", "-0.710155 | \n", "-1.384751 | \n", "-1.216259 | \n", "
3227 rows × 15 columns
\n", "SVR(C=0.1, epsilon=0.01, gamma=1)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
SVR(C=0.1, epsilon=0.01, gamma=1)