Skip to content

Instantly share code, notes, and snippets.

@Rohit-554
Created January 21, 2026 05:34
Show Gist options
  • Select an option

  • Save Rohit-554/fe35ebd085e94023ee6ee4f79c2ca246 to your computer and use it in GitHub Desktop.

Select an option

Save Rohit-554/fe35ebd085e94023ee6ee4f79c2ca246 to your computer and use it in GitHub Desktop.
unsupervisedLearning.kt
package io.jadu.nivi.ai
import org.apache.commons.csv.CSVFormat
import smile.anomaly.IsolationForest
import smile.base.cart.SplitRule
import smile.data.formula.Formula
import smile.io.Read
import smile.nlp.tokenizer.SimpleTokenizer
fun main() {
// --- Load Data ---
val format = CSVFormat.DEFAULT.builder()
.setHeader()
.setSkipHeaderRecord(true)
.get()
val irisData = Read.csv("server/src/main/kotlin/io/jadu/nivi/ai/iris_d.csv", format)
// --- Prepare Data ---
val formula = Formula.lhs("class")
val x = formula.x(irisData).toArray()
println("Training Isolation Forest...")
// Fit the model
val model = IsolationForest.fit(x)
// --- Test Data ---
val normalFlower = doubleArrayOf(5.1, 3.5, 1.4, 0.2)
val mutantFlower = doubleArrayOf(15.0, 10.0, 15.0, 10.0)
// Score > 0.5 usually means "Anomaly".
// Score < 0.5 usually means "Normal".
val normalScore = model.score(normalFlower)
val mutantScore = model.score(mutantFlower)
println("\n--- Results ---")
println("Normal Flower Score: $normalScore")
if (normalScore > 0.5) println("-> Verdict: ANOMALY") else println("-> Verdict: Normal")
println("\nMutant Flower Score: $mutantScore")
if (mutantScore > 0.5) println("-> Verdict: ANOMALY") else println("-> Verdict: Normal")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment