Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
SVMKit has been deprecated and has been renamed to Rumale. Initially, I started developing SVMKit as an experimental library aiming at implementing SVM in Ruby. However, since I added many other machine learning algorithms to SVMKit, I decided to change the library name. SVMKit will continue releasing for bugfix but will not add new features.
SVMKit is a machine learninig library in Ruby. SVMKit provides machine learning algorithms with interfaces similar to Scikit-Learn in Python. SVMKit supports Linear / Kernel Support Vector Machine, Logistic Regression, Linear Regression, Ridge, Lasso, Factorization Machine, Naive Bayes, Decision Tree, AdaBoost, Random Forest, K-nearest neighbor classifier, K-Means, DBSCAN, Principal Component Analysis, and Non-negative Matrix Factorization.
Add this line to your application's Gemfile:
gem 'svmkit'
And then execute:
$ bundle
Or install it yourself as:
$ gem install svmkit
SVMKit provides function loading libsvm format dataset file. We start by downloading the pendigits dataset from LIBSVM Data web site.
$ wget https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits
$ wget https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits.t
Training of the classifier with Linear SVM and RBF kernel feature map is the following code.
require 'svmkit'
# Load the training dataset.
samples, labels = SVMKit::Dataset.load_libsvm_file('pendigits')
# Map training data to RBF kernel feature space.
transformer = SVMKit::KernelApproximation::RBF.new(gamma: 0.0001, n_components: 1024, random_seed: 1)
transformed = transformer.fit_transform(samples)
# Train linear SVM classifier.
classifier = SVMKit::LinearModel::SVC.new(reg_param: 0.0001, max_iter: 1000, batch_size: 50, random_seed: 1)
classifier.fit(transformed, labels)
# Save the model.
File.open('transformer.dat', 'wb') { |f| f.write(Marshal.dump(transformer)) }
File.open('classifier.dat', 'wb') { |f| f.write(Marshal.dump(classifier)) }
Classifying testing data with the trained classifier is the following code.
require 'svmkit'
# Load the testing dataset.
samples, labels = SVMKit::Dataset.load_libsvm_file('pendigits.t')
# Load the model.
transformer = Marshal.load(File.binread('transformer.dat'))
classifier = Marshal.load(File.binread('classifier.dat'))
# Map testing data to RBF kernel feature space.
transformed = transformer.transform(samples)
# Classify the testing data and evaluate prediction results.
puts("Accuracy: %.1f%%" % (100.0 * classifier.score(transformed, labels)))
# Other evaluating approach
# results = classifier.predict(transformed)
# evaluator = SVMKit::EvaluationMeasure::Accuracy.new
# puts("Accuracy: %.1f%%" % (100.0 * evaluator.score(results, labels)))
Execution of the above scripts result in the following.
$ ruby train.rb
$ ruby test.rb
Accuracy: 98.4%
require 'svmkit'
# Load dataset.
samples, labels = SVMKit::Dataset.load_libsvm_file('pendigits')
# Define the estimator to be evaluated.
lr = SVMKit::LinearModel::LogisticRegression.new(reg_param: 0.0001, random_seed: 1)
# Define the evaluation measure, splitting strategy, and cross validation.
ev = SVMKit::EvaluationMeasure::LogLoss.new
kf = SVMKit::ModelSelection::StratifiedKFold.new(n_splits: 5, shuffle: true, random_seed: 1)
cv = SVMKit::ModelSelection::CrossValidation.new(estimator: lr, splitter: kf, evaluator: ev)
# Perform 5-cross validation.
report = cv.perform(samples, labels)
# Output result.
mean_logloss = report[:test_score].inject(:+) / kf.n_splits
puts("5-CV mean log-loss: %.3f" % mean_logloss)
require 'svmkit'
# Load dataset.
samples, labels = SVMKit::Dataset.load_libsvm_file('pendigits')
# Construct pipeline with kernel approximation and SVC.
rbf = SVMKit::KernelApproximation::RBF.new(gamma: 0.0001, n_components: 800, random_seed: 1)
svc = SVMKit::LinearModel::SVC.new(reg_param: 0.0001, max_iter: 1000, random_seed: 1)
pipeline = SVMKit::Pipeline::Pipeline.new(steps: { trns: rbf, clsf: svc })
# Define the splitting strategy and cross validation.
kf = SVMKit::ModelSelection::StratifiedKFold.new(n_splits: 5, shuffle: true, random_seed: 1)
cv = SVMKit::ModelSelection::CrossValidation.new(estimator: pipeline, splitter: kf)
# Perform 5-cross validation.
report = cv.perform(samples, labels)
# Output result.
mean_accuracy = report[:test_score].inject(:+) / kf.n_splits
puts("5-CV mean accuracy: %.1f %%" % (mean_accuracy * 100.0))
Execution of the above scripts result in the following.
$ ruby pipeline.rb
5-CV mean accuracy: 99.2 %
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/svmkit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the BSD 2-clause License.
Everyone interacting in the SVMKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that svmkit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.