本文整理汇总了Python中modshogun.RealFeatures类的典型用法代码示例。如果您正苦于以下问题:Python RealFeatures类的具体用法?Python RealFeatures怎么用?Python RealFeatures使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RealFeatures类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: features_dense_modular
def features_dense_modular (A=matrixA,B=matrixB,C=matrixC):
a=RealFeatures(A)
b=LongIntFeatures(B)
c=ByteFeatures(C)
# or 16bit wide ...
#feat1 = f.ShortFeatures(N.zeros((10,5),N.short))
#feat2 = f.WordFeatures(N.zeros((10,5),N.uint16))
# print(some statistics about a)
# get first feature vector and set it
a.set_feature_vector(array([1,4,0,0,0,9], dtype=float64), 0)
# get matrices
a_out = a.get_feature_matrix()
b_out = b.get_feature_matrix()
c_out = c.get_feature_matrix()
assert(all(a_out==A))
assert(all(b_out==B))
assert(all(c_out==C))
return a_out,b_out,c_out,a,b,c
示例2: transfer_multitask_clustered_logistic_regression
def transfer_multitask_clustered_logistic_regression (fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MSG_DEBUG
try:
from modshogun import MultitaskClusteredLogisticRegression
except ImportError:
print("MultitaskClusteredLogisticRegression not available")
exit()
features = RealFeatures(hstack((traindat,sin(traindat),cos(traindat))))
labels = BinaryLabels(hstack((label_train,label_train,label_train)))
n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors//3)
task_two = Task(n_vectors//3,2*n_vectors//3)
task_three = Task(2*n_vectors//3,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)
task_group.append_task(task_three)
mtlr = MultitaskClusteredLogisticRegression(1.0,100.0,features,labels,task_group,2)
#mtlr.io.set_loglevel(MSG_DEBUG)
mtlr.set_tolerance(1e-3) # use 1e-2 tolerance
mtlr.set_max_iter(100)
mtlr.train()
mtlr.set_current_task(0)
#print mtlr.get_w()
out = mtlr.apply_regression().get_labels()
return out
示例3: classifier_featureblock_logistic_regression
def classifier_featureblock_logistic_regression (fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import BinaryLabels, RealFeatures, IndexBlock, IndexBlockGroup
try:
from modshogun import FeatureBlockLogisticRegression
except ImportError:
print("FeatureBlockLogisticRegression not available")
exit(0)
features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))
n_features = features.get_num_features()
block_one = IndexBlock(0,n_features//2)
block_two = IndexBlock(n_features//2,n_features)
block_group = IndexBlockGroup()
block_group.add_block(block_one)
block_group.add_block(block_two)
mtlr = FeatureBlockLogisticRegression(0.1,features,labels,block_group)
mtlr.set_regularization(1) # use regularization ratio
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlr.train()
out = mtlr.apply().get_labels()
return out
示例4: features_dense_io_modular
def features_dense_io_modular():
from modshogun import RealFeatures, CSVFile
feats=RealFeatures()
f=CSVFile("../data/fm_train_real.dat","r")
f.set_delimiter(" ")
feats.load(f)
return feats
示例5: transfer_multitask_leastsquares_regression
def transfer_multitask_leastsquares_regression (fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import RegressionLabels, RealFeatures, Task, TaskGroup
try:
from modshogun import MultitaskLeastSquaresRegression
except ImportError:
print("MultitaskLeastSquaresRegression not available")
exit(0)
features = RealFeatures(traindat)
labels = RegressionLabels(label_train)
n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors//2)
task_two = Task(n_vectors//2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)
mtlsr = MultitaskLeastSquaresRegression(0.1,features,labels,task_group)
mtlsr.set_regularization(1) # use regularization ratio
mtlsr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlsr.train()
mtlsr.set_current_task(0)
out = mtlsr.apply_regression().get_labels()
return out
示例6: transfer_multitask_l12_logistic_regression
def transfer_multitask_l12_logistic_regression (fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup
try:
from modshogun import MultitaskL12LogisticRegression
except ImportError:
print("MultitaskL12LogisticRegression not available")
exit(0)
features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))
n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors//2)
task_two = Task(n_vectors//2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)
mtlr = MultitaskL12LogisticRegression(0.1,0.1,features,labels,task_group)
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlr.set_max_iter(10)
mtlr.train()
mtlr.set_current_task(0)
out = mtlr.apply_regression().get_labels()
return out
示例7: features_dense_zero_copy_modular
def features_dense_zero_copy_modular (in_data=data):
feats = None
if numpy.__version__ >= '1.5':
feats=numpy.array(in_data, dtype=float64, order='F')
a=RealFeatures()
a.frombuffer(feats, False)
b=numpy.array(a, copy=False)
c=numpy.array(a, copy=True)
d=RealFeatures()
d.frombuffer(a, False)
e=RealFeatures()
e.frombuffer(a, True)
a[:,0]=0
#print a[0:4]
#print b[0:4]
#print c[0:4]
#print d[0:4]
#print e[0:4]
else:
print("numpy version >= 1.5 is needed")
return feats
示例8: modelselection_grid_search_kernel
def modelselection_grid_search_kernel (num_subsets, num_vectors, dim_vectors):
# init seed for reproducability
Math.init_random(1)
random.seed(1);
# create some (non-sense) data
matrix=random.rand(dim_vectors, num_vectors)
# create num_feautres 2-dimensional vectors
features=RealFeatures()
features.set_feature_matrix(matrix)
# create labels, two classes
labels=BinaryLabels(num_vectors)
for i in range(num_vectors):
labels.set_label(i, 1 if i%2==0 else -1)
# create svm
classifier=LibSVM()
# splitting strategy
splitting_strategy=StratifiedCrossValidationSplitting(labels, num_subsets)
# accuracy evaluation
evaluation_criterion=ContingencyTableEvaluation(ACCURACY)
# cross validation class for evaluation in model selection
cross=CrossValidation(classifier, features, labels, splitting_strategy, evaluation_criterion)
cross.set_num_runs(1)
# print all parameter available for modelselection
# Dont worry if yours is not included, simply write to the mailing list
#classifier.print_modsel_params()
# model parameter selection
param_tree=create_param_tree()
#param_tree.print_tree()
grid_search=GridSearchModelSelection(cross, param_tree)
print_state=False
best_combination=grid_search.select_model(print_state)
#print("best parameter(s):")
#best_combination.print_tree()
best_combination.apply_to_machine(classifier)
# larger number of runs to have tighter confidence intervals
cross.set_num_runs(10)
cross.set_conf_int_alpha(0.01)
result=cross.evaluate()
casted=CrossValidationResult.obtain_from_generic(result);
#print "result mean:", casted.mean
return classifier,result,casted.mean
示例9: features_dense_real_modular
def features_dense_real_modular (A=matrix):
# ... of type Real, LongInt and Byte
a=RealFeatures(A)
# print(some statistics about a)
#print(a.get_num_vectors())
#print(a.get_num_features())
# get first feature vector and set it
#print(a.get_feature_vector(0))
a.set_feature_vector(array([1,4,0,0,0,9], dtype=float64), 0)
# get matrix
a_out = a.get_feature_matrix()
assert(all(a_out==A))
return a_out
示例10: multiclass_c45classifiertree_modular
def multiclass_c45classifiertree_modular(train=traindat,test=testdat,labels=label_traindat,ft=feattypes):
try:
from modshogun import RealFeatures, MulticlassLabels, CSVFile, C45ClassifierTree
from numpy import random, int32
except ImportError:
print("Could not import Shogun and/or numpy modules")
return
# wrap features and labels into Shogun objects
feats_train=RealFeatures(CSVFile(train))
feats_test=RealFeatures(CSVFile(test))
train_labels=MulticlassLabels(CSVFile(labels))
# divide train dataset into training and validation subsets in the ratio 2/3 to 1/3
subset=int32(random.permutation(feats_train.get_num_vectors()))
vsubset=subset[1:subset.size/3]
trsubset=subset[1+subset.size/3:subset.size]
# C4.5 Tree formation using training subset
train_labels.add_subset(trsubset)
feats_train.add_subset(trsubset)
c=C45ClassifierTree()
c.set_labels(train_labels)
c.set_feature_types(ft)
c.train(feats_train)
train_labels.remove_subset()
feats_train.remove_subset()
# prune tree using validation subset
train_labels.add_subset(vsubset)
feats_train.add_subset(vsubset)
c.prune_tree(feats_train,train_labels)
train_labels.remove_subset()
feats_train.remove_subset()
# Classify test data
output=c.apply_multiclass(feats_test).get_labels()
output_certainty=c.get_certainty_vector()
return c,output,output_certainty
示例11: transfer_multitask_group_regression
def transfer_multitask_group_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import RegressionLabels, RealFeatures, Task, TaskGroup, MultitaskLSRegression
features = RealFeatures(traindat)
labels = RegressionLabels(label_train)
n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.add_task(task_one)
task_group.add_task(task_two)
mtlsr = MultitaskLSRegression(0.1,features,labels,task_group)
mtlsr.train()
mtlsr.set_current_task(0)
out = mtlsr.apply_regression().get_labels()
return out
示例12: neuralnets_simple_modular
def neuralnets_simple_modular (train_fname, test_fname,
label_fname, C, epsilon):
from modshogun import NeuralLayers, NeuralNetwork, RealFeatures, BinaryLabels
from modshogun import Math_init_random, CSVFile
Math_init_random(17)
feats_train=RealFeatures(CSVFile(train_fname))
feats_test=RealFeatures(CSVFile(test_fname))
labels=BinaryLabels(CSVFile(label_fname))
layers = NeuralLayers()
network = NeuralNetwork(layers.input(feats_train.get_num_features()).linear(50).softmax(2).done())
network.quick_connect()
network.initialize_neural_network()
network.set_labels(labels)
network.train(feats_train)
return network, network.apply_multiclass(feats_test)
示例13: load_data
def load_data(num_train_samples=7291, m_data_dict=data_dict):
from modshogun import RealFeatures, MulticlassLabels
import numpy
train_vec = m_data_dict['yTr'][0][:num_train_samples].astype(numpy.float64)
train_labels = MulticlassLabels(train_vec)
test_vec = m_data_dict['yTe'][0].astype(numpy.float64)
test_labels = MulticlassLabels(test_vec)
print "#train_labels = " + str(train_labels.get_num_labels())
print "#test_labels = " + str(test_labels.get_num_labels())
train_mat = m_data_dict['xTr'][:,:num_train_samples].astype(numpy.float64)
train_features = RealFeatures(train_mat)
test_mat = m_data_dict['xTe'].astype(numpy.float64)
test_features = RealFeatures(test_mat)
print "#train_vectors = " + str(train_features.get_num_vectors())
print "#test_vectors = " + str(test_features.get_num_vectors())
print "data dimension = " + str(test_features.get_num_features())
return train_features, train_labels, test_features, test_labels
示例14: preprocessor_prunevarsubmean_modular
def preprocessor_prunevarsubmean_modular (fm_train_real=traindat,fm_test_real=testdat,width=1.4,size_cache=10):
from modshogun import Chi2Kernel
from modshogun import RealFeatures
from modshogun import PruneVarSubMean
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
preproc=PruneVarSubMean()
preproc.init(feats_train)
feats_train.add_preprocessor(preproc)
feats_train.apply_preprocessor()
feats_test.add_preprocessor(preproc)
feats_test.apply_preprocessor()
kernel=Chi2Kernel(feats_train, feats_train, width, size_cache)
km_train=kernel.get_kernel_matrix()
kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
return km_train,km_test,kernel
示例15: transfer_multitask_logistic_regression
def transfer_multitask_logistic_regression (fm_train=traindat,fm_test=testdat,label_train=label_traindat):
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MultitaskLogisticRegression
features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))
n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)
mtlr = MultitaskLogisticRegression(0.1,features,labels,task_group)
mtlr.set_regularization(1) # use regularization ratio
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlr.train()
mtlr.set_current_task(0)
out = mtlr.apply().get_labels()
return out