Python Forum
Can't make Random Forest Prediction work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't make Random Forest Prediction work
#1
Hi guys,

I am trying to predict the target variable "Exchange Rate EURUSD" using a data set that I created by myself. I included a lot of economic indicators and the monthly EURUSD closing price from 01/01/2000 to 01/12/2020, which gives me 240 columns ( 20 years * 12 months ) and 34 features ( 33 indicators and 1 EURUSD exchange rate)

Here is one sample of the data set :

https://imgur.com/a/iJuGtXw

I kept the date as an index and also replaced all "," with "." in python. All in all the code looks like this :

# Load Data Set

df = pd.read_csv("C:/merged.csv")
df = df.set_index('Date')
df["EURUSD Closing Price"] = df["EURUSD Closing Price"].replace(',', '.', regex=True).astype(float)

# Define variables

X = df.drop(["EURUSD Closing Price"], axis=1).values
y = df["EURUSD Closing Price"].values

# Split data into 75 train / 25 test

X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train = X[:int(X.shape[0]*0.75)]
X_test = X[int(X.shape[0]*0.75):]
y_train = y[:int(X.shape[0]*0.75)]
y_test = y[int(X.shape[0]*0.75):]

# RF Train and predict

# RF

rf = RandomForestRegressor(n_estimators = 1000, random_state = 42)
rf.fit(X_train, y_train)
rf.predict(y_train)
First of all, I have to keep the time series nature in the data, thats why the data can'T be split randomly into a train and test set.
Moreover, I know you should not predict on training set, but on test set. But I get an error even with that code :

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

If i do that, I get the new error :

ValueError: Number of features of the model must match the input. Model n_features is 33 and input n_features is 1

I kind of understand what the problem is, but I have no idea how to fix it. I would appreciate any help a lot !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Forest to Identify Page: Feature Selection JaneTan 0 1,290 Oct-14-2021, 09:40 AM
Last Post: JaneTan
  Prediction of Coal Fire Power Plant Pollutants Emission Dalpi 2 2,116 May-08-2020, 06:28 PM
Last Post: Dalpi
  prediction using linear regression (extrapolation?) in a loop karlito 0 3,179 Feb-05-2020, 10:56 AM
Last Post: karlito
  Random Forest Hyperparamter Optimization donnertrud 1 1,904 Jan-17-2020, 06:30 AM
Last Post: scidam
  Random Forest high R2 Score but poor prediction donnertrud 5 4,844 Jan-13-2020, 11:23 PM
Last Post: jefsummers
  AUCPR of individual features using Random Forest (Error: unhashable Type) melissa 1 3,280 Jul-10-2017, 12:48 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020