pub fn save_json<P: AsRef<Path>>(dataset: &Dataset, path: P) -> Result<()>Expand description
Save a dataset to a JSON file
Examples found in repository?
examples/dataset_loaders.rs (line 45)
7fn main() {
8 // Check if a CSV file is provided as a command-line argument
9 let args: Vec<String> = env::args().collect();
10 if args.len() < 2 {
11 println!("Usage: {} <path_to_csv_file>", args[0]);
12 println!("Example: {} examples/sampledata.csv", args[0]);
13 return;
14 }
15
16 let filepath = &args[1];
17
18 // Verify the file exists
19 if !Path::new(filepath).exists() {
20 println!("Error: File '{filepath}' does not exist");
21 return;
22 }
23
24 // Load CSV file
25 println!("Loading CSV file: {filepath}");
26 let csv_config = loaders::CsvConfig {
27 has_header: true,
28 target_column: None,
29 ..Default::default()
30 };
31 match loaders::load_csv(filepath, csv_config) {
32 Ok(dataset) => {
33 print_dataset_info(&dataset, "Loaded CSV");
34
35 // Split the dataset for demonstration
36 println!("\nDemonstrating train-test split...");
37 match train_test_split(&dataset, 0.2, Some(42)) {
38 Ok((train, test)) => {
39 println!("Training set: {} samples", train.n_samples());
40 println!("Test set: {} samples", test.n_samples());
41
42 // Save as JSON for demonstration
43 let jsonpath = format!("{filepath}.json");
44 println!("\nSaving training dataset to JSON: {jsonpath}");
45 if let Err(e) = loaders::save_json(&train, &jsonpath) {
46 println!("Error saving JSON: {e}");
47 } else {
48 println!("Successfully saved JSON file");
49
50 // Load back the JSON file
51 println!("\nLoading back from JSON file...");
52 match loaders::load_json(&jsonpath) {
53 Ok(loaded) => {
54 print_dataset_info(&loaded, "Loaded JSON");
55 }
56 Err(e) => println!("Error loading JSON: {e}"),
57 }
58 }
59 }
60 Err(e) => println!("Error splitting dataset: {e}"),
61 }
62 }
63 Err(e) => println!("Error loading CSV: {e}"),
64 }
65}