diff --git a/src/ml/f32/read.rs b/src/ml/f32/read.rs index b2f817f..f3fd95a 100644 --- a/src/ml/f32/read.rs +++ b/src/ml/f32/read.rs @@ -3,47 +3,46 @@ use std::fs; pub fn breaker(not_formated_string: &str) -> Vec { // print!("{not_formated_string}"); let mut formated_string: Vec = Vec::new(); - let mut push_str = String::new(); - for i in not_formated_string.chars(){ - if i == '\n'{ - formated_string.push(push_str.clone()); - }else{ - push_str.push(i); + let mut push_str = String::new(); + for i in not_formated_string.chars() { + if i == '\n' { + formated_string.push(push_str.clone()); + } else { + push_str.push(i); } } formated_string } -pub fn count_new_lines(string: &str) -> u16{ +pub fn count_new_lines(string: &str) -> u16 { let mut new_lines = 0; - for i in string.chars(){ - if i == '\n'{ - new_lines+=1; + for i in string.chars() { + if i == '\n' { + new_lines += 1; } } new_lines } -pub fn read_csv(path_of_the_file: &str, how_many_lines_do_want: u8) -> Vec>{ - let data: String = fs::read_to_string(&path_of_the_file).unwrap(); - let data = breaker(&data); - let mut str_to_f32: Vec> = Vec::new(); - for i in data{ - let mut push_vec: Vec = vec![]; - for j in i.chars() { - if j != ','{ - // let push_element: f32 = j - // .to_string() - // .parse() - // .expect("The methmod is not working at line 36\n {}"); - push_vec.push(j - .to_string() - .parse::() - .expect("the method is not working at line 40")); - } +pub fn read_csv(path_of_the_file: &str, how_many_lines_do_want: u8) -> Vec> { + let data: String = fs::read_to_string(&path_of_the_file).unwrap(); + let data = breaker(&data); + let mut str_to_f32: Vec> = Vec::new(); + for i in data { + let mut push_vec: Vec = vec![]; + for j in i.chars() { + if j != ',' { + // let push_element: f32 = j + // .to_string() + // .parse() + // .expect("The methmod is not working at line 36\n {}"); + push_vec.push( + j.to_string() + .parse::() + .expect("the method is not working at line 40"), + ); + } + } + str_to_f32.push(push_vec); } - str_to_f32.push(push_vec); - } str_to_f32 } - - diff --git a/src/ml/f64.rs b/src/ml/f64.rs index 68fc0d3..d5ebdd0 100644 --- a/src/ml/f64.rs +++ b/src/ml/f64.rs @@ -1,6 +1,6 @@ pub mod matrix; +pub mod img; pub mod nn; pub mod read; pub mod activation; pub mod gernerate; - diff --git a/src/ml/f64/gernerate.rs b/src/ml/f64/gernerate.rs index 9ff1b31..de3803b 100644 --- a/src/ml/f64/gernerate.rs +++ b/src/ml/f64/gernerate.rs @@ -1,11 +1,13 @@ extern crate rand; - use rand::Rng; use std::collections::HashMap; pub fn storing_labels<'a>(formated_data: &'a Vec, labels: &mut HashMap) { labels.insert(formated_data[0] as u16, &formated_data[1..]); } + + + pub fn gernerate_imgs<'a>( labels: HashMap>>, range: &u32, diff --git a/src/ml/f64/img.rs b/src/ml/f64/img.rs new file mode 100644 index 0000000..c41d0a9 --- /dev/null +++ b/src/ml/f64/img.rs @@ -0,0 +1,8 @@ +use crate::ml::f64::matrix; + +pub struct Img { + label: u8, + matrix: matrix::MatrixStruct, +} + + diff --git a/src/ml/f64/matrix.rs b/src/ml/f64/matrix.rs index 62e6932..f5edce9 100644 --- a/src/ml/f64/matrix.rs +++ b/src/ml/f64/matrix.rs @@ -1,6 +1,6 @@ pub struct MatrixStruct{ pub rows: u16, - pub columns: u16, + pub columns: u16, pub matrix: Vec>, } pub fn matrix_create<'a>(row: &'a u16, column: &'a u16) -> MatrixStruct{ @@ -11,6 +11,14 @@ pub fn matrix_create<'a>(row: &'a u16, column: &'a u16) -> MatrixStruct{ matrix: vec, } } +pub fn matrix_display(m: &MatrixStruct){ + for i in 0..m.rows as usize{ + for j in 0..m.columns as usize{ + print!("{}", m.matrix[i][j]); + } + println!(); + } +} pub fn positive(f: &f64) -> f64{ if *f<0.0{ return *f * -1_f64; diff --git a/src/ml/f64/read.rs b/src/ml/f64/read.rs index b2f817f..20238c6 100644 --- a/src/ml/f64/read.rs +++ b/src/ml/f64/read.rs @@ -1,49 +1,87 @@ +use crate::ml::f64::img; +use crate::ml::f64::matrix; use std::fs; -pub fn breaker(not_formated_string: &str) -> Vec { - // print!("{not_formated_string}"); - let mut formated_string: Vec = Vec::new(); - let mut push_str = String::new(); - for i in not_formated_string.chars(){ - if i == '\n'{ - formated_string.push(push_str.clone()); - }else{ - push_str.push(i); - } - } - formated_string -} - -pub fn count_new_lines(string: &str) -> u16{ +// pub fn breaker(not_formated_string: &str) -> Vec { +// // print!("{not_formated_string}"); +// let mut formated_string: Vec = Vec::new(); +// let mut push_str = String::new(); +// for i in not_formated_string.chars() { +// if i == '\n' { +// formated_string.push(push_str.clone()); +// } else { +// push_str.push(i); +// } +// } +// formated_string +// } +// +pub fn count_new_lines(string: &str) -> u16 { let mut new_lines = 0; - for i in string.chars(){ - if i == '\n'{ - new_lines+=1; + for i in string.chars() { + if i == '\n' { + new_lines += 1; } } new_lines } -pub fn read_csv(path_of_the_file: &str, how_many_lines_do_want: u8) -> Vec>{ - let data: String = fs::read_to_string(&path_of_the_file).unwrap(); - let data = breaker(&data); - let mut str_to_f32: Vec> = Vec::new(); - for i in data{ - let mut push_vec: Vec = vec![]; - for j in i.chars() { - if j != ','{ - // let push_element: f32 = j - // .to_string() - // .parse() - // .expect("The methmod is not working at line 36\n {}"); - push_vec.push(j - .to_string() - .parse::() - .expect("the method is not working at line 40")); - } +// pub fn matrix_fit(unformated_data: &Vec) -> matrix::MatrixStruct{ +// +// } +// pub fn matrix_fit(unformated_data: &Vec) -> matrix::MatrixStruct{ +// let mut matrix = matrix::matrix_create(&28, &28); +// for i in 0..28{ +// let mut multiplier = 0; +// for j in 0..28{ +// matrix.matrix[i][j] = unformated_data[j+multiplier]; +// } +// multiplier += 28; +// } +// matrix +// } +// pub fn read_csv(path_of_the_file: &str, how_many_lines_do_want: &u8) -> Vec> { +// let data = breaker(&fs::read_to_string(&path_of_the_file).unwrap()); +// let mut counter = 0; +// let mut str_to_f32: Vec> = Vec::new(); +// for i in &data { +// let mut push_vec: Vec = vec![0.0; 28]; +// for j in i.chars() { +// match j { +// j if j != ',' => push_vec.push( +// j.to_string() +// .parse::() +// .expect("the method at line 64 is not working as expected"), +// ), +// '\n' => counter += 1, +// _ => (), +// } +// if counter == *how_many_lines_do_want { +// break; +// } +// } +// str_to_f32.push(push_vec); +// } +// str_to_f32 +// } +pub fn line(line_no: &u16, lines: &std::str::Lines) -> String{ + let mut str = ""; + for i in 0..*line_no{ + if i == line_no -1 { + str = lines.next().unwrap(); + }else { + lines.next(); + } + } + str.to_string() +} +pub fn read_csv(path_of_the_file: &str, lines: &u8) -> matrix::MatrixStruct { + let imgs: Vec> = Vec::with_capacity(*lines as usize); + static MAXCHAR: u16 = 10000; + let lines: String = (fs::read_to_string(path_of_the_file).unwrap()).lines(); + let mut i: u16 = 0; + while i < *lines { + let j = 0; + x + i += 1; } - str_to_f32.push(push_vec); - } - str_to_f32 } - -