How to load training data in PyBrain?

Here is how I did it:

ds = SupervisedDataSet(6,3)

tf = open('mycsvfile.csv','r')

for line in tf.readlines():
    data = [float(x) for x in line.strip().split(',') if x != '']
    indata =  tuple(data[:6])
    outdata = tuple(data[6:])
    ds.addSample(indata,outdata)

n = buildNetwork(ds.indim,8,8,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.01,momentum=0.5,verbose=True)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)

In this case the neural network has 6 inputs and 3 outputs. The csv file has 9 values on each line separated by a comma. The first 6 values are input values and the last three are outputs.


You just use a pandas DataFrame this way

import pandas as pd

dataset = SupervisedDataSet(6,3)

df = pd.read_csv('mycsvfile.csv')

dataset.setField('input', df.values[:,:6]) # this sets the features

y=[[x] for x in df.values[:,:6])] # Do this to avoid IndexError: tuple index out of range
                                  # as the target field should be a list of lists, 
                                  # even if its shape is 1

dataset.setField('target', y)     # this set the target[s] field[s]
del df,y

and you are good to go.