danielbedo/boilesdk

语言: Scala

git: https://github.com/danielbedo/boilesdk

README.md (中文)

boilesdk:scala中的无样板机器学习辅助库

Boilesdk旨在成为一个基于类型和依赖类型的辅助库,简化了常见问题 与使用无形的机器学习问题相关的任务。

目前的功能

将任意案例类转换为双精度向量

在我们的应用程序中,大多数时候我们使用模型(case)类来表示我们的数据。 通常,第一个任务 - 无论我们想要使用的机器学习算法 - 是将这些类转换为双精度矢量。

case class House(size: Double, numRooms: Int, price: Int) {
    def toDouble(): Vector[Double] = Vector(size, numRooms.toDouble, price.toDouble) 
}

如果你只有一个课程并且不经常改变,这是完全正常的,但是一直都要更新它是一项繁琐的工作。 如果您在嵌套的案例类中编码了50个功能,那么每天都会在尝试使用您的功能时进行更改。 您可以在运行时使用反射检查类并在那里添加方法,但是如果您更改了case类但忘记更改吐出Vector表示的方法 你可能只能在运行时找到它。

一个简单的案例类

让我们在编译时解决这些问题并使用无形来为我们生成这个方法。 您需要做的就是定义您的case类并导入doubleConversion,如下所示:

import boilesdk.derivation.generic.doubleConversion._

case class House(size: Double, numRooms: Int, price: Int, free: Boolean)
val house = House(51.5, 3, 122, false)
println(house.toDouble)

输出:

Vector(51.5, 3.0, 122.0, 0.0)
为缺少的类型添加自定义转换器

目前,可以使用以下基本类型的转换:Int,Double,Boolean。 如果你的类包含其他类型,那么方法toDouble将无法在其上使用,尽管你很容易 将缺少类型的隐式转换器添加到协同对象,这将使其可用。

case class House(size: Double, numRooms: Int, price: Int, free: Boolean, city: String)

// String needs a converter as it's not provided by the library
object House {
  implicit val dcString: DoubleConverter[String] =
    // not a useful converter, any string just gets represented as 0.0
    new DoubleConverter[String] {
      def toDouble(x: String): Vector[Double] =
        Vector(0.0)
    }
}
val house = House(51.5, 3, 122, false, "Munich")
import House._  // don't forget to pull the new implicit into scope
println(house.toDouble)

输出:

Vector(51.5, 3.0, 122.0, 0.0, 0.0)
嵌套案例类

嵌套的案例类也会变平:

case class House(size: Double, numRooms: Int, wc: WindowCount)
case class WindowCount(num: Int)

val house = House(2.0, 3, WindowCount(5))
println(house.toDouble)

输出:

Vector(2.0, 3.0, 5.0)

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

boilesdk: boilerplate-free machine learning helper library in scala

Boilesdk aims to be a typeclass and dependent type based helper library that simplifies common
tasks associated with machine learning problems using shapeless.

Current features

Converting arbitrary case classes to vector of Doubles

Most of the time in our applications we represent our data using model (case) classes.
Usually the first task - regardless of the machine learning algorithm we want to use - is to convert these classes to a vector of doubles.

case class House(size: Double, numRooms: Int, price: Int) {
    def toDouble(): Vector[Double] = Vector(size, numRooms.toDouble, price.toDouble) 
}

This is perfectly fine if you have only one class and it's not changing often, but it get's a tedious task to update it all the time
if you have 50 features encoded in nested case classes that change everyday as you are experimenting with your features.
You could inspect the class at runtime using reflection and add the method there, but then if you change the case class but forget to change the method that spits out the Vector representation
you might only find it out at runtime.

A simple case class

Let's solve these problems at compile time and use shapeless to generate this method for us.
All you need to do is define your case class and import doubleConversion as follows:

import boilesdk.derivation.generic.doubleConversion._

case class House(size: Double, numRooms: Int, price: Int, free: Boolean)
val house = House(51.5, 3, 122, false)
println(house.toDouble)

output:

Vector(51.5, 3.0, 122.0, 0.0)
Adding custom converters for missing types

For now there are conversions available for the following primitive types: Int, Double, Boolean.
If your class contains some other type then the method toDouble won't be available on it, altough you can easily
add an implicit converter for the missing type to the companion object which will make it available.

case class House(size: Double, numRooms: Int, price: Int, free: Boolean, city: String)

// String needs a converter as it's not provided by the library
object House {
  implicit val dcString: DoubleConverter[String] =
    // not a useful converter, any string just gets represented as 0.0
    new DoubleConverter[String] {
      def toDouble(x: String): Vector[Double] =
        Vector(0.0)
    }
}
val house = House(51.5, 3, 122, false, "Munich")
import House._  // don't forget to pull the new implicit into scope
println(house.toDouble)

output:

Vector(51.5, 3.0, 122.0, 0.0, 0.0)
Nested case classes

Nested case classes also get flattened into the vector:

case class House(size: Double, numRooms: Int, wc: WindowCount)
case class WindowCount(num: Int)

val house = House(2.0, 3, WindowCount(5))
println(house.toDouble)

output:

Vector(2.0, 3.0, 5.0)