@@ -23,13 +23,11 @@ object LogisticRegression:
2323 def logits (input : Tensor1 [Feature , Float ]): Tensor0 [Float ] = linear(input)
2424 def probits (input : Tensor1 [Feature , Float ]): Tensor0 [Float ] = sigmoid(logits(input))
2525 def apply (input : Tensor1 [Feature , Float ]): Tensor0 [Boolean ] = logits(input) >= Tensor0 (0f )
26- def main (args : Array [String ]): Unit =
2726
28- import io .github .quafadas .table .*
29- val df = CSV
30- .resource(" penguins.csv" , TypeInferrer .FromAllRows )
31- .filter(row => ! (row.species == 2 ))
32- .toSeq
27+ def main (args : Array [String ]): Unit =
28+
29+ val df = PenguinCSV .parse(" ./data/penguins.csv" )
30+ .filter(row => row.species != 2 )
3331
3432 val dfShuffled = scala.util.Random .shuffle(df)
3533
@@ -41,7 +39,7 @@ object LogisticRegression:
4139 row.body_mass_g.toFloat
4240 )
4341 }.toArray
44- val labelData = dfShuffled.column[ " species" ] .toArray.map {
42+ val labelData = dfShuffled.map(_. species) .toArray.map {
4543 case 1 => true
4644 case 0 => false
4745 }
@@ -114,4 +112,29 @@ object LogisticRegression:
114112 println(predictions)
115113 val predictionClasses = trainingData.vmap(Axis [Sample ])(x => finalModel(x))
116114
117- println(" \n Training complete. Optimized parameters:" + finalParams)
115+ println(" \n Training complete. Optimized parameters:" + finalParams)
116+
117+ object PenguinCSV :
118+ case class Row (
119+ species : Int ,
120+ bill_length_mm : Double ,
121+ bill_depth_mm : Double ,
122+ flipper_length_mm : Double ,
123+ body_mass_g : Double
124+ )
125+
126+ def parse (path : String ): Seq [Row ] =
127+ val source = scala.io.Source .fromFile(path)
128+ try
129+ val lines = source.getLines().toSeq
130+ lines.drop(1 ).map { line =>
131+ val parts = line.split(" ," )
132+ Row (
133+ species = parts(1 ).toInt,
134+ bill_length_mm = parts(2 ).toDouble,
135+ bill_depth_mm = parts(3 ).toDouble,
136+ flipper_length_mm = parts(4 ).toDouble,
137+ body_mass_g = parts(5 ).toDouble
138+ )
139+ }.toSeq
140+ finally source.close()
0 commit comments