def run_with_crossvalidation(self, data, iterations=5):
        """
        This function estimates the performance of the neural network using crossvalidation using a specified dataset.
        Args:
        :param ds (TweetRegressionDatasetFactory): the dataset used to crossvalidate the network.
        :param iterations (int, optional): number of iterations for the crossvalidation.
        :returns error (float): the average percent error of the dataset, tested on the network using crossvalidation.
        """
        x = ds.DS['input']
        y = ds.DS['target']
        n, m = x.shape
        errors = np.zeros(iterations)
        cv = cross_validation.KFold(n, iterations, shuffle=True)

        i = 0
        for train_index, test_index in cv:
            x_train = x[train_index, :]
            y_train = y[train_index, :]
            x_test = x[test_index, :]
            y_test = y[test_index, :]

            ds_train = RegressionData.conv2DS(x_train, y_train)
            ds_test = RegressionData.conv2DS(x_test, y_test)

            trainer = BackpropTrainer(
                self.network,
                dataset=ds_train,
                learningrate=self.learningrate,
                momentum=self.momentum,
                verbose=self.verbose,
                batchlearning=self.batchlearning)

            trainer.trainUntilConvergence(
                dataset=ds_train,
                maxEpochs=self.max_epochs,
                continueEpochs=self.con_epochs)

            tstresult = self.test(ds_test)
            errors[i] = tstresult[0]
            i += 1

        print "Simple Regression Neural Network cross-validation test errors: " % errors
        return np.average(errors)
# Karol Dzitkowski
# [email protected]
# 10-10-2014

from pylab import *
from csv_data import RegressionData
from regression import RegressionNeuralNetwork
from pybrain.utilities import percentError

d_train = RegressionData('data/regress_1_train.csv')
nn = RegressionNeuralNetwork(1, 1, 3, verbose=True, learningrate=0.025)

nn.apply_custom_network([3,3])

#RUN NETWORK FOR CLASSIFICATION
t = nn.run(d_train)
print 'train error', nn.test(d_train)

d_test = RegressionData('data/regress_1_tst.csv')
print 'test error', nn.test(d_test)

# get error
# result = t.testOnClassData(dataset=d_train.DS)
# error = percentError(result, d_train.DS['class'])
# print 'error =', error

# PLOT RESULTS
figure(1)
d_train.plotResult(nn)
figure(2)
d_test.plotResult(nn)