Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jayhuang75/a05ba695d8b40dc528543d7a84ac389a to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/a05ba695d8b40dc528543d7a84ac389a to your computer and use it in GitHub Desktop.
go-train-delay-ml-pipeline-feature-target-convert
async fn feature_to_matrix(&self, df: &DataFrame) -> Result<DenseMatrix<f64>, AppError> {
/* function to convert feature dataframe to a DenseMatrix, readable by smartcore*/
let nrows = df.height();
let ncols = df.width();
// convert to array
let features_res = df.to_ndarray::<Float64Type>()?;
// create a zero matrix and populate with features
let mut xmatrix: DenseMatrix<f64> = BaseMatrix::zeros(nrows, ncols);
// populate the matrix
// initialize row and column counters
let mut col: u32 = 0;
let mut row: u32 = 0;
for val in features_res.iter() {
// Debug
//println!("{},{}", usize::try_from(row).unwrap(), usize::try_from(col).unwrap());
// define the row and col in the final matrix as usize
let m_row = usize::try_from(row)?;
let m_col = usize::try_from(col)?;
// NB we are dereferencing the borrow with *val otherwise we would have a &val type, which is
// not what set wants
xmatrix.set(m_row, m_col, *val);
// check what we have to update
if m_col == ncols - 1 {
row += 1;
col = 0;
} else {
col += 1;
}
}
// Ok so we can return DenseMatrix, otherwise we'll have std::result::Result<Densematrix, PolarsError>
Ok(xmatrix)
}
async fn target_to_ndarray(&self, df: &DataFrame) -> Result<Vec<f64>, AppError> {
let target_array = df.to_ndarray::<Float64Type>()?;
// create a vec type and populate with y values
let mut y: Vec<f64> = Vec::new();
for val in target_array.iter() {
y.push(*val);
}
Ok(y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment