Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
translate mkl.py from python 2to3
  • Loading branch information
snikumbh committed Aug 13, 2019
1 parent 3ae5a5f commit ef0db55
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions mkl.py
Expand Up @@ -20,9 +20,9 @@
from modshogun import MKLClassification
from modshogun import LibSVM
except ImportError:
print "ImportError:"
print "Importing the large-scale machine learning toolbox SHOGUN failed."
print "Refer to instructions on http://www.shogun-toolbox.org/ for installing SHOGUN with the python_modular interface."
print("ImportError:")
print("Importing the large-scale machine learning toolbox SHOGUN failed.")
print("Refer to instructions on http://www.shogun-toolbox.org/ for installing SHOGUN with the python_modular interface.")
raise


Expand All @@ -46,7 +46,7 @@
parser.add_option("-L", "--testlabelsFilename", type="str", action="store", default = "kernels/testlabels.txt",
dest="testlabelsFilename", help="Name of the file providing the testlabels [default: %default]")
parser.add_option("-I", "--imbalance", type="float", action="store", default = "1.0",
dest="Imbalance", help="Imbalance, multiplied with cost parameter for SVM negative class [default: %default]")
dest="Imbalance", help="Imbalance, multiplied with cost parameter for SVM minority class [default: %default]")
parser.add_option("-t", "--whetherPerformTest", type="int", action="store", default = 1,
dest="performTest", help="Specify 1 if performTest, else 0.[default: %default]")

Expand All @@ -66,7 +66,7 @@
trainkernel = CombinedKernel()
for k in range(0, len(kernelFilenames)):
thisFname = kernelFilenames[k]
print thisFname
print(thisFname)
if os.path.exists(thisFname):
trainkernel.append_kernel(CustomKernel(numpy.loadtxt(thisFname, delimiter=',')))
else:
Expand All @@ -80,7 +80,7 @@
mkl.set_mkl_norm(options.MKLNorm) #2,3

# set cost (neg, pos)
mkl.set_C(options.C*options.Imbalance, options.C)
mkl.set_C(options.C, options.C*options.Imbalance)
mkl.set_C_mkl(options.C)
# set kernel and labels
mkl.set_kernel(trainkernel)
Expand All @@ -94,20 +94,20 @@
mkl.set_epsilon(1e-5)
mkl.parallel.set_num_threads(10)
mkl.io.disable_progress()
print "Before SVM train with MKL"
print("Before SVM train with MKL")
try:
mkl.train()
except:
print "Train error caught here"
print("Train error caught here")
raise

print "Training done"
print("Training done")
alphas = mkl.get_alphas()
bias = mkl.get_bias()
sv = mkl.get_support_vectors()
print str(bias)
print(str(bias))
kw = trainkernel.get_subkernel_weights()
print kw
print(kw)
head, tail = os.path.split(options.subkernelWeightsFilename)
biasFilename = head+'/biasValue.txt'
alphasFilename = head+'/alphas.txt'
Expand All @@ -121,7 +121,7 @@
numpy.savetxt(svIndicesFilename, sv, fmt='%d', delimiter=',')
# write the subkernel weights to file
numpy.savetxt(options.subkernelWeightsFilename, kw, fmt='%.10f', delimiter=',')
print "Subkernel weights, bias and alphas written to individual files"
print("Subkernel weights, bias and alphas written to individual files")


# separately for test, one will have to
Expand All @@ -146,9 +146,9 @@
predictions = mkl.apply().get_values()

numpy.savetxt(options.predictedlabelsFilename, predictions, fmt='%.10f', delimiter=',')
print "Predicted labels written to file"
print("Predicted labels written to file")
else:
predictions = mkl.apply().get_values()
numpy.savetxt(options.predictedlabelsFilename, predictions, fmt='%.10f', delimiter=',')
print "Training prediction labels written to file"
print("Training prediction labels written to file")

0 comments on commit ef0db55

Please sign in to comment.