在python中使用随机森林模型进行预测

时间:2017-07-07 23:23:48

标签: python pandas scikit-learn classification random-forest

我将此three column dataset格式化为以下

t_stamp,Xval,Ytval
0.000543,0,10
0.000575,0,10
0.041324,1,10
0.041331,2,10
0.041336,3,10
0.04134,4,10
0.041345,5,10
0.04135,6,10
0.041354,7,10

如何使用Y Xval random forest classifier model sklearn Python的{​​{1}}的当前值[0,0,1,2,3]预测Xval的当前值}?将Ytval列的rolling OLS regression model作为输入的含义 - 我想预测random forest model的第5行值。使用简单的import pandas as pd df = pd.read_csv('data_pred.csv') model = pd.stats.ols.MovingOLS(y=df.Ytval, x=df[['Xval']], window_type='rolling', window=5, intercept=True) ,我们可以按照以下方式执行此操作,但我想使用$('#exampleModal').on('show.bs.modal', function (event) { modal.find('.modal-body input').val('new value') }) 执行此操作。

Channel

1 个答案:

答案 0 :(得分:1)

您可以通过改变数据来实现滚动输入数据,以便X的最后5个值中的每一个都成为它自己的特征:

import pandas as pd
from io import StringIO
from sklearn.ensemble import RandomForestRegressor

data = StringIO("""t_stamp,Xval,Ytval
0.000543,0,10
0.000575,0,10
0.041324,1,10
0.041331,2,10
0.041336,3,10
0.04134,4,10
0.041345,5,10
0.04135,6,10
0.041354,7,10""")

df = pd.read_csv(data)

for i in range(1,6):
    df['Xval_t'+str(i)] = df['Xval'].shift(i)

产生df

t_stamp    Xval Ytval   Xval_t1 Xval_t2 Xval_t3 Xval_t4 Xval_t5
0.000543    0   10      NaN     NaN     NaN     NaN     NaN
0.000575    0   10      0.0     NaN     NaN     NaN     NaN
0.041324    1   10      0.0     0.0     NaN     NaN     NaN
0.041331    2   10      1.0     0.0     0.0     NaN     NaN
0.041336    3   10      2.0     1.0     0.0     0.0     NaN
0.041340    4   10      3.0     2.0     1.0     0.0     0.0
0.041345    5   10      4.0     3.0     2.0     1.0     0.0
0.041350    6   10      5.0     4.0     3.0     2.0     1.0
0.041354    7   10      6.0     5.0     4.0     3.0     2.0

当然,您需要决定如何处理NaNs。我只是为了示范而放弃它们。

df.dropna(inplace=True)

X = df[['Xval', 'Xval_t1', 'Xval_t2', 'Xval_t3', 'Xval_t4', 'Xval_t5']].values
y = df['Ytval'].values

reg = RandomForestRegressor()
reg.fit(X,y)
print(reg.predict(X))

结果:

[ 10.  10.  10.  10.]