@@ -111,6 +111,65 @@ pub(crate) fn deserialize_data(
111111 Ok ( ( x, y, num_samples, num_features) )
112112}
113113
114+ impl < X : Copy + std:: fmt:: Debug , Y : Copy + std:: fmt:: Debug > std:: fmt:: Display for Dataset < X , Y > {
115+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
116+ struct Target < Y > {
117+ name : String ,
118+ value : Y ,
119+ }
120+ struct Feature < X > {
121+ name : String ,
122+ value : X ,
123+ }
124+ struct DataPoint < X , Y > {
125+ labels : Vec < Target < Y > > ,
126+ features : Vec < Feature < X > > ,
127+ }
128+ impl < X : Copy + std:: fmt:: Debug , Y : Copy + std:: fmt:: Debug > std:: fmt:: Display for DataPoint < X , Y > {
129+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
130+ write ! (
131+ f,
132+ "{} : {}" ,
133+ self . labels
134+ . iter( )
135+ . map( |target| format!( "{}:{:?}, " , target. name, target. value) )
136+ . collect:: <String >( ) ,
137+ self . features
138+ . iter( )
139+ . map( |feature| format!( "{}:{:?}, " , feature. name, feature. value) )
140+ . collect:: <String >( )
141+ )
142+ }
143+ }
144+ let mut datapoints = Vec :: new ( ) ;
145+ for sample_index in 0 ..self . num_samples {
146+ let mut features = Vec :: new ( ) ;
147+ for feature_index in 0 ..self . feature_names . len ( ) {
148+ features. push ( Feature {
149+ name : self . feature_names [ feature_index] . to_owned ( ) ,
150+ value : self . data [ sample_index * self . num_features + feature_index] ,
151+ } ) ;
152+ }
153+ let mut targets = Vec :: new ( ) ;
154+ for target_index in 0 ..self . target_names . len ( ) {
155+ targets. push ( Target {
156+ name : self . target_names [ target_index] . to_owned ( ) ,
157+ value : self . target [ sample_index * self . target_names . len ( ) + target_index] ,
158+ } ) ;
159+ }
160+ datapoints. push ( DataPoint {
161+ labels : targets,
162+ features,
163+ } )
164+ }
165+ let mut out = format ! ( "{}\n " , self . description) ;
166+ for point in datapoints {
167+ out. push_str ( & format ! ( "{}\n " , point) ) ;
168+ }
169+ write ! ( f, "{}" , out)
170+ }
171+ }
172+
114173#[ cfg( test) ]
115174mod tests {
116175 use super :: * ;
@@ -133,4 +192,10 @@ mod tests {
133192 assert_eq ! ( m[ 0 ] . len( ) , 5 ) ;
134193 assert_eq ! ( * m[ 1 ] [ 3 ] , 9 ) ;
135194 }
195+
196+ #[ test]
197+ fn display ( ) {
198+ let dataset = iris:: load_dataset ( ) ;
199+ println ! ( "{}" , dataset) ;
200+ }
136201}
0 commit comments