#To import all shogun classes from modshogun import * #Generate some random data X = 2 * random.randn(20,2) traindata=r_[X + 5, X + 8].T print traindata feats_train=RealFeatures(traindata) #create array of labels with 1 and -1 trainlab=concatenate((ones(20),-ones(20))) print trainlab #convert to shogun format labels labels=BinaryLabels(trainlab) # Plot the training data figure(figsize=(6,4)) gray() _=scatter(traindata[0, :], traindata[1,:], c=labels, s=50) title("Training Data") gray() #prameters to svm C=0.9 epsilon=1e-3 svm=LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC) svm.set_epsilon(epsilon) #train svm.train() size=100 x1=linspace(0, 14, size) x2=linspace(0, 14, size) x, y=meshgrid(x1, x2) #Generate X-Y grid test data grid=RealFeatures(array((ravel(x), ravel(y)))) #apply on test grid predictions = svm.apply(grid) z=predictions.get_values().reshape((size, size)) #plot jet() figure(figsize=(8,6)) title("Classification") c=pcolor(x, y, z) _=contour(x, y, z, linewidths=1, colors='black', hold=True) _=colorbar(c) gray() _=scatter(traindata[0, :], traindata[1,:], c=labels, s=50) gray() f=SparseRealFeatures() #Load the file and generate labels. trainlab=f.load_with_labels(LibSVMFile('../../../data/toy/diabetes_scale.svm')) labels=BinaryLabels(trainlab) #Get the feature matrix mat=f.get_full_feature_matrix() feats=array(mat[1]) feats=vstack((feats, array(mat[5]))) print feats, feats.shape #convert to shogun format feats_train=RealFeatures(feats) #plot the training data figure(figsize=(6,5)) _=scatter(feats[0, :], feats[1,:], c=trainlab, s=50) _=xlabel('Plasma glucose concentration') _=ylabel('Body mass index') C=0.9 epsilon=1e-3 svm=LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC) svm.set_epsilon(epsilon) #train svm.train() size=100 x1=linspace(-1.2, 1.2, size) x2=linspace(-1.2, 1.2, size) x, y=meshgrid(x1, x2) #Generate X-Y grid test data grid=RealFeatures(array((ravel(x), ravel(y)))) #apply on test grid predictions = svm.apply(grid) z=predictions.get_values().reshape((size, size)) #plot jet() figure(figsize=(8,6)) title("Classification") c=pcolor(x, y, z) _=contour(x, y, z, linewidths=1, colors='black', hold=True) _=colorbar(c) gray() _=scatter(feats[0, :], feats[1,:], c=trainlab, s=50) _=xlabel('Plasma glucose concentration') _=ylabel('Body mass index') #split features for training and evaluation feats=array(mat[1]) feats_t=feats[0:700] feats_e=feats[700:785] feats=array(mat[5]) feats_t1=feats[0:700] feats_e1=feats[700:785] feats_t=vstack((feats_t, feats_t1)) feats_e=vstack((feats_e, feats_e1)) feats_train=RealFeatures(feats_t) feats_evaluate=RealFeatures(feats_e) label_t=trainlab[0:700] labels=BinaryLabels(label_t) label_e=trainlab[700:785] labels_true=BinaryLabels(label_e) svm=LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC) svm.set_epsilon(epsilon) #train and evaluate svm.train() output=svm.apply(feats_evaluate) #use ROCEvaluation to get accuracy evaluator=ROCEvaluation() print 'Accuracy(%):' print evaluator.evaluate(output,labels_true)*100