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