def BuildModel(self, data, labels, options): cart = CARTree() cart.set_feature_types(np.array([False] * data.get_num_features())) cart.set_labels(labels) cart.train(data) return cart
def regression_cartree_modular(num_train=500,num_test=50,x_range=15,noise_var=0.2,ft=feattypes): try: from modshogun import RealFeatures, RegressionLabels, CSVFile, CARTree, PT_REGRESSION from numpy import random except ImportError: print("Could not import Shogun and/or numpy modules") return random.seed(1) # form training dataset : y=x with noise X_train=random.rand(1,num_train)*x_range; Y_train=X_train+random.randn(num_train)*noise_var # form test dataset X_test=array([[float(i)/num_test*x_range for i in range(num_test)]]) # wrap features and labels into Shogun objects feats_train=RealFeatures(X_train) feats_test=RealFeatures(X_test) train_labels=RegressionLabels(Y_train[0]) # CART Tree formation c=CARTree(ft,PT_REGRESSION,5,True) c.set_labels(train_labels) c.train(feats_train) # Classify test data output=c.apply_regression(feats_test).get_labels() return c,output
def stochasticgbmachine_modular(train=traindat,train_labels=label_traindat,ft=feat_types): try: from modshogun import RealFeatures, RegressionLabels, CSVFile, CARTree, StochasticGBMachine, SquaredLoss except ImportError: print("Could not import Shogun modules") return # wrap features and labels into Shogun objects feats=RealFeatures(CSVFile(train)) labels=RegressionLabels(CSVFile(train_labels)) # divide into training (90%) and test dataset (10%) p=np.random.permutation(labels.get_num_labels()) num=labels.get_num_labels()*0.9 cart=CARTree() cart.set_feature_types(ft) cart.set_max_depth(1) loss=SquaredLoss() s=StochasticGBMachine(cart,loss,500,0.01,0.6) # train feats.add_subset(np.int32(p[0:num])) labels.add_subset(np.int32(p[0:num])) s.set_labels(labels) s.train(feats) feats.remove_subset() labels.remove_subset() # apply feats.add_subset(np.int32(p[num:len(p)])) labels.add_subset(np.int32(p[num:len(p)])) output=s.apply_regression(feats) feats.remove_subset() labels.remove_subset() return s,output
def multiclass_cartree_modular(train=traindat, test=testdat, labels=label_traindat, ft=feattypes): try: from modshogun import RealFeatures, MulticlassLabels, CSVFile, CARTree, PT_MULTICLASS except ImportError: print("Could not import Shogun modules") return # wrap features and labels into Shogun objects feats_train = RealFeatures(CSVFile(train)) feats_test = RealFeatures(CSVFile(test)) train_labels = MulticlassLabels(CSVFile(labels)) # CART Tree formation with 5 fold cross-validation pruning c = CARTree(ft, PT_MULTICLASS, 5, True) c.set_labels(train_labels) c.train(feats_train) # Classify test data output = c.apply_multiclass(feats_test).get_labels() return c, output
def multiclass_cartree_modular(train=traindat,test=testdat,labels=label_traindat,ft=feattypes): try: from modshogun import RealFeatures, MulticlassLabels, CSVFile, CARTree, PT_MULTICLASS except ImportError: print("Could not import Shogun modules") return # wrap features and labels into Shogun objects feats_train=RealFeatures(CSVFile(train)) feats_test=RealFeatures(CSVFile(test)) train_labels=MulticlassLabels(CSVFile(labels)) # CART Tree formation with 5 fold cross-validation pruning c=CARTree(ft,PT_MULTICLASS,5,True) c.set_labels(train_labels) c.train(feats_train) # Classify test data output=c.apply_multiclass(feats_test).get_labels() return c,output
def stochasticgbmachine_modular(train=traindat, train_labels=label_traindat, ft=feat_types): try: from modshogun import RealFeatures, RegressionLabels, CSVFile, CARTree, StochasticGBMachine, SquaredLoss except ImportError: print("Could not import Shogun modules") return # wrap features and labels into Shogun objects feats = RealFeatures(CSVFile(train)) labels = RegressionLabels(CSVFile(train_labels)) # divide into training (90%) and test dataset (10%) p = np.random.permutation(labels.get_num_labels()) num = labels.get_num_labels() * 0.9 cart = CARTree() cart.set_feature_types(ft) cart.set_max_depth(1) loss = SquaredLoss() s = StochasticGBMachine(cart, loss, 500, 0.01, 0.6) # train feats.add_subset(np.int32(p[0:num])) labels.add_subset(np.int32(p[0:num])) s.set_labels(labels) s.train(feats) feats.remove_subset() labels.remove_subset() # apply feats.add_subset(np.int32(p[num:len(p)])) labels.add_subset(np.int32(p[num:len(p)])) output = s.apply_regression(feats) feats.remove_subset() labels.remove_subset() return s, output