2
$\begingroup$

I am fairly new to Sklearn and machine learning and have encountered an issue when using SVR with an RBF kernel. Below is my predicted data compared directly with my real data:

enter image description here

I do not know what I am doing wrong and was wondering if any of you could help me.

Below is my code:

scaler=MinMaxScaler(feature_range=(0,1))
data=scaler.fit_transform(df['close'].values.reshape(-1,1).astype('float32'))

def split_data(data, test_size=0.2):
    split=len(data)-int(test_size * len(data))
    train=data[:split]
    test=data[split:]

    return train,test

xtrain,xtest=split_data(data)
ytrain,ytest=np.roll(xtrain,-1)[:-1].ravel(), np.roll(xtest,-1)[:-1].ravel()
xtrain,xtest=xtrain[:-1], xtest[:-1]

svr_poly=SVR(kernel='poly', C=1e3, degree=2)
svr_lin=SVR(kernel='linear', C=1e3)
svr_rbf=SVR(kernel='rbf', C=1e3, gamma=0.1)

lin_regr=LinearRegression()

svr_poly.fit(xtrain,ytrain)
svr_lin.fit(xtrain,ytrain)
svr_rbf.fit(xtrain,ytrain)

lin_regr.fit(xtrain,ytrain)

Thanks!

EDIT

The reason I only have one feature is because I want to include it in the following loop to predict prices given the past prediction:

def predict(iterations, data, buy_threshold=0.2, sell_threshold=0.2):

    preds=[data[-1]]
    lin_preds=[data[-1]]
    mean_preds=[]

    buysell=[]
    for d in range(iterations):
        pred=svr_poly.predict(preds[-1].reshape(-1,1))
        lin_pred=svr_lin.predict(preds[-1].reshape(-1,1))


        ### BUY/SELL/KEEP
        # MEAN AVERAGE OF LINEAR AND POLYNOMIAL PREDICTIONS
        mean_pred=(pred+lin_pred)/2

        # NEXT AVERAGE PREDICTION
        next_mean_pred=(svr_poly.predict(mean_pred.reshape(-1,1))+lin_regr.predict(mean_pred.reshape(-1,1)))/2

        if(next_mean_pred >= mean_pred*(1-buy_threshold)):
            buysell.append('BUY')
        elif(next_mean_pred <= mean_pred*(1+sell_threshold)):
            buysell.append('SELL')
        else:
            buysell.append('KEEP')


        ### APPEND PREDICTIONS TO LISTS
        preds.append(pred)
        lin_preds.append(lin_pred)
        mean_preds.append(mean_pred)

    gap=preds[0]-preds[1]
    for i in range(len(preds)):
        preds[i]+=gap

    return np.concatenate(tuple(preds[1:])), np.concatenate(tuple(lin_preds[1:])), np.concatenate(tuple(mean_preds)), buysell

Also here is my training data outputs plotted: enter image description here

$\endgroup$
2
  • $\begingroup$ Is this a model with only one feature? $\endgroup$ Nov 18, 2018 at 17:30
  • $\begingroup$ @anymous.asker I think so... I was half following a tutorial on Python36 $\endgroup$
    – M Patel
    Nov 19, 2018 at 18:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.