@ -6,7 +6,8 @@ import 'package:ml_dataframe/ml_dataframe.dart';
void main ( ) {
test ( ' LinearRegressor basic sanity check ' , ( ) {
/ / Simple: y = 2 x + 100
final data = DataFrame ( [
final data = DataFrame (
[
[ 1.0 , 102.0 ] ,
[ 2.0 , 104.0 ] ,
[ 3.0 , 106.0 ] ,
@ -17,7 +18,10 @@ void main() {
[ 50.0 , 200.0 ] ,
[ 0.0 , 100.0 ] ,
[ 100.0 , 300.0 ] ,
] , headerExists: false , header: [ ' x ' , ' y ' ] ) ;
] ,
headerExists: false ,
header: [ ' x ' , ' y ' ] ,
) ;
debugPrint ( ' Training data columns: ${ data . header } ' ) ;
debugPrint ( ' Training data rows: ${ data . rows . length } ' ) ;
@ -25,7 +29,9 @@ void main() {
final model = LinearRegressor ( data , ' y ' ) ;
final testDf = DataFrame (
[ [ 25.0 ] ] ,
[
[ 25.0 ] ,
] ,
headerExists: false ,
header: [ ' x ' ] ,
) ;
@ -38,28 +44,41 @@ void main() {
test ( ' LinearRegressor multi-feature with constant column produces zeros ' , ( ) {
/ / isFlood = 0 for all rows → zero - variance column → singular matrix
final data = DataFrame ( [
final data = DataFrame (
[
[ 0.0 , 50.0 , 14.0 , 0.0 , 1900.0 ] ,
[ 0.0 , 80.0 , 14.0 , 0.0 , 2200.0 ] ,
[ 2.0 , 50.0 , 14.0 , 0.0 , 5000.0 ] ,
[ 4.0 , 50.0 , 14.0 , 0.0 , 9500.0 ] ,
] , headerExists: false , header: [
' pathLength ' , ' messageBytes ' , ' hourOfDay ' , ' isFlood ' , ' deliveryMs ' ,
] ) ;
] ,
headerExists: false ,
header: [
' pathLength ' ,
' messageBytes ' ,
' hourOfDay ' ,
' isFlood ' ,
' deliveryMs ' ,
] ,
) ;
final model = LinearRegressor ( data , ' deliveryMs ' ) ;
final testDf = DataFrame (
[ [ 2.0 , 50.0 , 14.0 , 0.0 ] ] ,
[
[ 2.0 , 50.0 , 14.0 , 0.0 ] ,
] ,
headerExists: false ,
header: [ ' pathLength ' , ' messageBytes ' , ' hourOfDay ' , ' isFlood ' ] ,
) ;
final pred = model . predict ( testDf ) . rows . first . first ;
debugPrint ( ' With constant isFlood column: hops=2 → ${ ( pred as num ) . round ( ) } ms (likely 0) ' ) ;
debugPrint (
' With constant isFlood column: hops=2 → ${ ( pred as num ) . round ( ) } ms (likely 0) ' ,
) ;
} ) ;
test ( ' LinearRegressor 2-feature works correctly ' , ( ) {
/ / Just pathLength + messageBytes → deliveryMs
final data = DataFrame ( [
final data = DataFrame (
[
[ 0.0 , 50.0 , 1900.0 ] ,
[ 0.0 , 80.0 , 2200.0 ] ,
[ 2.0 , 50.0 , 5000.0 ] ,
@ -70,13 +89,18 @@ void main() {
[ 2.0 , 30.0 , 4800.0 ] ,
[ 4.0 , 30.0 , 9000.0 ] ,
[ 0.0 , 60.0 , 2000.0 ] ,
] , headerExists: false , header: [ ' pathLength ' , ' messageBytes ' , ' deliveryMs ' ] ) ;
] ,
headerExists: false ,
header: [ ' pathLength ' , ' messageBytes ' , ' deliveryMs ' ] ,
) ;
final model = LinearRegressor ( data , ' deliveryMs ' ) ;
for ( final hops in [ 0.0 , 2.0 , 4.0 ] ) {
final testDf = DataFrame (
[ [ hops , 50.0 ] ] ,
[
[ hops , 50.0 ] ,
] ,
headerExists: false ,
header: [ ' pathLength ' , ' messageBytes ' ] ,
) ;
@ -87,7 +111,8 @@ void main() {
test ( ' LinearRegressor multi-feature with variance in all columns ' , ( ) {
/ / Mix flood and direct so isFlood has variance
final data = DataFrame ( [
final data = DataFrame (
[
[ 0.0 , 50.0 , 14.0 , 0.0 , 1900.0 ] ,
[ 0.0 , 80.0 , 10.0 , 0.0 , 2200.0 ] ,
[ 2.0 , 50.0 , 16.0 , 0.0 , 5000.0 ] ,
@ -98,9 +123,16 @@ void main() {
[ - 1.0 , 60.0 , 18.0 , 1.0 , 6500.0 ] ,
[ - 1.0 , 30.0 , 10.0 , 1.0 , 4000.0 ] ,
[ - 1.0 , 80.0 , 22.0 , 1.0 , 7000.0 ] ,
] , headerExists: false , header: [
' pathLength ' , ' messageBytes ' , ' hourOfDay ' , ' isFlood ' , ' deliveryMs ' ,
] ) ;
] ,
headerExists: false ,
header: [
' pathLength ' ,
' messageBytes ' ,
' hourOfDay ' ,
' isFlood ' ,
' deliveryMs ' ,
] ,
) ;
final model = LinearRegressor ( data , ' deliveryMs ' ) ;
@ -116,7 +148,9 @@ void main() {
header: [ ' pathLength ' , ' messageBytes ' , ' hourOfDay ' , ' isFlood ' ] ,
) ;
final pred = model . predict ( testDf ) . rows . first . first ;
debugPrint ( ' 4-feature: hops= ${ tc [ 0 ] } flood= ${ tc [ 3 ] } → ${ ( pred as num ) . round ( ) } ms ' ) ;
debugPrint (
' 4-feature: hops= ${ tc [ 0 ] } flood= ${ tc [ 3 ] } → ${ ( pred as num ) . round ( ) } ms ' ,
) ;
}
} ) ;
}