本文整理汇总了Python中shogun.Features.SparseRealFeatures类的典型用法代码示例。如果您正苦于以下问题:Python SparseRealFeatures类的具体用法?Python SparseRealFeatures怎么用?Python SparseRealFeatures使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SparseRealFeatures类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_output_plot_isolines
def compute_output_plot_isolines(classifier, kernel=None, train=None, sparse=False, pos=None, neg=None, regression=False):
size=100
if pos is not None and neg is not None:
x1_max=max(1.2*pos[0,:])
x1_min=min(1.2*neg[0,:])
x2_min=min(1.2*neg[1,:])
x2_max=max(1.2*pos[1,:])
x1=linspace(x1_min, x1_max, size)
x2=linspace(x2_min, x2_max, size)
else:
x1=linspace(-5, 5, size)
x2=linspace(-5, 5, size)
x, y=meshgrid(x1, x2)
dense=RealFeatures(array((ravel(x), ravel(y))))
if sparse:
test=SparseRealFeatures()
test.obtain_from_simple(dense)
else:
test=dense
if kernel and train:
kernel.init(train, test)
else:
classifier.set_features(test)
labels = None
if regression:
labels=classifier.apply().get_labels()
else:
labels=classifier.apply().get_confidences()
z=labels.reshape((size, size))
return x, y, z
示例2: features_read_svmlight_format_modular
def features_read_svmlight_format_modular(fname):
import os
from shogun.Features import SparseRealFeatures
f=SparseRealFeatures()
lab=f.load_svmlight_file(fname)
f.write_svmlight_file('testwrite.light', lab)
os.unlink('testwrite.light')
示例3: convSparseToShog
def convSparseToShog(data,delFeature=False):
resFeat = SparseRealFeatures()
resFeat.create_sparse_feature_matrix(len(data))
for iRec in xrange(len(data)):
feat = data[iRec]["feature"]
resFeat.set_sparse_feature_vector(iRec,feat["ind"].astype('i4')-1,feat["val"].astype('f8'))
if delFeature:
data[iRec]["feature"] = None
return resFeat
示例4: features_sparse_modular
def features_sparse_modular(A):
from scipy.sparse import csc_matrix
from shogun.Features import SparseRealFeatures
from numpy import array, float64, all
# sparse representation X of dense matrix A
# note, will work with types other than float64 too,
# but requires recent scipy.sparse
X=csc_matrix(A)
#print A
# create sparse shogun features from dense matrix A
a=SparseRealFeatures(A)
a_out=a.get_full_feature_matrix()
#print a_out
assert(all(a_out==A))
#print a_out
# create sparse shogun features from sparse matrix X
a.set_sparse_feature_matrix(X)
a_out=a.get_full_feature_matrix()
#print a_out
assert(all(a_out==A))
# create sparse shogun features from sparse matrix X
a=SparseRealFeatures(X)
a_out=a.get_full_feature_matrix()
#print a_out
assert(all(a_out==A))
# obtain (data,row,indptr) csc arrays of sparse shogun features
z=csc_matrix(a.get_sparse_feature_matrix())
z_out=z.todense()
#print z_out
assert(all(z_out==A))
示例5: getSparseRealFeatures
def getSparseRealFeatures(self,sequences,method="frequences"):
maxSeqLen = max( ( len(seq) for seq in sequences ) )
kmer_ind = numpy.zeros(maxSeqLen,dtype='i8')
if method == 'frequences':
kmer_val = numpy.zeros(maxSeqLen,dtype='f4')
else:
kmer_val = numpy.zeros(maxSeqLen,dtype='i4')
kmerMethod = getattr(self,method)
resFeat = SparseRealFeatures()
resFeat.create_sparse_feature_matrix(len(sequences))
for iSeq in xrange(len(sequences)):
seq = sequences[iSeq]
if isinstance(seq,str):
seq = numpy.fromstring(seq,'S1')
self.process(seq)
(size,total) = kmerMethod(kmer_val,kmer_ind)
#print size, total, kmer_val[:10],kmer_ind[:10]
resFeat.set_sparse_feature_vector(iSeq,kmer_ind[:size].astype('i4')-1,kmer_val[:size].astype('f8'))
#pdb.set_trace()
return resFeat
示例6: classifier_svmlin_modular
def classifier_svmlin_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,C=0.9,epsilon=1e-5,num_threads=1):
from shogun.Features import RealFeatures, SparseRealFeatures, BinaryLabels
from shogun.Classifier import SVMLin
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
labels=BinaryLabels(label_train_twoclass)
svm=SVMLin(C, feats_train, labels)
svm.set_epsilon(epsilon)
svm.parallel.set_num_threads(num_threads)
svm.set_bias_enabled(True)
svm.train()
svm.set_features(feats_test)
svm.get_bias()
svm.get_w()
svm.apply().get_labels()
predictions = svm.apply()
return predictions, svm, predictions.get_labels()
示例7: classify
def classify (true_labels):
num_feats=2
num_vec=true_labels.get_num_labels()
data_train=numpy.concatenate(
(numpy.random.randn(num_feats, num_vec/2)-1,
numpy.random.randn(num_feats, num_vec/2)+1),
axis=1)
realfeat=RealFeatures(data_train)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
C=3.
svm=SVMOcas(C, feats_train, true_labels)
svm.train()
data_test=numpy.concatenate(
(numpy.random.randn(num_feats, num_vec/2)-1,
numpy.random.randn(num_feats, num_vec/2)+1),
axis=1)
realfeat=RealFeatures(data_test)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
svm.set_features(feats_test)
return numpy.array(svm.classify().get_labels())
示例8: subgradient_svm
def subgradient_svm ():
print 'SubGradientSVM'
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SubGradientSVM
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
C=0.9
epsilon=1e-3
num_threads=1
max_train_time=1.
labels=Labels(label_train_twoclass)
svm=SubGradientSVM(C, feats_train, labels)
svm.set_epsilon(epsilon)
svm.parallel.set_num_threads(num_threads)
svm.set_bias_enabled(False)
svm.set_max_train_time(max_train_time)
svm.train()
svm.set_features(feats_test)
svm.classify().get_labels()
示例9: svmlin
def svmlin ():
print 'SVMLin'
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SVMLin
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
C=0.9
epsilon=1e-5
num_threads=1
labels=Labels(label_train_twoclass)
svm=SVMLin(C, feats_train, labels)
svm.set_epsilon(epsilon)
svm.parallel.set_num_threads(num_threads)
svm.set_bias_enabled(True)
svm.train()
svm.set_features(feats_test)
svm.get_bias()
svm.get_w()
svm.classify().get_labels()
示例10: svmsgd
def svmsgd ():
print 'SVMSGD'
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SVMSGD
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
C=0.9
num_threads=1
num_iter=5
labels=Labels(label_train_twoclass)
svm=SVMSGD(C, feats_train, labels)
svm.set_epochs(num_iter)
#svm.io.set_loglevel(0)
svm.train()
svm.set_features(feats_test)
labelPrediction = svm.classify().get_labels()
print labelPrediction>0
示例11: sparse_euclidian_distance
def sparse_euclidian_distance ():
print 'SparseEuclidianDistance'
from shogun.Features import RealFeatures, SparseRealFeatures
from shogun.Distance import SparseEuclidianDistance
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
distance=SparseEuclidianDistance(feats_train, feats_train)
dm_train=distance.get_distance_matrix()
distance.init(feats_train, feats_test)
dm_test=distance.get_distance_matrix()
示例12: distance_sparseeuclidean_modular
def distance_sparseeuclidean_modular (fm_train_real=traindat,fm_test_real=testdat):
from shogun.Features import RealFeatures, SparseRealFeatures
from shogun.Distance import SparseEuclidianDistance
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
distance=SparseEuclidianDistance(feats_train, feats_train)
dm_train=distance.get_distance_matrix()
distance.init(feats_train, feats_test)
dm_test=distance.get_distance_matrix()
return distance,dm_train,dm_test
示例13: classifier_svmsgd_modular
def classifier_svmsgd_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,C=0.9,num_threads=1,num_iter=5):
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SVMSGD
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
labels=Labels(label_train_twoclass)
svm=SVMSGD(C, feats_train, labels)
svm.set_epochs(num_iter)
#svm.io.set_loglevel(0)
svm.train()
svm.set_features(feats_test)
svm.apply().get_labels()
predictions = svm.apply()
return predictions, svm, predictions.get_labels()
示例14: classifier_subgradientsvm_modular
def classifier_subgradientsvm_modular(fm_train_real, fm_test_real, label_train_twoclass, C, epsilon, max_train_time):
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SubGradientSVM
realfeat = RealFeatures(fm_train_real)
feats_train = SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat = RealFeatures(fm_test_real)
feats_test = SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
labels = Labels(label_train_twoclass)
svm = SubGradientSVM(C, feats_train, labels)
svm.set_epsilon(epsilon)
svm.set_max_train_time(max_train_time)
svm.train()
svm.set_features(feats_test)
labels = svm.apply().get_labels()
return labels, svm
示例15: svmsgd
def svmsgd ():
print 'SVMSGD'
from shogun.Features import RealFeatures, SparseRealFeatures, Labels
from shogun.Classifier import SVMSGD
realfeat=RealFeatures(fm_train_real)
feats_train=SparseRealFeatures()
feats_train.obtain_from_simple(realfeat)
realfeat=RealFeatures(fm_test_real)
feats_test=SparseRealFeatures()
feats_test.obtain_from_simple(realfeat)
C=0.9
epsilon=1e-5
num_threads=1
labels=Labels(label_train_twoclass)
svm=SVMSGD(C, feats_train, labels)
#svm.io.set_loglevel(0)
svm.train()
svm.set_features(feats_test)
svm.classify().get_labels()