def RunKMeansShogun(q): import numpy as np from shogun.Distance import EuclideanDistance from shogun.Features import RealFeatures from shogun import Clustering from shogun.Mathematics import Math_init_random totalTimer = Timer() if seed: Math_init_random(seed.group(1)) try: data = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(data.T) distance = EuclideanDistance(dataFeat, dataFeat) # Create the K-Means object and perform K-Means clustering. with totalTimer: model = Clustering.KMeans(int(clusters.group(1)), distance) model.set_max_iter(maxIterations) model.train() labels = model.apply().get_labels() centers = model.get_cluster_centers() except Exception as e: q.put(-1) return -1 time = totalTimer.ElapsedTime() q.put(time) return time
def classifier_larank_modular(fm_train_real=traindat, fm_test_real=testdat, label_train_multiclass=label_traindat, C=0.9, num_threads=1, num_iter=5): from shogun.Features import RealFeatures, Labels from shogun.Kernel import GaussianKernel from shogun.Classifier import LaRank from shogun.Mathematics import Math_init_random Math_init_random(17) feats_train = RealFeatures(fm_train_real) feats_test = RealFeatures(fm_test_real) width = 2.1 kernel = GaussianKernel(feats_train, feats_train, width) epsilon = 1e-5 labels = Labels(label_train_multiclass) svm = LaRank(C, kernel, labels) #svm.set_tau(1e-3) svm.set_batch_mode(False) #svm.io.enable_progress() svm.set_epsilon(epsilon) svm.train() out = svm.apply(feats_train).get_labels() predictions = svm.apply() return predictions, svm, predictions.get_labels()
def run_clustering(data, k): from shogun.Clustering import KMeans from shogun.Mathematics import Math_init_random Math_init_random(42) fea = RealFeatures(data) distance = EuclidianDistance(fea, fea) kmeans = KMeans(k, distance) print("Running clustering...") kmeans.train() return kmeans.get_cluster_centers()
def clustering_kmeans_modular(fm_train=traindat, k=3): from shogun.Distance import EuclidianDistance from shogun.Features import RealFeatures from shogun.Clustering import KMeans from shogun.Mathematics import Math_init_random Math_init_random(17) feats_train = RealFeatures(fm_train) distance = EuclidianDistance(feats_train, feats_train) kmeans = KMeans(k, distance) kmeans.train() out_centers = kmeans.get_cluster_centers() kmeans.get_radiuses() return out_centers, kmeans
def features_director_dot_modular(fm_train_real, fm_test_real, label_train_twoclass, C, epsilon): from shogun.Features import RealFeatures, SparseRealFeatures, BinaryLabels from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC_DUAL from shogun.Mathematics import Math_init_random Math_init_random(17) feats_train = RealFeatures(fm_train_real) feats_test = RealFeatures(fm_test_real) labels = BinaryLabels(label_train_twoclass) dfeats_train = NumpyFeatures(fm_train_real) dfeats_test = NumpyFeatures(fm_test_real) dlabels = BinaryLabels(label_train_twoclass) print feats_train.get_computed_dot_feature_matrix() print dfeats_train.get_computed_dot_feature_matrix() svm = LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL) svm.set_epsilon(epsilon) svm.set_bias_enabled(True) svm.train() svm.set_features(feats_test) svm.apply().get_labels() predictions = svm.apply() dfeats_train.__disown__() dfeats_train.parallel.set_num_threads(1) dsvm = LibLinear(C, dfeats_train, dlabels) dsvm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL) dsvm.set_epsilon(epsilon) dsvm.set_bias_enabled(True) dsvm.train() dfeats_test.__disown__() dfeats_test.parallel.set_num_threads(1) dsvm.set_features(dfeats_test) dsvm.apply().get_labels() dpredictions = dsvm.apply() return predictions, svm, predictions.get_labels()
def classifier_liblinear_modular(fm_train_real, fm_test_real, label_train_twoclass, C, epsilon): from shogun.Features import RealFeatures, SparseRealFeatures, Labels from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC_DUAL from shogun.Mathematics import Math_init_random Math_init_random(17) feats_train = RealFeatures(fm_train_real) feats_test = RealFeatures(fm_test_real) labels = Labels(label_train_twoclass) svm = LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL) svm.set_epsilon(epsilon) svm.set_bias_enabled(True) svm.train() svm.set_features(feats_test) svm.apply().get_labels() predictions = svm.apply() return predictions, svm, predictions.get_labels()
def classifier_multiclassocas_modular(num_vec=10, num_class=3, distance=15, width=2.1, C=1, epsilon=1e-5, seed=1): from shogun.Features import RealFeatures, MulticlassLabels from shogun.Classifier import MulticlassOCAS from shogun.Mathematics import Math_init_random # reproducible results random.seed(seed) Math_init_random(seed) # generate some training data where each class pair is linearly separable label_train = array([mod(x, num_class) for x in range(num_vec)], dtype="float64") label_test = array([mod(x, num_class) for x in range(num_vec)], dtype="float64") fm_train = array(random.randn(num_class, num_vec)) fm_test = array(random.randn(num_class, num_vec)) for i in range(len(label_train)): fm_train[label_train[i], i] += distance fm_test[label_test[i], i] += distance feats_train = RealFeatures(fm_train) feats_test = RealFeatures(fm_test) labels = MulticlassLabels(label_train) classifier = MulticlassOCAS(C, feats_train, labels) classifier.train() out = classifier.apply(feats_test).get_labels() #print label_test #print out return out, classifier
def classifier_larank_modular (num_vec,num_class,distance,C=0.9,num_threads=1,num_iter=5,seed=1): from shogun.Features import RealFeatures, MulticlassLabels from shogun.Kernel import GaussianKernel from shogun.Classifier import LaRank from shogun.Mathematics import Math_init_random # reproducible results Math_init_random(seed) random.seed(seed) # generate some training data where each class pair is linearly separable label_train=array([mod(x,num_class) for x in range(num_vec)],dtype="float64") label_test=array([mod(x,num_class) for x in range(num_vec)],dtype="float64") fm_train=array(random.randn(num_class,num_vec)) fm_test=array(random.randn(num_class,num_vec)) for i in range(len(label_train)): fm_train[label_train[i],i]+=distance fm_test[label_test[i],i]+=distance feats_train=RealFeatures(fm_train) feats_test=RealFeatures(fm_test) width=2.1 kernel=GaussianKernel(feats_train, feats_train, width) epsilon=1e-5 labels=MulticlassLabels(label_train) svm=LaRank(C, kernel, labels) #svm.set_tau(1e-3) svm.set_batch_mode(False) #svm.io.enable_progress() svm.set_epsilon(epsilon) svm.train() out=svm.apply(feats_test).get_labels() predictions = svm.apply() return predictions, svm, predictions.get_labels()