@@ -134,3 +134,62 @@ mod tests {
134
134
assert_eq ! ( * m[ 1 ] [ 3 ] , 9 ) ;
135
135
}
136
136
}
137
+
138
+ impl < X : Copy + std:: fmt:: Debug , Y : Copy + std:: fmt:: Debug > std:: fmt:: Display for Dataset < X , Y > {
139
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
140
+ struct Target < Y > {
141
+ name : String ,
142
+ value : Y ,
143
+ }
144
+ struct Feature < X > {
145
+ name : String ,
146
+ value : X ,
147
+ }
148
+ struct DataPoint < X , Y > {
149
+ labels : Vec < Target < Y > > ,
150
+ features : Vec < Feature < X > > ,
151
+ }
152
+ impl < X : Copy + std:: fmt:: Debug , Y : Copy + std:: fmt:: Debug > std:: fmt:: Display for DataPoint < X , Y > {
153
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
154
+ write ! (
155
+ f,
156
+ "{} : {}" ,
157
+ self . labels
158
+ . iter( )
159
+ . map( |target| format!( "{}:{:?}, " , target. name, target. value) )
160
+ . collect:: <String >( ) ,
161
+ self . features
162
+ . iter( )
163
+ . map( |feature| format!( "{}:{:?}, " , feature. name, feature. value) )
164
+ . collect:: <String >( )
165
+ )
166
+ }
167
+ }
168
+ let mut datapoints = Vec :: new ( ) ;
169
+ for sample_index in 0 ..self . num_samples {
170
+ let mut features = Vec :: new ( ) ;
171
+ for feature_index in 0 ..self . feature_names . len ( ) {
172
+ features. push ( Feature {
173
+ name : self . feature_names [ feature_index] . to_owned ( ) ,
174
+ value : self . data [ sample_index * self . num_features + feature_index] ,
175
+ } ) ;
176
+ }
177
+ let mut targets = Vec :: new ( ) ;
178
+ for target_index in 0 ..self . target_names . len ( ) {
179
+ targets. push ( Target {
180
+ name : self . target_names [ target_index] . to_owned ( ) ,
181
+ value : self . target [ sample_index * self . target_names . len ( ) + target_index] ,
182
+ } ) ;
183
+ }
184
+ datapoints. push ( DataPoint {
185
+ labels : targets,
186
+ features,
187
+ } )
188
+ }
189
+ let mut out = format ! ( "{}\n " , self . description) ;
190
+ for point in datapoints {
191
+ out. push_str ( & format ! ( "{}\n " , point) ) ;
192
+ }
193
+ write ! ( f, "{}" , out)
194
+ }
195
+ }
0 commit comments