Skip to content

Instantly share code, notes, and snippets.

View Steboss89's full-sized avatar

Stefano Bosisio Steboss89

View GitHub Profile
@Steboss89
Steboss89 / lin_reg_import.rs
Created June 10, 2021 13:25
Import libraries for linear regression
// import rusty_machine for linear regression
// as well as rust_machine Matrix and Vector as we need them for input
use rusty_machine;
use rusty_machine::linalg::Matrix; // surprise, Matrix is in linear algebra
use rusty_machine::linalg::BaseMatrix; // implement traits for matrix
use rusty_machine::linalg::Vector;
use rusty_machine::learning::lin_reg::LinRegressor;
use rusty_machine::learning::SupModel; // this is extremelly important
// as this implement the traits to user predict and train
//use smartcore::metrics::*;
@Steboss89
Steboss89 / lin_reg_preproc.rs
Created June 10, 2021 13:41
Preprocess in linear regression
// Read input data
let ifile = "FULLPATHTODATASET";
let mut input_data = read_housing_csv(&ifile);
// data preprocessing: train test split
// define the length of the test data
let test_chunk_size: f64 = input_data.len() as f64 * 0.3; // we are taking the 30% as test data
// as cast between types
let test_chunk_size = test_chunk_size.round() as usize;
// split
@Steboss89
Steboss89 / lin_reg_model.rs
Created June 10, 2021 13:45
linear Regression model in rusty_machine
// MODEL!
let mut linearRegression = LinRegressor::default();
// train
linearRegression.train(&x_train_matrix, &y_train_vector);
// predictions
let preds = linearRegression.predict(&x_test_matrix).unwrap();
// convert to matrix both preds and y_test
let preds_matrix = Matrix::new(test_size, 1, preds);
let y_preds_matrix = Matrix::new(test_size, 1, y_test);
// compute the mse
@Steboss89
Steboss89 / main.rs
Created June 10, 2021 15:06
Main routine for linear regression
use std::vec::Vec;
use std::env::args;
mod linear_regression;
fn main() {
println!("Hello, world!");
linear_regression::run();
}
@Steboss89
Steboss89 / read_housing_csv.rs
Created June 10, 2021 15:13
Declare the reading csv function
pub fn read_housing_csv(filename: impl AsRef<Path>) -> Vec<HousingDataset>
@Steboss89
Steboss89 / create_search_space.py
Created June 14, 2021 21:55
Create the search space
def create_space():
r""" Function to create the search space"""
nodes = [8, 16, 32, 64, 128, 256, 512]
activations = ['relu', 'softmax', 'softplus', 'tanh', 'selu', 'sigmoid']
# build models
known_models = [tf.keras.layers.LSTM,
tf.keras.layer.GRU,
tf.keras.layer.Bidirectional]
for i in range(len(nodes)):
@Steboss89
Steboss89 / search_strategy.py
Created June 16, 2021 10:30
Possible approach to implement the search strategy
def search_strategy():
r""" Function to combine search space bits together
and create a new model"""
# retrieve the output from search space
space_output = search_space()
# from here use a suitable technique to retrieve new models
models = MonteCarloSearchTree(space_output)
# alternatively we could build something like
model = Sequential()
@Steboss89
Steboss89 / Cargo.toml
Created June 28, 2021 14:34
Polars, let's learn dataframes
[package]
name = "polars_learning"
version = "0.1.0"
authors = ["Steboss89"]
edition = "2018"
[dependencies]
polars = "0.14.2"
@Steboss89
Steboss89 / read_csv.rs
Created June 28, 2021 14:39
Read a csv file and return a dataframe
use polars::prelude::*;//{CsvReader, DataType, Field, Result as PolarResult, Schema, DataFrame,};
use polars::prelude::{Result as PolarResult};
use polars::frame::DataFrame;
use std::fs::File;
use std::path::{Path};
// an eye on traits:
use polars::prelude::SerReader;
// If we don't import this one we might get the error CsvReader::new(file) new function not found in CsvReader
pub fn read_csv<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> {
@Steboss89
Steboss89 / shape.rs
Created June 28, 2021 14:46
Get shape and info from a dataframe
pub fn deal_with_shape<P: AsRef<Path>>(path: P) -> () {
/* Example function to retrieve shape info from a dataframe */
let df = read_csv(&path).unwrap();
// shape
// reming {:#?} otherwise error ^^^^^ `(usize, usize)` cannot be formatted with the default formatter
let shape = df.shape();
println!("{:#?}", shape);
// schema
println!("{:#?}", df.schema());
// dtypes