本文整理汇总了Python中shogun.Kernel.GaussianKernel类的典型用法代码示例。如果您正苦于以下问题:Python GaussianKernel类的具体用法?Python GaussianKernel怎么用?Python GaussianKernel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GaussianKernel类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: classify
def classify(classifier, features, labels, C=5, kernel_name=None, kernel_args=None):
from shogun.Features import RealFeatures
sigma = 10000
kernel = GaussianKernel(features, features, sigma)
# TODO
# kernel = LinearKernel(features, features)
# kernel = PolyKernel(features, features, 50, 2)
# kernel = kernels[kernel_name](features, features, *kernel_args)
svm = classifier(C, kernel, labels)
svm.train(features)
x_size = 640
y_size = 400
size = 100
x1 = np.linspace(0, x_size, size)
y1 = np.linspace(0, y_size, size)
x, y = np.meshgrid(x1, y1)
test = RealFeatures(np.array((np.ravel(x), np.ravel(y))))
kernel.init(features, test)
out = svm.apply(test).get_values()
if not len(out):
out = svm.apply(test).get_labels()
z = out.reshape((size, size))
z = np.transpose(z)
return x, y, z
开发者ID:dvalcarce,项目名称:shogun-gsoc,代码行数:28,代码来源:svm.py
示例2: mlprocess
def mlprocess(task_filename, data_filename, pred_filename, verbose=True):
"""Demo of creating machine learning process."""
task_type, fidx, lidx, train_idx, test_idx = parse_task(task_filename)
outputs = init_output(task_type)
all_data = parse_data(data_filename)
train_ex, train_lab, test_ex, test_lab = split_data(all_data, fidx, lidx, train_idx, test_idx)
label_train = outputs.str2label(train_lab)
if verbose:
print 'Number of features: %d' % train_ex.shape[0]
print '%d training examples, %d test examples' % (len(train_lab), len(test_lab))
feats_train = RealFeatures(train_ex)
feats_test = RealFeatures(test_ex)
width=1.0
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train)
svm = init_svm(task_type, kernel, labels)
svm.train()
kernel.init(feats_train, feats_test)
preds = svm.classify().get_labels()
pred_label = outputs.label2str(preds)
pf = open(pred_filename, 'w')
for pred in pred_label:
pf.write(pred+'\n')
pf.close()
开发者ID:jbeltram,项目名称:mldata-utils,代码行数:28,代码来源:mlprocess.py
示例3: statistics_kmm
def statistics_kmm (n,d):
from shogun.Features import RealFeatures
from shogun.Features import DataGenerator
from shogun.Kernel import GaussianKernel, MSG_DEBUG
from shogun.Statistics import KernelMeanMatching
from shogun.Mathematics import Math
# init seed for reproducability
Math.init_random(1)
random.seed(1);
data = random.randn(d,n)
# create shogun feature representation
features=RealFeatures(data)
# use a kernel width of sigma=2, which is 8 in SHOGUN's parametrization
# which is k(x,y)=exp(-||x-y||^2 / tau), in constrast to the standard
# k(x,y)=exp(-||x-y||^2 / (2*sigma^2)), so tau=2*sigma^2
kernel=GaussianKernel(10,8)
kernel.init(features,features)
kmm = KernelMeanMatching(kernel,array([0,1,2,3,7,8,9],dtype=int32),array([4,5,6],dtype=int32))
w = kmm.compute_weights()
#print w
return w
开发者ID:Argram,项目名称:shogun,代码行数:26,代码来源:statistics_kmm.py
示例4: regression_svrlight_modular
def regression_svrlight_modular(fm_train=traindat,fm_test=testdat,label_train=label_traindat, \
width=1.2,C=1,epsilon=1e-5,tube_epsilon=1e-2,num_threads=3):
from shogun.Features import Labels, RealFeatures
from shogun.Kernel import GaussianKernel
try:
from shogun.Regression import SVRLight
except ImportError:
print('No support for SVRLight available.')
return
feats_train=RealFeatures(fm_train)
feats_test=RealFeatures(fm_test)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train)
svr=SVRLight(C, epsilon, kernel, labels)
svr.set_tube_epsilon(tube_epsilon)
svr.parallel.set_num_threads(num_threads)
svr.train()
kernel.init(feats_train, feats_test)
out = svr.apply().get_labels()
return out, kernel
开发者ID:harshitsyal,项目名称:shogun,代码行数:28,代码来源:regression_svrlight_modular.py
示例5: kernel_io_modular
def kernel_io_modular (fm_train_real=traindat,fm_test_real=testdat,width=1.9):
from shogun.Features import RealFeatures
from shogun.Kernel import GaussianKernel
from shogun.Library import AsciiFile, BinaryFile
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
km_train=kernel.get_kernel_matrix()
f=AsciiFile("gaussian_train.ascii","w")
kernel.save(f)
del f
kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
f=AsciiFile("gaussian_test.ascii","w")
kernel.save(f)
del f
#clean up
import os
os.unlink("gaussian_test.ascii")
os.unlink("gaussian_train.ascii")
return km_train, km_test, kernel
开发者ID:AsherBond,项目名称:shogun,代码行数:27,代码来源:kernel_io_modular.py
示例6: createKernel
def createKernel(self, feats_train):
"""Call the corresponding constructor for the kernel"""
if self.kparam['name'] == 'gauss':
kernel = GaussianKernel(feats_train, feats_train, self.kparam['width'])
elif self.kparam['name'] == 'linear':
kernel = LinearKernel(feats_train, feats_train, self.kparam['scale'])
elif self.kparam['name'] == 'poly':
kernel = PolyKernel(feats_train, feats_train, self.kparam['degree'],
self.kparam['inhomogene'], self.kparam['normal'])
elif self.kparam['name'] == 'wd':
kernel = WeightedDegreePositionStringKernel(feats_train, feats_train, self.kparam['degree'])
kernel.set_shifts(self.kparam['shift'] * numpy.ones(self.kparam['seqlength'], dtype=numpy.int32))
elif self.kparam['name'] == 'spec':
kernel = CommWordStringKernel(feats_train, feats_train)
elif self.kparam['name'] == 'localalign':
kernel = LocalAlignmentStringKernel(feats_train, feats_train)
elif self.kparam['name'] == 'localimprove':
kernel = LocalityImprovedStringKernel(feats_train, feats_train, self.kparam['length'], \
self.kparam['indeg'], self.kparam['outdeg'])
else:
print 'Unknown kernel %s' % self.kparam['name']
raise ValueError
self.kernel = kernel
return kernel
开发者ID:boya888,项目名称:oqtans_tools,代码行数:25,代码来源:EasySVM.py
示例7: kernel_gaussian_modular
def kernel_gaussian_modular (fm_train_real=traindat,fm_test_real=testdat, width=1.3):
from shogun.Features import RealFeatures
from shogun.Kernel import GaussianKernel
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
km_train=kernel.get_kernel_matrix()
kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
return km_train,km_test,kernel
开发者ID:coodoing,项目名称:shogun,代码行数:13,代码来源:kernel_gaussian_modular.py
示例8: create_param_tree
def create_param_tree():
root=ModelSelectionParameters()
c1=ModelSelectionParameters("C1")
root.append_child(c1)
c1.build_values(-1.0, 1.0, R_EXP)
c2=ModelSelectionParameters("C2")
root.append_child(c2)
c2.build_values(-1.0, 1.0, R_EXP)
gaussian_kernel=GaussianKernel()
# print all parameter available for modelselection
# Dont worry if yours is not included, simply write to the mailing list
gaussian_kernel.print_modsel_params()
param_gaussian_kernel=ModelSelectionParameters("kernel", gaussian_kernel)
gaussian_kernel_width=ModelSelectionParameters("width")
gaussian_kernel_width.build_values(-1.0, 1.0, R_EXP, 1.0, 2.0)
param_gaussian_kernel.append_child(gaussian_kernel_width)
root.append_child(param_gaussian_kernel)
power_kernel=PowerKernel()
# print all parameter available for modelselection
# Dont worry if yours is not included, simply write to the mailing list
power_kernel.print_modsel_params()
param_power_kernel=ModelSelectionParameters("kernel", power_kernel)
root.append_child(param_power_kernel)
param_power_kernel_degree=ModelSelectionParameters("degree")
param_power_kernel_degree.build_values(1.0, 2.0, R_LINEAR)
param_power_kernel.append_child(param_power_kernel_degree)
metric=MinkowskiMetric(10)
# print all parameter available for modelselection
# Dont worry if yours is not included, simply write to the mailing list
metric.print_modsel_params()
param_power_kernel_metric1=ModelSelectionParameters("distance", metric)
param_power_kernel.append_child(param_power_kernel_metric1)
param_power_kernel_metric1_k=ModelSelectionParameters("k")
param_power_kernel_metric1_k.build_values(1.0, 2.0, R_LINEAR)
param_power_kernel_metric1.append_child(param_power_kernel_metric1_k)
return root
开发者ID:ratschlab,项目名称:ASP,代码行数:51,代码来源:modelselection_grid_search_kernel.py
示例9: gaussian
def gaussian ():
print 'Gaussian'
from shogun.Features import RealFeatures
from shogun.Kernel import GaussianKernel
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
width=1.9
kernel=GaussianKernel(feats_train, feats_train, width)
km_train=kernel.get_kernel_matrix()
kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
开发者ID:memimo,项目名称:shogun-liblinear,代码行数:14,代码来源:kernel_gaussian_modular.py
示例10: classifier_libsvm_minimal_modular
def classifier_libsvm_minimal_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,width=2.1,C=1):
from shogun.Features import RealFeatures, BinaryLabels
from shogun.Classifier import LibSVM
from shogun.Kernel import GaussianKernel
feats_train=RealFeatures(fm_train_real);
feats_test=RealFeatures(fm_test_real);
kernel=GaussianKernel(feats_train, feats_train, width);
labels=BinaryLabels(label_train_twoclass);
svm=LibSVM(C, kernel, labels);
svm.train();
kernel.init(feats_train, feats_test);
out=svm.apply().get_labels();
testerr=mean(sign(out)!=label_train_twoclass)
开发者ID:Argram,项目名称:shogun,代码行数:16,代码来源:classifier_libsvm_minimal_modular.py
示例11: create_param_tree
def create_param_tree():
from shogun.ModelSelection import ModelSelectionParameters, R_EXP, R_LINEAR
from shogun.ModelSelection import ParameterCombination
from shogun.Kernel import GaussianKernel, PolyKernel
root=ModelSelectionParameters()
tau=ModelSelectionParameters("tau")
root.append_child(tau)
# also R_LINEAR/R_LOG is available as type
min=-1
max=1
type=R_EXP
step=1.5
base=2
tau.build_values(min, max, type, step, base)
# gaussian kernel with width
gaussian_kernel=GaussianKernel()
# print all parameter available for modelselection
# Dont worry if yours is not included but, write to the mailing list
gaussian_kernel.print_modsel_params()
param_gaussian_kernel=ModelSelectionParameters("kernel", gaussian_kernel)
gaussian_kernel_width=ModelSelectionParameters("width");
gaussian_kernel_width.build_values(5.0, 8.0, R_EXP, 1.0, 2.0)
param_gaussian_kernel.append_child(gaussian_kernel_width)
root.append_child(param_gaussian_kernel)
# polynomial kernel with degree
poly_kernel=PolyKernel()
# print all parameter available for modelselection
# Dont worry if yours is not included but, write to the mailing list
poly_kernel.print_modsel_params()
param_poly_kernel=ModelSelectionParameters("kernel", poly_kernel)
root.append_child(param_poly_kernel)
# note that integers are used here
param_poly_kernel_degree=ModelSelectionParameters("degree")
param_poly_kernel_degree.build_values(1, 2, R_LINEAR)
param_poly_kernel.append_child(param_poly_kernel_degree)
return root
开发者ID:flxb,项目名称:shogun,代码行数:47,代码来源:modelselection_grid_search_krr.py
示例12: regression_kernel_ridge_modular
def regression_kernel_ridge_modular (fm_train=traindat,fm_test=testdat,label_train=label_traindat,width=0.8,tau=1e-6):
from shogun.Features import Labels, RealFeatures
from shogun.Kernel import GaussianKernel
from shogun.Regression import KernelRidgeRegression
feats_train=RealFeatures(fm_train)
feats_test=RealFeatures(fm_test)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train)
krr=KernelRidgeRegression(tau, kernel, labels)
krr.train(feats_train)
kernel.init(feats_train, feats_test)
out = krr.apply().get_labels()
return out,kernel,krr
开发者ID:harshitsyal,项目名称:shogun,代码行数:19,代码来源:regression_kernel_ridge_modular.py
示例13: classifier_multiclassmachine_modular
def classifier_multiclassmachine_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import LibSVM, KernelMulticlassMachine, ONE_VS_REST_STRATEGY
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train_multiclass)
classifier = LibSVM(C, kernel, labels)
classifier.set_epsilon(epsilon)
mc_classifier = KernelMulticlassMachine(ONE_VS_REST_STRATEGY,kernel,classifier,labels)
mc_classifier.train()
kernel.init(feats_train, feats_test)
out = mc_classifier.apply().get_labels()
return out
开发者ID:ashish-sadh,项目名称:shogun,代码行数:19,代码来源:classifier_multiclassmachine_modular.py
示例14: classifier_gmnpsvm_modular
def classifier_gmnpsvm_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, MulticlassLabels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import GMNPSVM
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=MulticlassLabels(label_train_multiclass)
svm=GMNPSVM(C, kernel, labels)
svm.set_epsilon(epsilon)
svm.train(feats_train)
kernel.init(feats_train, feats_test)
out=svm.apply(feats_test).get_labels()
return out,kernel
开发者ID:behollis,项目名称:muViewBranch,代码行数:19,代码来源:classifier_gmnpsvm_modular.py
示例15: classifier_multiclasslibsvm_modular
def classifier_multiclasslibsvm_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import MulticlassLibSVM
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=Labels(label_train_multiclass)
svm=MulticlassLibSVM(C, kernel, labels)
svm.set_epsilon(epsilon)
svm.train()
kernel.init(feats_train, feats_test)
out = svm.apply().get_labels()
predictions = svm.apply()
return predictions, svm, predictions.get_labels()
开发者ID:serialhex,项目名称:shogun,代码行数:19,代码来源:classifier_multiclasslibsvm_modular.py
示例16: mkl_binclass_modular
def mkl_binclass_modular (train_data, testdata, train_labels, test_labels, d1, d2):
# create some Gaussian train/test matrix
tfeats = RealFeatures(train_data)
tkernel = GaussianKernel(128, d1)
tkernel.init(tfeats, tfeats)
K_train = tkernel.get_kernel_matrix()
pfeats = RealFeatures(test_data)
tkernel.init(tfeats, pfeats)
K_test = tkernel.get_kernel_matrix()
# create combined train features
feats_train = CombinedFeatures()
feats_train.append_feature_obj(RealFeatures(train_data))
# and corresponding combined kernel
kernel = CombinedKernel()
kernel.append_kernel(CustomKernel(K_train))
kernel.append_kernel(GaussianKernel(128, d2))
kernel.init(feats_train, feats_train)
# train mkl
labels = Labels(train_labels)
mkl = MKLClassification()
# not to use svmlight
mkl.set_interleaved_optimization_enabled(0)
# which norm to use for MKL
mkl.set_mkl_norm(2)
# set cost (neg, pos)
mkl.set_C(1, 1)
# set kernel and labels
mkl.set_kernel(kernel)
mkl.set_labels(labels)
# train
mkl.train()
# test
# create combined test features
feats_pred = CombinedFeatures()
feats_pred.append_feature_obj(RealFeatures(test_data))
# and corresponding combined kernel
kernel = CombinedKernel()
kernel.append_kernel(CustomKernel(K_test))
kernel.append_kernel(GaussianKernel(128, d2))
kernel.init(feats_train, feats_pred)
# and classify
mkl.set_kernel(kernel)
output = mkl.apply().get_labels()
output = [1.0 if i>0 else -1.0 for i in output]
accu = len(where(output == test_labels)[0]) / float(len(output))
return accu
开发者ID:leiding326,项目名称:data-science,代码行数:58,代码来源:mkl_binclass_modular.py
示例17: classifier_libsvmoneclass_modular
def classifier_libsvmoneclass_modular (fm_train_real=traindat,fm_test_real=testdat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, Labels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import LibSVMOneClass
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
svm=LibSVMOneClass(C, kernel)
svm.set_epsilon(epsilon)
svm.train()
kernel.init(feats_train, feats_test)
svm.apply().get_labels()
predictions = svm.apply()
return predictions, svm, predictions.get_labels()
开发者ID:Anshul-Bansal,项目名称:gsoc,代码行数:19,代码来源:classifier_libsvmoneclass_modular.py
示例18: regression_kernel_ridge_modular
def regression_kernel_ridge_modular (n=100,n_test=100, \
x_range=6,x_range_test=10,noise_var=0.5,width=1, tau=1e-6, seed=1):
from shogun.Features import RegressionLabels, RealFeatures
from shogun.Kernel import GaussianKernel
from shogun.Regression import KernelRidgeRegression
# reproducable results
random.seed(seed)
# easy regression data: one dimensional noisy sine wave
n=15
n_test=100
x_range_test=10
noise_var=0.5;
X=random.rand(1,n)*x_range
X_test=array([[float(i)/n_test*x_range_test for i in range(n_test)]])
Y_test=sin(X_test)
Y=sin(X)+random.randn(n)*noise_var
# shogun representation
labels=RegressionLabels(Y[0])
feats_train=RealFeatures(X)
feats_test=RealFeatures(X_test)
kernel=GaussianKernel(feats_train, feats_train, width)
krr=KernelRidgeRegression(tau, kernel, labels)
krr.train(feats_train)
kernel.init(feats_train, feats_test)
out = krr.apply().get_labels()
# plot results
#plot(X[0],Y[0],'x') # training observations
#plot(X_test[0],Y_test[0],'-') # ground truth of test
#plot(X_test[0],out, '-') # mean predictions of test
#legend(["training", "ground truth", "mean predictions"])
#show()
return out,kernel,krr
开发者ID:Argram,项目名称:shogun,代码行数:42,代码来源:regression_kernel_ridge_modular.py
示例19: regression_libsvr_modular
def regression_libsvr_modular (svm_c=1, svr_param=0.1, n=100,n_test=100, \
x_range=6,x_range_test=10,noise_var=0.5,width=1, seed=1):
from shogun.Features import RegressionLabels, RealFeatures
from shogun.Kernel import GaussianKernel
from shogun.Regression import LibSVR, LIBSVR_NU_SVR, LIBSVR_EPSILON_SVR
# reproducable results
random.seed(seed)
# easy regression data: one dimensional noisy sine wave
n=15
n_test=100
x_range_test=10
noise_var=0.5;
X=random.rand(1,n)*x_range
X_test=array([[float(i)/n_test*x_range_test for i in range(n_test)]])
Y_test=sin(X_test)
Y=sin(X)+random.randn(n)*noise_var
# shogun representation
labels=RegressionLabels(Y[0])
feats_train=RealFeatures(X)
feats_test=RealFeatures(X_test)
kernel=GaussianKernel(feats_train, feats_train, width)
# two svr models: epsilon and nu
svr_epsilon=LibSVR(svm_c, svr_param, kernel, labels, LIBSVR_EPSILON_SVR)
svr_epsilon.train()
svr_nu=LibSVR(svm_c, svr_param, kernel, labels, LIBSVR_NU_SVR)
svr_nu.train()
# predictions
kernel.init(feats_train, feats_test)
out1_epsilon=svr_epsilon.apply().get_labels()
out2_epsilon=svr_epsilon.apply(feats_test).get_labels()
out1_nu=svr_epsilon.apply().get_labels()
out2_nu=svr_epsilon.apply(feats_test).get_labels()
return out1_epsilon,out2_epsilon,out1_nu,out2_nu ,kernel
开发者ID:Argram,项目名称:shogun,代码行数:42,代码来源:regression_libsvr_modular.py
示例20: classifier_multiclassmachine_modular
def classifier_multiclassmachine_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun.Features import RealFeatures, MulticlassLabels
from shogun.Kernel import GaussianKernel
from shogun.Classifier import LibSVM, KernelMulticlassMachine, MulticlassOneVsRestStrategy
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)
kernel=GaussianKernel(feats_train, feats_train, width)
labels=MulticlassLabels(label_train_multiclass)
classifier = LibSVM()
classifier.set_epsilon(epsilon)
#print labels.get_labels()
mc_classifier = KernelMulticlassMachine(MulticlassOneVsRestStrategy(),kernel,classifier,labels)
mc_classifier.train()
kernel.init(feats_train, feats_test)
out = mc_classifier.apply().get_labels()
return out
开发者ID:lgatto,项目名称:shogun,代码行数:20,代码来源:classifier_multiclassmachine_modular.py
注:本文中的shogun.Kernel.GaussianKernel类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论