Esempio n. 1
0
 def mae(self):
     actual = self.get_series()
     fitted = self.get_fitted_values()
     return ml_metrics.mae(actual, fitted)
Esempio n. 2
0
# Определяем p и q
# fig = plt.figure(figsize=(12,8))
# ax1 = fig.add_subplot(211)
# fig = sm.graphics.tsa.plot_acf(otg1diff.values.squeeze(), lags=25, ax=ax1)
# ax2 = fig.add_subplot(212)
# fig = sm.graphics.tsa.plot_pacf(otg1diff, lags=25, ax=ax2)

p=1
q=1
d=1
# прогноз(Построение ARIMA модели)
dat = '2018-11-30'
src_data_model = otg[:dat]
model = sm.tsa.ARIMA(src_data_model, order=(p,d,q), freq='M').fit(disp=0)
print(model.summary())
# остатки модели и построение для них ACF
q_test = sm.tsa.stattools.acf(model.resid, qstat=True) #свойство resid, хранит остатки модели, qstat=True, означает что применяем указынный тест к коэф-ам
#Расчет коэффициента детерминации R^2
pred = model.predict(dat,'2018-12-31', typ='levels')
trn = otg[dat:'2018-11-30']
l = len(trn)+1
r2 = r2_score(trn, pred[1:l])
print ('R^2: %1.2f' % r2)
print("Среднеквадратичное отклонение нашей модели:")
print(metrics.rmse(trn,pred[1:l]))
print("Средняя абсолютная ошибка прогноза:")
print(metrics.mae(trn,pred[1:l]))

otg.plot()
pred.plot(style='r--')
plt.show()
print 'p-value: ', sm.stats.CompareMeans(r1, r2).ttest_ind()[1]

otg1diff.plot(figsize=(12, 6))

fig = plt.figure(figsize=(12, 8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(otg1diff.values.squeeze(), lags=25, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(otg1diff, lags=25, ax=ax2)
src_data_model = otg[:'2013-05-26']
model = sm.tsa.ARIMA(src_data_model, order=(1, 1, 1),
                     freq='W').fit(full_output=False, disp=0)

print model.summary()

q_test = sm.tsa.stattools.acf(
    model.resid, qstat=True)  #свойство resid, хранит остатки модели
#qstat=True, означает что применяем указынный тест к коэф-ам
print DataFrame({'Q-stat': q_test[1], 'p-value': q_test[2]})

pred = model.predict('2013-05-26', '2014-12-31', typ='levels')
trn = otg['2013-05-26':]

r2 = r2_score(trn, pred[1:32])
print 'R^2: %1.2f' % r2

metrics.rmse(trn, pred[1:32])
metrics.mae(trn, pred[1:32])

otg.plot(figsize=(12, 6))
pred.plot(style='r--')
def scores(actuals, predicteds):
    rmses = rmse(actual=actuals, predicted=predicteds)
    mses = mean_squared_error(actuals, predicteds)
    maes = mae(actual=actuals, predicted=predicteds)
    r2s = r2_score(actuals, predicteds)
    return rmses, maes, r2s, mses
Esempio n. 5
0
# pred = model.predict(start=src_data_model.shape[0], end=src_data_model.shape[0]+100)
pred = model.predict(start='2017-10-25 00:00:00', end='2017-10-30 00:00:00')
trn = p['2017-10-25 00:00:00':'2017-10-30 00:00:00']
print(pred)
# pred.plot(figsize=(12, 8), color='red')
plt.show()
# r2 = r2_score(trn, pred[1:32])
# print('R_2= %1.2f' % r2)

# RMSE for ARIMA
rmse = metrics.rmse(trn, pred)
print(rmse)
print(type(rmse))

# MAE for ARIMA
mae = metrics.mae(trn, pred)
print(mae)

scale = 1
deviation = float(rmse)
lower = pred - deviation * scale
lowerDF = pd.DataFrame({'Box': lower.values}, index=lower.index)
print(lowerDF)
lower_arr = lowerDF.as_matrix().squeeze()
p_arr = p.loc[lowerDF.index].as_matrix().squeeze()
pred_arr = pred.loc[lowerDF.index].as_matrix().squeeze()
print(p_arr)
print(pred_arr)
#lowerDF.index = p.index
print("lower =", type(lower_arr))
print(lower_arr)
Esempio n. 6
0
r1 = sm.stats.DescrStatsW(otg1diff[m:])
r2 = sm.stats.DescrStatsW(otg1diff[:m])
print 'p-value: ', sm.stats.CompareMeans(r1,r2).ttest_ind()[1]

otg1diff.plot(figsize=(12,6))

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(otg1diff.values.squeeze(), lags=25, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(otg1diff, lags=25, ax=ax2)
src_data_model = otg[:'2013-05-26']
model = sm.tsa.ARIMA(src_data_model, order=(1,1,1), freq='W').fit(full_output=False, disp=0)

print model.summary()

q_test = sm.tsa.stattools.acf(model.resid, qstat=True) #свойство resid, хранит остатки модели 
                                                            #qstat=True, означает что применяем указынный тест к коэф-ам
print DataFrame({'Q-stat':q_test[1], 'p-value':q_test[2]})

pred = model.predict('2013-05-26','2014-12-31', typ='levels')
trn = otg['2013-05-26':]

r2 = r2_score(trn, pred[1:32])
print 'R^2: %1.2f' % r2

metrics.rmse(trn,pred[1:32])
metrics.mae(trn,pred[1:32])

otg.plot(figsize=(12,6))
pred.plot(style='r--')
Esempio n. 7
0
    trainer.trainEpochs(1)
    predictedA, actualA = predict(n, trainData['input'], trainData['target'])
    error = metrics.rmse(actualA, predictedA)
    predictedA, actualA = predict(n, tstdata['input'], tstdata['target'])
    error2 = metrics.rmse(actualA, predictedA)

    graph.append((i, error, error2))

with open('results/graphs/'+filename, 'w') as fp:
    a = csv.writer(fp, delimiter=',')
    a.writerows(graph)


#
# Write the output of the final network
#
predictedA, actualA = predict(n, tstdata['input'], tstdata['target'])

print "MAPE: ", mape(actualA, predictedA)
print "RMSE: ", metrics.rmse(actualA, predictedA)
print "MAE: ", metrics.mae(actualA, predictedA)

data = [["actual", "predicted"]]
data.extend(np.hstack([actualA, predictedA]))

with open('results/'+filename, 'w') as fp:
    a = csv.writer(fp, delimiter=',')
    a.writerows(data)


Esempio n. 8
0
#TestModels.R2_Y2.plot(ax=axes[1], kind='bar', color='green', title='R2_Y2')
#random_forest is the best ->
model = models[1]
model.fit(Xtrn, Ytrn)
#model.summary()
inf=model.feature_importances_
print inf
#then predict with 
#print trn
res=model.predict(trn)
print(res)
#print trg
print "rmse"
m = metrics.rmse(trg.sure.values,res)

err = metrics.mae(trg.sure.values,res)
print m
print "mean_abs_err"
print err
print "in persentage:"
print(100.0*err/(trg.sure.values.max() - trg.sure.values.min()))

#plot.grid()
#plt.show()

fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(fig)
ys = t1.dpr.values
xs = t1.speed.values
#ax.scatter(xs,ys,t1.sure.values, label='Original Scalar Field (DPR,Speed)=>(Migration Time)')
Esempio n. 9
0
 def test_mae(self):
     self.assertAlmostEqual(metrics.mae(range(0, 11), range(1, 12)), 1)
     self.assertAlmostEqual(
         metrics.mae([0, .5, 1, 1.5, 2], [0, .5, 1, 1.5, 2]), 0)
     self.assertAlmostEqual(metrics.mae(range(1, 5), [1, 2, 3, 5]), 0.25)
Esempio n. 10
0
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(otg1diff.values.squeeze(), lags=25, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(otg1diff, lags=25, ax=ax2)
print 'otg'
print otg
src_data_model = otg[:'2015-11-09 03:26:26']
model = sm.tsa.ARIMA(src_data_model, order=(4,1,0)).fit() #trend='nc' if need
print model.summary()
q_test = sm.tsa.stattools.acf(model.resid, qstat=True)
print DataFrame({'Q-stat':q_test[1], 'p-value':q_test[2]})

pred = model.predict('2015-11-09 03:26:16','2015-11-09 03:29:06', typ='levels')
trn = otg['2015-11-09 03:26:26':]
r2 = r2_score(trn, pred)
print 'R^2: %1.2f' % r2
#mean-square rmse

metrics.rmse(trn,pred)

metrics.mae(trn,pred)

fig, (ax1) = plt.subplots(nrows = 1, sharex=True)
ax1.plot(otg.index,otg.values)

ax1.plot_date(pred.index,pred.values,'r--')

plt.show()

#print pred.values
Esempio n. 11
0
def run_all_models():
    x_dense_train, y_train, x_dense_test, x_dense_valid, x_sparse_train,\
        x_sparse_test, x_sparse_valid, y_test, x_char_train, x_char_test, \
        x_char_valid, valid_index \
        = get_dat_data()
    models_dense = [rf_model, gradient_model, svr_model, mlp_model,
                    gradient_model_lad, ada_model]
    models_new = [neighbors_model]
    all_test_pred = []
    all_valid_pred = []
    print('fitting fancy_text_model')
    ftext_test_pred, fvalid_pred = fancy_text_model(x_sparse_train, y_train,
                                                    x_sparse_test,
                                                    x_sparse_valid,
                                                    'text_model.pkl',
                                                    use_cache=True)
    print('MAE of fancy_text_model')
    print(mae(y_test, ftext_test_pred))
    all_test_pred.append(ftext_test_pred)
    all_valid_pred.append(fvalid_pred)
    print('fitting sgd_text_model')
    sgd_text_test_pred, sgd_text_valid_pred = sgd_text_model(x_sparse_train, y_train,
                                                             x_sparse_test,
                                                             x_sparse_valid,
                                                             'sgdtext_model.pkl',
                                                             use_cache=False)
    print('MAE of sgd_text_model')
    print(mae(y_test, sgd_text_test_pred))
    all_test_pred.append(sgd_text_test_pred)
    all_valid_pred.append(sgd_text_valid_pred)
    print('fitting fancy_text_model on char')
    fchar_test_pred, fchar_pred = fancy_text_model(x_char_train, y_train,
                                                   x_char_test,
                                                   x_char_valid,
                                                   'char_model.pkl',
                                                   use_cache=True)
    print('MAE of fancy_text_model on char')
    print(mae(y_test, fchar_test_pred))
    all_test_pred.append(fchar_test_pred)
    all_valid_pred.append(fchar_pred)
    sgd_char_test_pred, sgd_char_valid_pred = sgd_text_model(x_char_train, y_train,
                                                             x_char_test,
                                                             x_char_valid,
                                                             'sgdchar_model.pkl',
                                                             use_cache=False)
    print('MAE of sgd_text_model on char')
    print(mae(y_test, sgd_char_test_pred))
    all_test_pred.append(sgd_char_test_pred)
    all_valid_pred.append(sgd_char_valid_pred)
    normer = StandardScaler()
    x_dense_train = normer.fit_transform(x_dense_train)
    x_dense_test = normer.transform(x_dense_test)
    x_dense_valid = normer.transform(x_dense_valid)
    print(x_dense_valid.shape)
    for model in models_dense:
        print('fitting %s' % str(model.func_name))
        cache_name = 'full' + model.func_name + '.pkl'
        t_test_pred, t_valid_pred = model(x_dense_train, y_train,
                                          x_dense_test, x_dense_valid,
                                          cache_name, use_cache=True)
        print('MAE of %s' % str(model.func_name))
        print(mae(y_test, t_test_pred))
        all_test_pred.append(t_test_pred)
        all_valid_pred.append(t_valid_pred)
    for model in models_new:
        print('fitting %s' % str(model.func_name))
        cache_name = 'full' + model.func_name + '.pkl'
        t_test_pred, t_valid_pred = model(x_dense_train, y_train,
                                          x_dense_test, x_dense_valid,
                                          cache_name, use_cache=False)
        print('MAE of %s' % str(model.func_name))
        print(mae(y_test, t_test_pred))
        all_test_pred.append(t_test_pred)
        all_valid_pred.append(t_valid_pred)
    print('ensembling predictions')
    all_test_pred = np.vstack(all_test_pred).T
    all_valid_pred = np.vstack(all_valid_pred).T
    all_test_pred = normer.fit_transform(all_test_pred)
    all_valid_pred = normer.transform(all_valid_pred)
    test_pred, valid_pred = ensembler(all_test_pred, y_test, all_valid_pred)
    print('MAE of ensemble')
    print(mae(y_test, test_pred))
    write_soln(valid_pred, valid_index)
Esempio n. 12
0
 def test_mae(self):
     self.assertAlmostEqual(metrics.mae(range(0,11), range(1,12)), 1)
     self.assertAlmostEqual(metrics.mae([0,.5,1,1.5,2], [0,.5,1,1.5,2]), 0)
     self.assertAlmostEqual(metrics.mae(range(1,5), [1,2,3,5]), 0.25)