-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Multiclass Logistic Regression (#143)
* feat: MultiClass LogisticRegression * bump: to 1.2.0-BETA2
- Loading branch information
Showing
7 changed files
with
130 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/londogard/nlp/meachinelearning/encoders/Encoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.londogard.nlp.meachinelearning.encoders | ||
|
||
import org.jetbrains.kotlinx.multik.ndarray.data.* | ||
|
||
|
||
interface Encoder { | ||
fun fit(input: D1Array<Int>): Unit | ||
fun transform(input: D1Array<Int>): D2Array<Int> | ||
fun fitTransform(input: D1Array<Int>): D2Array<Int> { | ||
fit(input) | ||
return transform(input) | ||
} | ||
fun invert(input: D2Array<Int>): D1Array<Int> | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/londogard/nlp/meachinelearning/encoders/OneHotEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.londogard.nlp.meachinelearning.encoders | ||
|
||
import org.jetbrains.kotlinx.multik.api.mk | ||
import org.jetbrains.kotlinx.multik.api.zeros | ||
import org.jetbrains.kotlinx.multik.ndarray.data.* | ||
import org.jetbrains.kotlinx.multik.ndarray.operations.forEachIndexed | ||
import org.jetbrains.kotlinx.multik.ndarray.operations.max | ||
import kotlin.properties.Delegates | ||
|
||
class OneHotEncoder: Encoder { | ||
private var yMax by Delegates.notNull<Int>() | ||
|
||
override fun fit(input: D1Array<Int>) { | ||
yMax = (input.max() ?: 0) + 1 // minimum of 2 classes = [0,1] | ||
} | ||
|
||
override fun transform(input: D1Array<Int>): D2Array<Int> { | ||
val out = mk.zeros<Int>(input.shape[0], yMax) | ||
input.forEachIndexed { index, i -> out[index, i] = 1 } | ||
|
||
return out | ||
} | ||
|
||
override fun invert(input: D2Array<Int>): D1Array<Int> { | ||
return mk.math.argMaxD2(input, 1) | ||
} | ||
} |
8 changes: 6 additions & 2 deletions
8
src/main/kotlin/com/londogard/nlp/meachinelearning/predictors/BasePredictor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package com.londogard.nlp.meachinelearning.predictors | ||
|
||
import com.londogard.nlp.meachinelearning.predictors.classifiers.AutoOneHotClassifier | ||
import org.jetbrains.kotlinx.multik.ndarray.data.D2 | ||
import org.jetbrains.kotlinx.multik.ndarray.data.D2Array | ||
import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray | ||
|
||
interface BasePredictor<T: Number> { | ||
interface BasePredictor<T : Number> { | ||
fun fit(X: MultiArray<Float, D2>, y: D2Array<T>) | ||
fun predict(X: MultiArray<Float, D2>): D2Array<T> | ||
} | ||
} | ||
|
||
fun <T : BasePredictor<Int>> T.asAutoOneHotClassifier(): AutoOneHotClassifier<T> = | ||
AutoOneHotClassifier(this) |
21 changes: 21 additions & 0 deletions
21
.../kotlin/com/londogard/nlp/meachinelearning/predictors/classifiers/AutoOneHotClassifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.londogard.nlp.meachinelearning.predictors.classifiers | ||
|
||
import com.londogard.nlp.meachinelearning.encoders.OneHotEncoder | ||
import com.londogard.nlp.meachinelearning.predictors.BasePredictor | ||
import org.jetbrains.kotlinx.multik.ndarray.data.D1Array | ||
import org.jetbrains.kotlinx.multik.ndarray.data.D2 | ||
import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray | ||
|
||
class AutoOneHotClassifier<T : BasePredictor<Int>>(val predictor: T) : BasePredictor<Int> by predictor { | ||
private val oneHotEncoder = OneHotEncoder() | ||
|
||
@JvmName("fitSimple") | ||
fun fit(X: MultiArray<Float, D2>, y: D1Array<Int>) { | ||
val yEncoded = oneHotEncoder.fitTransform(y) | ||
predictor.fit(X, yEncoded) | ||
} | ||
|
||
fun predictSimple(X: MultiArray<Float, D2>): D1Array<Int> { | ||
return oneHotEncoder.invert(predictor.predict(X)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters