#!/usr/bin/env python3
#
# script for generating a hashing trick version of an svm dataset
#
# note this uses a binary representation of the input data, not the
# raw svm, and writes that out as well.
#
# python3 hashing_trick.py <path-to-svm>.svm budget
#
# Again, note the argument svmfile is not read, instead we look at
#
# <path-to-svm>.{data,indices,indptr,y}.bin, which are the binary
# equivalents generated by svm2bins rust executable.
#
# writes out to <path-to-svm>.ht<budget>.{data,indices,indptr,y}.bin

import sys
assert len(sys.argv) == 3, sys.argv

svmfile = sys.argv[1]
budget = int(sys.argv[2])

assert svmfile.endswith('.svm'), svmfile
outprefix = svmfile[:len(svmfile) - len('.svm')]

from svmlight_loader_install import binprefix

X, y = binprefix(svmfile)

rows, cols = X.shape

import numpy as np
np.random.seed(1234)
ixs = np.random.randint(budget, size=cols)
sgn = 2 * np.random.randint(1, size=cols) - 1
indptr = np.arange(len(ixs) + 1, dtype=np.uint32)

from scipy import sparse as sps

X = X.dot(sps.csc_matrix((sgn, ixs, indptr)).T)

dtypes = [np.float64, np.uint32, np.uint64, np.float64]
arrays = [X.data, X.indices, X.indptr, y]
suffixes = ['data', 'indices', 'indptr', 'y']
for dtype, array, suffix in zip(dtypes, arrays, suffixes):
    outfn = outprefix + '.ht{}.{}.bin'.format(budget, suffix)
    array = array.astype(dtype)
    array.tofile(outfn)

#!/usr/bin/env python3
#
# script for generating a hashing trick version of an svm dataset
#
# note this uses a binary representation of the input data, not the
# raw svm, and writes that out as well.
#
# python3 hashing_trick.py <path-to-svm>.svm budget
#
# Again, note the argument svmfile is not read, instead we look at
#
# <path-to-svm>.{data,indices,indptr,y}.bin, which are the binary
# equivalents generated by svm2bins rust executable.
#
# writes out to <path-to-svm>.ht<budget>.{data,indices,indptr,y}.bin

import sys
assert len(sys.argv) == 3, sys.argv

svmfile = sys.argv[1]
budget = int(sys.argv[2])

assert svmfile.endswith('.svm'), svmfile
outprefix = svmfile[:len(svmfile) - len('.svm')]

from svmlight_loader_install import binprefix

X, y = binprefix(svmfile)

rows, cols = X.shape

import numpy as np
np.random.seed(1234)
ixs = np.random.randint(budget, size=cols)
sgn = 2 * np.random.randint(1, size=cols) - 1
indptr = np.arange(len(ixs) + 1, dtype=np.uint32)

from scipy import sparse as sps

X = X.dot(sps.csc_matrix((sgn, ixs, indptr)).T)

dtypes = [np.float64, np.uint32, np.uint64, np.float64]
arrays = [X.data, X.indices, X.indptr, y]
suffixes = ['data', 'indices', 'indptr', 'y']
for dtype, array, suffix in zip(dtypes, arrays, suffixes):
    outfn = outprefix + '.ht{}.{}.bin'.format(budget, suffix)
    array = array.astype(dtype)
    array.tofile(outfn)

#!/usr/bin/env python3 # # script for generating a hashing trick version of an svm dataset # # note this uses a binary representation of the input data, not the # raw svm, and writes that out as well. # # python3 hashing_trick.py <path-to-svm>.svm budget # # Again, note the argument svmfile is not read, instead we look at # # <path-to-svm>.{data,indices,indptr,y}.bin, which are the binary # equivalents generated by svm2bins rust executable. # # writes out to <path-to-svm>.ht<budget>.{data,indices,indptr,y}.bin