我刚接触Python超过6个月(新手)。我尝试实现此代码来训练从Python Metatrader5获取的数据。当我运行代码时,我得到一个好像数组为空的错误,我似乎不明白为什么数组为空。我在这3天里一直试图弄清楚为什么?所以我求助于您的所有专家。
import numpy as np
import pandas as pd
import pytz
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from pandas import DataFrame
from datetime import datetime
from MetaTrader5 import *
from pytz import timezone
#Access metatrader
MT5Initialize()
MT5WaitForTerminal()
utc_tz = timezone('UTC')
# import the pandas module for displaying obtained data in the tabular form
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500) # max table width to display
# GET rates from MT5
rates = MT5CopyRatesFrom("EURUSD", MT5_TIMEFRAME_H1, utc_from, 5)
df = DataFrame(rates, columns= ['time', 'open', 'low', 'high', 'close',
'tick_volume', 'spread', 'real_volume'])
df = df[['close']]
#FORECAST DATA
# A variable for predicting 'n' days out into the future
forecast_out = 5 #'n=30' days
#Create another column (the target or dependent variable) shifted 'n' units up
df['Prediction'] = df[['close']].shift(-forecast_out)
### Create the independent data set (X) #######
# Convert the dataframe to a numpy array
X = np.array(df.drop(['Prediction'],1))
#Remove the last 'n' rows
X = X[:-forecast_out]
### Create the dependent data set (y) #####
# Convert the dataframe to a numpy array (All of the values including the NaN's)
y = np.array(df['Prediction'])
# Get all of the y values except the last 'n' rows
y = y[:-forecast_out]
# Split the data into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create and train the Support Vector Machine (Regressor)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_rbf.fit(x_train, y_train)
# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
svm_confidence = svr_rbf.score(x_test, y_test)
# Create and train the Linear Regression Model
lr = LinearRegression()
# Train the model
lr.fit(x_train, y_train)
# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
lr_confidence = lr.score(x_test, y_test)
# Set x_forecast equal to the last 30 rows of the original data set from Adj. Close column
x_forecast = np.array(df.drop(['Prediction'],1))[-forecast_out:]
# Print linear regression model predictions for the next 'n' days
lr_prediction = lr.predict(x_forecast)
print("Linear Price: ", lr_prediction)
# Print support vector regressor model predictions for the next 'n' days
svm_prediction = svr_rbf.predict(x_forecast)
print("Prediction: ", svm_prediction)
ERROR:
**ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.**