44compared for different values of P, center_X, center_Y, scale_X, and scale_Y. The
55results are saved to a CSV file for further analysis.
66
7- Engstrøm, O.-C. G. (2024 ):
8- https://arxiv.org/abs/2401.13185
7+ O.-C. G. Engstrøm and M. H. Jensen (2025 ):
8+ https://analyticalsciencejournals.onlinelibrary.wiley.com/doi/full/10.1002/cem.70008
99
1010Author: Ole-Christian Galbo Engstrøm
11- E-mail: ole.e@di.ku .dk
11+ E-mail: ocge@foss .dk
1212"""
1313
1414import os
2828
2929from cvmatrix .__init__ import __version__
3030from cvmatrix .cvmatrix import CVMatrix
31+ from cvmatrix .partitioner import Partitioner
3132from tests .naive_cvmatrix import NaiveCVMatrix
3233
3334
3435def save_result_to_csv (
35- model , P , N , K , M , center_X , center_Y , scale_X , scale_Y , time , version
36+ model , use_weights , P , N , K , M , center_X , center_Y , scale_X , scale_Y , time , version
3637):
3738 try :
3839 with open ("benchmark_results.csv" , "x" ) as f :
39- f .write ("model,P,N,K,M," "center_X,center_Y,scale_X,scale_Y,time,version\n " )
40+ f .write (
41+ "model,weights,P,N,K,M,"
42+ "center_X,center_Y,scale_X,scale_Y,time,version\n "
43+ )
4044 except FileExistsError :
4145 pass
4246 with open ("benchmark_results.csv" , "a" ) as f :
4347 f .write (
44- f"{ model } ,{ P } ,{ N } ,{ K } ,{ M } ,"
48+ f"{ model } ,{ use_weights } , { P } ,{ N } ,{ K } ,{ M } ,"
4549 f"{ center_X } ,{ center_Y } ,{ scale_X } ,{ scale_Y } ,"
4650 f"{ time } ,{ version } \n "
4751 )
@@ -96,7 +100,6 @@ def execute_algorithm(
96100
97101 # Create the model
98102 model = model_class (
99- folds = cv_splits ,
100103 center_X = center_X ,
101104 center_Y = center_Y ,
102105 scale_X = scale_X ,
@@ -105,12 +108,26 @@ def execute_algorithm(
105108 copy = True ,
106109 )
107110
111+ # Create the validation partitioner
112+ p = Partitioner (folds = cv_splits )
113+
108114 # Fit the model
109115 model .fit (X , Y , weights )
110116
111- # Compute the training set matrices
112- for fold in model .folds_dict .keys ():
113- model .training_XTX_XTY (fold )
117+ if isinstance (model , NaiveCVMatrix ):
118+ # Compute the training set matrices
119+ for fold in p .folds_dict :
120+ # Get the training indices for the current fold
121+ training_indices = np .concatenate (
122+ [p .get_validation_indices (f ) for f in p .folds_dict if f != fold ]
123+ )
124+ model .training_XTX_XTY (training_indices )
125+ else :
126+ # Compute the training set matrices
127+ for fold in p .folds_dict :
128+ # Get the validation indices for the current fold
129+ validation_indices = p .get_validation_indices (fold )
130+ model .training_XTX_XTY (validation_indices )
114131
115132
116133if __name__ == "__main__" :
@@ -122,19 +139,20 @@ def execute_algorithm(
122139 dtype = np .float64 # Data type
123140 X = rng .random ((N , K ), dtype = dtype ) # Random X matrix
124141 Y = rng .random ((N , M ), dtype = dtype ) # Random Y matrix
125- # weights = rng.random((N,), dtype=dtype) # Random weights
126- weights = None
142+ weights = rng .random ((N ,), dtype = dtype ) # Random weights
127143 cv_splits = np .arange (N ) # We can use mod P for P-fold cross-validation
144+ use_weights = [True , False ] # Whether to use weights or not
128145 center_Xs = [True , False ]
129146 center_Ys = [True , False ]
130147 scale_Xs = [True , False ]
131148 scale_Ys = [True , False ]
132149 Ps = [3 , 5 , 10 , 100 , 1000 , 10000 , 100000 ]
133150
134- for center_X , center_Y , scale_X , scale_Y , P in product (
135- center_Xs , center_Ys , scale_Xs , scale_Ys , Ps
151+ for use_w , center_X , center_Y , scale_X , scale_Y , P in product (
152+ use_weights , center_Xs , center_Ys , scale_Xs , scale_Ys , Ps
136153 ):
137154 print (
155+ f"weights={ use_w } , "
138156 f"P={ P } , "
139157 f"center_X={ center_X } , center_Y={ center_Y } , "
140158 f"scale_X={ scale_X } , scale_Y={ scale_Y } , "
@@ -149,13 +167,14 @@ def execute_algorithm(
149167 scale_Y = scale_Y ,
150168 X = X ,
151169 Y = Y ,
152- weights = weights ,
170+ weights = weights if use_weights else None ,
153171 ),
154172 number = 1 ,
155173 )
156174 print (f"CVMatrix, Time: { time :.2f} seconds" )
157175 save_result_to_csv (
158176 "CVMatrix" ,
177+ use_w ,
159178 P ,
160179 N ,
161180 K ,
@@ -191,6 +210,7 @@ def execute_algorithm(
191210 print ()
192211 save_result_to_csv (
193212 "NaiveCVMatrix" ,
213+ use_w ,
194214 P ,
195215 N ,
196216 K ,
0 commit comments