本文整理汇总了Python中pybrain.datasets.ClassificationDataSet.setField方法的典型用法代码示例。如果您正苦于以下问题:Python ClassificationDataSet.setField方法的具体用法?Python ClassificationDataSet.setField怎么用?Python ClassificationDataSet.setField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybrain.datasets.ClassificationDataSet
的用法示例。
在下文中一共展示了ClassificationDataSet.setField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: classify
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def classify(Xtrain, Ytrain, n_hidden=5):
""" Use entirety of provided X, Y to predict
Arguments
Xtrain -- Training data
Ytrain -- Training prediction
Returns
classifier -- a classifier fitted to Xtrain and Ytrain
"""
# PyBrain expects data in its DataSet format
trndata = ClassificationDataSet(Xtrain.shape[1], nb_classes=2)
trndata.setField('input', Xtrain)
# Apprently, arrays don't work here as they try to access second dimension size...
trndata.setField('target', mat(Ytrain).transpose())
trndata._convertToOneOfMany() # one output neuron per class
# build neural net and train it
net = buildNetwork(trndata.indim, n_hidden, trndata.outdim, outclass=SoftmaxLayer)
trainer = BackpropTrainer(net, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
trainer.trainUntilConvergence()
#trainer.trainEpochs(5)
print "trained"
#trainer.trainEpochs(5)
# Return a functor that wraps calling predict
return NeuralNetworkClassifier(trainer)
示例2: load_data
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def load_data(filename):
"""
load dataset for classification
"""
assert os.path.exists(filename)==True
dat = scipy.io.loadmat(filename)
inputs = dat['inputs']
#print len(inputs)
targets = dat['targets']
#print len(targets)
assert len(inputs)==len(targets)
global alldata
global indim
global outdim
indim = len(inputs[0])
outdim = 1
#print indim
alldata = ClassificationDataSet(indim, outdim, nb_classes = 8)
alldata.setField('input',inputs)
alldata.setField('target',targets)
assert len(alldata['input'])==len(alldata['target'])
print type(alldata)
示例3: ConvertToOneOfMany
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def ConvertToOneOfMany(d,nb_classes,bounds=(0,1)):
d2 = ClassificationDataSet(d.indim, d.outdim, nb_classes=nb_classes)
for n in range(d.getLength()):
d2.addSample( d.getSample(n)[0], d.getSample(n)[1] )
oldtarg=d.getField('target')
newtarg=np.zeros([len(d),nb_classes],dtype='Int32')+bounds[0]
for i in range(len(d)):
newtarg[i,int(oldtarg[i])]=bounds[1]
d2.setField('class',oldtarg)
d2.setField('target',newtarg)
return(d2)
示例4: convert_to_pybrain_dataset
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def convert_to_pybrain_dataset(X, Y=None):
if Y is None:
Y = [0]*X.shape[0]
# Apprently, arrays don't work here as they try to access second dimension size...
Y = mat(Y).transpose()
data = ClassificationDataSet(X.shape[1], Y.shape[1], nb_classes=2)
data.setField('input', X)
data.setField('target', Y)
data._convertToOneOfMany() # one output neuron per class
return data
示例5: loss_multiclass_nn_old
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def loss_multiclass_nn_old(X_feats, Y, nn):
DS = ClassificationDataSet( X_feats.shape[1], 1, nb_classes=2 )
#for i in range(X_feats.shape[0]):
# DS.addSample( X_feats[i,:], [0.0] )
DS.setField('input', X_feats)
DS.setField('target', np.zeros((X_feats.shape[0],1)))
DS._convertToOneOfMany()
prob = nn.activateOnDataset(DS)
Y2 = classifier.to_one_of_k_coding(Y, 0)
local_likelihood = -np.dot(np.log(prob).flat, Y2.flat)
likelihood = mpi.COMM.allreduce(local_likelihood)
num_data = mpi.COMM.allreduce(len(Y))
return float(likelihood) / num_data
示例6: cross_validation
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def cross_validation(trndata, folds=3, **kwargs):
"""
kwargs are parameters for the model
"""
input = np.vsplit(trndata['input'], folds)
target = np.vsplit(trndata['target'], folds)
zipped = zip(input, target)
accuracy_sum = 0
for i in len(zipped):
new_train = ClassificationDataSet(attributes, nb_classes=classes_number)
new_test = ClassificationDataSet(attributes, nb_classes=classes_number)
test_zipped = zipped[i]
train_zipped = zipped[:i] + zipped[(i+1):]
new_train.setField('input', np.vstack[train_zipped[0]])
new_train.setField('target', np.vstack[train_zipped[1]])
new_test.setField('input', test_zipped[0])
new_test.setField('target', train_zipped[1])
model = FNNClassifier()
model.train(new_train, new_test, kwargs)
out, targ = model.predict(new_test)
accuracy_sum += accuracy(out, targ)
return accuracy_sum / folds
示例7: big_training
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def big_training(np_data, num_nets=1, num_epoch=20, net_builder=net_full, train_size=.1, testing=False):
sss = cross_validation.StratifiedShuffleSplit(np_data[:,:1].ravel(), n_iter=num_nets , test_size=1-train_size, random_state=3476)
nets=[None for net_ind in range(num_nets)]
trainaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
testaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
net_ind=0
for train_index, test_index in sss:
print ('%s Building %d. network.' %(time.ctime(), net_ind+1))
#print("TRAIN:", len(train_index), "TEST:", len(test_index))
trainset = ClassificationDataSet(np_data.shape[1] - 1, 1)
trainset.setField('input', np_data[train_index,1:]/100-.6)
trainset.setField('target', np_data[train_index,:1])
trainset._convertToOneOfMany( )
trainlabels = trainset['class'].ravel().tolist()
if testing:
testset = ClassificationDataSet(np_data.shape[1] - 1, 1)
testset.setField('input', np_data[test_index,1:]/100-.6)
testset.setField('target', np_data[test_index,:1])
testset._convertToOneOfMany( )
testlabels = testset['class'].ravel().tolist()
nets[net_ind] = net_builder()
trainer = BackpropTrainer(nets[net_ind], trainset)
for i in range(num_epoch):
for ii in range(3):
err = trainer.train()
print ('%s Epoch %d: Network trained with error %f.' %(time.ctime(), i+1, err))
trainaccu[net_ind][i]=accuracy_score(trainlabels,trainer.testOnClassData())
print ('%s Epoch %d: Train accuracy is %f' %(time.ctime(), i+1, trainaccu[net_ind][i]))
print ([sum([trainaccu[y][i]>tres for y in range(net_ind+1)]) for tres in [0,.1,.2,.3,.4,.5,.6]])
if testing:
testaccu[net_ind][i]=accuracy_score(testlabels,trainer.testOnClassData(testset))
print ('%s Epoch %d: Test accuracy is %f' %(time.ctime(), i+1, testaccu[net_ind][i]))
NetworkWriter.writeToFile(nets[net_ind], 'nets/'+net_builder.__name__+str(net_ind)+'.xml')
net_ind +=1
return [nets, trainaccu, testaccu]
示例8: validate
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def validate(trainer, dataset, n_folds, max_epochs):
l = dataset.getLength()
inp = dataset.getField("input")
tar = dataset.getField("target")
indim = dataset.indim
outdim = dataset.outdim
assert l > n_folds
perms = np.array_split(np.arange(l), n_folds)
perf = 0.0
for i in range(n_folds):
# determine train indices
train_perms_idxs = range(n_folds)
train_perms_idxs.pop(i)
temp_list = []
for train_perms_idx in train_perms_idxs:
temp_list.append(perms[ train_perms_idx ])
train_idxs = np.concatenate(temp_list)
# determine test indices
test_idxs = perms[i]
# train
#print("training iteration", i)
train_ds = ClassificationDataSet(indim, outdim)
train_ds.setField("input" , inp[train_idxs])
train_ds.setField("target" , tar[train_idxs])
train_ds._convertToOneOfMany()
trainer = copy.deepcopy(trainer)
trainer.setData(train_ds)
if not max_epochs:
trainer.train()
else:
trainer.trainEpochs(max_epochs)
# test
#print("testing iteration", i)
test_ds = ClassificationDataSet(indim, outdim)
test_ds.setField("input" , inp[test_idxs])
test_ds.setField("target" , tar[test_idxs])
test_ds._convertToOneOfMany()
# perf += self.getPerformance( trainer.module, dataset )
# perf += self._calculatePerformance(trainer.module, dataset)
perf += percentError(trainer.testOnClassData(dataset=test_ds),
test_ds['class'])
perf /= n_folds
return perf
示例9: generateGridData
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def generateGridData(x,y, return_ticks=False):
""" Generates a dataset containing a regular grid of points. The x and y arguments
contain start, end, and step each. Returns the dataset and the x and y mesh or ticks."""
x = np.arange(x[0], x[1], x[2])
y = np.arange(y[0], y[1], y[2])
X, Y = np.meshgrid(x, y)
shape = X.shape
# need column vectors in dataset, not arrays
ds = ClassificationDataSet(2,1)
ds.setField('input', np.concatenate((X.reshape(X.size, 1),Y.reshape(X.size, 1)), 1))
ds.setField('target', np.zeros([X.size,1]))
ds._convertToOneOfMany()
if return_ticks:
return (ds, x, y)
else:
return (ds, X, Y)
示例10: train
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def train(self, X, y):
ds = ClassificationDataSet(self.numFeatures, self.numLabels, nb_classes=self.numLabels)
ds.setField("input", X)
ds.setField("target", y)
self.net = buildNetwork(self.numFeatures, len(y), self.numLabels, outclass=SigmoidLayer, bias=True)
print X
print y
trainer = BackpropTrainer(self.net, ds, learningrate=0.12)
print "Training now.... on " + str(len(y)) + " training examples"
startDate = datetime.datetime.now()
trainer.trainUntilConvergence(verbose=True, validationProportion=0.05)
datetime.timedelta(0, 8, 562000)
dateDiff = datetime.datetime.now() - startDate
timeDiff = divmod(dateDiff.days * 86400 + dateDiff.seconds, 60)
print "DONE TRAINING. TOOK %s min %s sec\r" % (timeDiff[0], timeDiff[1])
print "=======================================================================================\r"
示例11: get_predictions_nn_old
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def get_predictions_nn_old(self, X, special_bias=None):
X_feats = np.ascontiguousarray(np.hstack((X[self.feat_list[i]] for i in range(len(self.feat_list)))))
X_feats -= self.m
X_feats /= self.std
if special_bias != None:
X_feats = np.ascontiguousarray(np.hstack((X_feats, special_bias)))
DS = ClassificationDataSet( X_feats.shape[1], 1, nb_classes=2 )
#for i in range(X_feats.shape[0]):
# DS.addSample( X_feats[i,:], [0.0] )
DS.setField('input', X_feats)
DS.setField('target', np.zeros((X_feats.shape[0],1)))
DS._convertToOneOfMany()
prob = self._nn.activateOnDataset(DS)
prob = mpi.COMM.gather(prob)
if mpi.is_root():
return np.vstack(prob)
else:
return np.zeros((0))
示例12: Accuracy
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def Accuracy(self, X, Y, special_bias = None):
X_feats = np.ascontiguousarray(np.hstack((X[self.feat_list[i]] for i in range(len(self.feat_list)))))
X_feats -= self.m
X_feats /= self.std
if special_bias != None:
X_feats = np.ascontiguousarray(np.hstack((X_feats, special_bias)))
if self._type=='linsvm' or self._type=='logreg' or self._type=='logreg_atwv':
self.test_accu = classifier.Evaluator.accuracy(Y, np.dot(X_feats,self.w)+self.b)
elif self._type=='nn_atwv':
pred = get_predictions_nn(X_feats, self._weights_nn, arch=[10])[0]
pred[:,0] = 0.5
self.test_accu = classifier.Evaluator.accuracy(Y, pred)
else:
DS = ClassificationDataSet( X_feats.shape[1], 1, nb_classes=2 )
#for i in range(X_feats.shape[0]):
# DS.addSample( X_feats[i,:], [Y[i]] )
DS.setField('input', X_feats)
DS.setField('target', Y[:,np.newaxis])
DS._convertToOneOfMany()
predict,targts = self._nn_trainer.testOnClassData(DS, verbose=True,return_targets=True)
self.test_accu = np.sum(np.array(predict)==np.array(targts))/float(len(targts))
return self.test_accu
示例13: bootstrap
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
def bootstrap(trndata, iter=100):
"""
check http://sci2s.ugr.es/keel/pdf/specific/articulo/jain_boot_87.pdf for notation
"""
print trndata.calculateStatistics()
np_array = np.hstack((trndata['input'], trndata['target']))
my_range = range(np_array.shape[0])
print trndata['target'].shape
app_sum = 0
e0_sum = 0
for i in range(iter):
indices = list(set([random.choice(my_range) for i in my_range]))
np_train_array = np.vstack(np_array[indices])
new_training_samples = ClassificationDataSet(attributes, classes_number)
new_training_samples.setField('input', np_train_array[:, :54])
new_training_samples.setField('target', np_train_array[:, 54:55])
new_training_samples._convertToOneOfMany()
test_indices = list(set(my_range) - set(indices))
new_test_samples = ClassificationDataSet(attributes, classes_number)
np_test_array = np.vstack(np_array[test_indices])
new_test_samples.setField('input', np_test_array[:, :54])
new_test_samples.setField('target', np_test_array[:, 54:55])
new_test_samples._convertToOneOfMany()
print new_training_samples.calculateStatistics()
print new_test_samples.calculateStatistics()
model = FNNClassifier()
model.train(new_training_samples, new_test_samples)
(xtrn, ytrn) = model.predict(new_training_samples)
(xtest, ytest) = model.predict(new_test_samples)
app_sum += (1 - accuracy(xtrn, ytrn))
e0_sum += (1 - accuracy(xtest, ytest))
app = app_sum / float(iter)
e0 = e0_sum / float(iter)
e632 = 0.368 * app + 0.632 * e0
print e632
return e632
示例14: SupervisedDataSet
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
scale = preprocessing.Normalizer().fit(XtrainPos)
XtrainPos = scale.fit_transform(XtrainPos)
XtestPos = scale.fit_transform(XtestPos)
# Neural Network
YtrainPos = YtrainPos.reshape( -1, 1 )
YtestPos = YtestPos.reshape( -1, 1 )
input_size = XtrainPos.shape[1]
target_size = YtrainPos.shape[1]
hidden_size = 50 # arbitrarily chosen
#ds = SupervisedDataSet(input_size,target_size )
ds = ClassificationDataSet(21)
ds.setField( 'input', XtrainPos )
ds.setField( 'target', YtrainPos )
ds._convertToOneOfMany(bounds=[0, 1])
net = buildNetwork( input_size, hidden_size, 5, bias = True )
trainer = BackpropTrainer( net, ds )
epochs = 2
print "training for {} epochs...".format( epochs )
for i in range( epochs ):
mse = trainer.train()
rmse = sqrt(mse)
print "training RMSE, epoch {}: {}".format( i + 1, rmse )
#trainer.trainUntilConvergence( verbose = True, validationProportion = 0.15, maxEpochs = 1000, continueEpochs = 10 )
示例15: ClassificationDataSet
# 需要导入模块: from pybrain.datasets import ClassificationDataSet [as 别名]
# 或者: from pybrain.datasets.ClassificationDataSet import setField [as 别名]
#alldata = ClassificationDataSet(2, 1, nb_classes=3)
#for n in xrange(400):
# for klass in range(3):
# input = multivariate_normal(means[klass],cov[klass])
# alldata.addSample(input, [klass])
#
#tstdata,trndata = alldata.splitWithProportion( 0.25 )
(X_train, Y_train, X_test, Y_test) = prepare_sets(TM_dict['TM_sparse'], split=1.0)
class_dict = {'cotidiano':0, 'esporte':1, 'mundo':2,
'poder':3, 'ilustrada':4, 'mercado':5}
ord_labels = ['cotidiano', 'esporte', 'mundo', 'poder', 'ilustrada', 'mercado']
DS = ClassificationDataSet(inp=X_train.shape[1], nb_classes=len(class_dict), class_labels=ord_labels)
assert(X_train.shape[0] == Y_train.shape[0])
DS.setField('input', X_train)
DS.setField('target', Y_train)
tstdata,trndata = DS.splitWithProportion( 0.25 )
trndata._convertToOneOfMany()
tstdata._convertToOneOfMany()
print "Number of training patterns: ", len(trndata)
print "Input and output dimensions: ", trndata.indim, trndata.outdim
print "First sample (input, target, class):"
print trndata['input'][0], trndata['target'][0], trndata['class'][0]
nneuronios = 10
fnn = buildNetwork( trndata.indim, nneuronios, trndata.outdim, outclass=SoftmaxLayer )
trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)