Skip to content

Instantly share code, notes, and snippets.

@Steboss89
Created June 30, 2021 14:43
Show Gist options
  • Save Steboss89/bf1e14baa909a9369f6d04095a011707 to your computer and use it in GitHub Desktop.
Save Steboss89/bf1e14baa909a9369f6d04095a011707 to your computer and use it in GitHub Desktop.
read csv and extract features
pub fn read_csv<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> {
/* Example function to create a dataframe from an input csv file*/
let file = File::open(path).expect("Cannot open file.");
CsvReader::new(file)
.has_header(true)
//.with_delimiter(b' ')
//NB b" " throws an error
.finish()
}
pub fn feature_and_target(in_df: &DataFrame) -> (PolarResult<DataFrame>,PolarResult<DataFrame>) {
/* Read a dataframe, select the columns we need for feature training and target and return
the two new dataframes*/
let features = in_df.select(vec!["crim",
"zn",
"indus",
"chas",
"nox",
"rm",
"age",
"dis",
"rad",
"tax",
"ptratio",
"black",
"lstat"]);
let target = in_df.select("medv");
(features, target)
}
fn main() {
// Read input data
let ifile = "FULLPATHTO/boston_dataset.csv";
let df = read_csv(&ifile).unwrap();
// select features
let (features, target) = feature_and_target(&df);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment