sklearn-porter

Repository: https://github.com/nok/sklearn-porter

NuSVC

Documentation: sklearn.svm.NuSVC


In [1]:
import sys
sys.path.append('../../../../..')

Load data


In [2]:
from sklearn.datasets import load_iris

iris_data = load_iris()

X = iris_data.data
y = iris_data.target

print(X.shape, y.shape)


((150, 4), (150,))

Train classifier


In [3]:
from sklearn import svm

clf = svm.NuSVC(gamma=0.001, kernel='rbf', random_state=0)
clf.fit(X, y)


Out[3]:
NuSVC(cache_size=200, class_weight=None, coef0=0.0,
   decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',
   max_iter=-1, nu=0.5, probability=False, random_state=0, shrinking=True,
   tol=0.001, verbose=False)

Transpile classifier


In [4]:
from sklearn_porter import Porter

porter = Porter(clf, language='ruby')
output = porter.export()

print(output)


class NuSVC

	def initialize (nClasses, nRows, vectors, coefficients, intercepts, weights, kernel, gamma, coef0, degree)
		@nClasses = nClasses
		@classes = Array.new(nClasses)
		for i in 0 ... nClasses
			@classes[i] = i
		end
		@nRows = nRows

		@vectors = vectors
		@coefficients = coefficients
		@intercepts = intercepts
		@weights = weights

		@kernel = kernel.upcase
		@gamma = gamma
		@coef0 = coef0
		@degree = degree
	end

	def predict (features)
    
    	kernels = Array.new(@vectors.length)
    	case @kernel
    	when "LINEAR"
    		for i in 0 ... @vectors.length
    			kernel = 0
    			for j in 0 ... @vectors[i].length
    				kernel += @vectors[i][j] * features[j]
    			end
    			kernels[i] = kernel
    		end
    	when 'POLY'
    		for i in 0 ... @vectors.length
    			kernel = 0
    			for j in 0 ... @vectors[i].length
    				kernel += @vectors[i][j] * features[j]
    			end
    			kernels[i] = (((@gamma * kernel) + @coef0) ** @degree)
    		end
    	when "RBF"
    		for i in 0 ... @vectors.length
    			kernel = 0
    			for j in 0 ... @vectors[i].length
    				kernel += ((@vectors[i][j] - features[j]) ** 2)
    			end
    			kernels[i] = Math.exp(-@gamma * kernel)
    		end
    	when 'SIGMOID'
    		for i in 0 ... @vectors.length
    			kernel = 0
    			for j in 0 ... @vectors[i].length
    				kernel += @vectors[i][j] * features[j]
    			end
    			kernels[i] = Math.tanh((@gamma * kernel) + @coef0)
    		end
    	end
    
    	starts = Array.new(@nRows, 0)
    	for i in 0 ... @nRows
    		if i != 0
    			start = 0
    			for j in 0 ... i
    				start += @weights[j]
    			end
    			starts[i] = start
    		else
    			starts[0] = 0
    		end
    	end
    
    	ends = Array.new(@nRows, 0)
    	for i in 0 ... @nRows
    		ends[i] = @weights[i] + starts[i]
    	end
    
    	if @nClasses == 2
    
    		for i in 0 ... kernels.length
    			kernels[i] = -kernels[i]
    		end
    
    		decision = 0
    		for k in starts[1] ... ends[1]
    			decision += kernels[k] * @coefficients[0][k]
    		end
    		for k in starts[0] ... ends[0]
    			decision += kernels[k] * @coefficients[0][k]
    		end
    		decision += @intercepts[0];
    
    		if decision > 0
    			return 0
    		end
    		return 1
    
    	end
    
    	decisions = Array.new(@intercepts.length, 0)
    	d = 0
    	for i in 0 ... @nRows
    		for j in i + 1 ... @nRows
    			tmp = 0
    			for k in starts[j] ... ends[j]
    				tmp += @coefficients[i][k] * kernels[k]
    			end
    			for k in starts[i] ... ends[i]
    				tmp += @coefficients[j - 1][k] * kernels[k]
    			end
    			decisions[d] = tmp + @intercepts[d]
    			d = d + 1
    		end
    	end
    
    	votes = Array.new(@intercepts.length, 0)
    	d = 0
    	for i in 0 ... @nRows
    		for j in i + 1 ... @nRows
    			votes[d] = decisions[d] > 0 ? i : j
    			d = d + 1
    		end
    	end
    
    	amounts = Array.new(@nClasses, 0)
    	for i in 0 ... votes.length
    		amounts[votes[i]] += 1
    	end
    
    	classVal = -1
    	classIdx = -1
    	for i in 0 ... amounts.length
    		if amounts[i] > classVal
    			classVal = amounts[i]
    			classIdx = i
    		end
    	end
    	return @classes[classIdx]
    
    end

end

if ARGV.length == 4

	# Features:
	features = ARGV.collect { |i| i.to_f }

	# Parameters:
	vectors = [[4.9, 3.0, 1.4, 0.2], [4.6, 3.1, 1.5, 0.2], [5.4, 3.9, 1.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.4], [6.7, 3.0, 5.0, 1.7], [6.0, 2.9, 4.5, 1.5], [5.7, 2.6, 3.5, 1.0], [5.5, 2.4, 3.8, 1.1], [5.5, 2.4, 3.7, 1.0], [5.8, 2.7, 3.9, 1.2], [6.0, 2.7, 5.1, 1.6], [5.4, 3.0, 4.5, 1.5], [6.0, 3.4, 4.5, 1.6], [6.7, 3.1, 4.7, 1.5], [6.3, 2.3, 4.4, 1.3], [5.6, 3.0, 4.1, 1.3], [5.5, 2.5, 4.0, 1.3], [5.5, 2.6, 4.4, 1.2], [6.1, 3.0, 4.6, 1.4], [5.8, 2.6, 4.0, 1.2], [5.0, 2.3, 3.3, 1.0], [5.6, 2.7, 4.2, 1.3], [5.7, 3.0, 4.2, 1.2], [5.7, 2.9, 4.2, 1.3], [6.2, 2.9, 4.3, 1.3], [5.1, 2.5, 3.0, 1.1], [5.7, 2.8, 4.1, 1.3], [5.8, 2.7, 5.1, 1.9], [6.3, 2.9, 5.6, 1.8], [4.9, 2.5, 4.5, 1.7], [6.5, 3.2, 5.1, 2.0], [6.4, 2.7, 5.3, 1.9], [5.7, 2.5, 5.0, 2.0], [5.8, 2.8, 5.1, 2.4], [6.4, 3.2, 5.3, 2.3], [6.5, 3.0, 5.5, 1.8], [6.0, 2.2, 5.0, 1.5], [5.6, 2.8, 4.9, 2.0], [6.3, 2.7, 4.9, 1.8], [6.2, 2.8, 4.8, 1.8], [6.1, 3.0, 4.9, 1.8], [7.2, 3.0, 5.8, 1.6], [6.3, 2.8, 5.1, 1.5], [6.1, 2.6, 5.6, 1.4], [6.4, 3.1, 5.5, 1.8], [6.0, 3.0, 4.8, 1.8], [6.9, 3.1, 5.4, 2.1], [6.9, 3.1, 5.1, 2.3], [5.8, 2.7, 5.1, 1.9], [6.7, 3.0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]]
	coefficients = [[4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 0.0, 4.680538527007988, 0.0, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 4.680538527007988, 0.0, -0.0, -0.0, -0.0, -4.680538527007988, -0.0, -0.0, -0.0, -4.680538527007988, -0.0, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -0.0, -4.680538527007988, -0.0, -0.0, -4.680538527007988, -0.0, -4.680538527007988, -0.0, -4.680538527007988, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -0.0, -0.0, -0.0, -0.0, -0.0, -4.680538527007988, -4.680538527007988, -4.680538527007988, -0.0, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -4.680538527007988, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -0.0, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -0.0, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366, -2.1228182659346366], [0.0, 0.0, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 0.0, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 2.1228182659346366, 47.52934177369389, 47.52934177369389, 47.52934177369389, 0.0, 47.52934177369389, 47.52934177369389, 47.52934177369389, 0.0, 47.52934177369389, 0.0, 0.0, 0.0, 0.0, 47.52934177369389, 0.0, 47.52934177369389, 47.52934177369389, 0.0, 47.52934177369389, 0.0, 47.52934177369389, 0.0, 47.52934177369389, 47.52934177369389, 47.52934177369389, 47.52934177369389, 47.52934177369389, 47.52934177369389, 0.0, 0.0, 0.0, 0.0, 47.52934177369389, 47.52934177369389, 47.52934177369389, 47.52934177369389, 47.52934177369389, 0.0, 0.0, 47.52934177369389, 47.52934177369389, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -0.0, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -47.52934177369389, -0.0, -47.52934177369389]]
	intercepts = [0.09572808365772528, 0.049757317370245795, -0.08418168966801846]
	weights = [28, 49, 27]

	# Prediction:
	clf = NuSVC.new 3, 3, vectors, coefficients, intercepts, weights, "rbf", 0.001, 0.0, 3
	estimation = clf.predict features
	puts estimation

end