Skip to content

Instantly share code, notes, and snippets.

View Steboss89's full-sized avatar

Stefano Bosisio Steboss89

View GitHub Profile
@Steboss89
Steboss89 / AvgCumSum.py
Created February 22, 2021 10:49
Compute an averaged cumulative sum of a signal X
avg_x = np.mean(X)
Xt = []
for i, val in enumerate(X, 0):
if i==0:
Xt.append(val - avg_x)
else:
Xt.append(Xt[i-1] + (val - avg_x))
reader.lines().enumerate()
.map(|(numb, line)| line.expect(&format!("Impossible to read line number {}", numb)))
.map(|row| read_single_line(row))
.collect()
@Steboss89
Steboss89 / read_housing_csv.rs
Created June 8, 2021 15:58
Read a csv in Rust. First step map out the lines
reader.lines().enumerate()
.map(|(numb, line)| line.expect(&format!("Impossible to read line number {}", numb)))
.map(|row| read_single_line(row))
.collect()
@Steboss89
Steboss89 / HousingDataset.rs
Created June 8, 2021 16:01
Impl of HousingDataset
pub fn new(v: Vec<&str>) -> HousingDataset {
// note we have declared a new vector of &strings, remember borrowship, which will be a struct HousingDatset
// as input we are receiving a string from the csv file.
// we are going to decode this as a f64 vector using the unwrap function
let unwrapped_text: Vec<f64> = v.iter().map(|r| r.parse().unwrap()).collect();
@Steboss89
Steboss89 / Cargo.toml
Created June 8, 2021 16:10
Cargo for read_csv
[lib]
name = "read_csv"
path = "src/reader.rs"
struct Person {
name: String, // NB there's a comma here
surname: String,
age: u32
}
impl Person{
// method to get the person
fn who_are_you(&self){
@Steboss89
Steboss89 / traitimpl.rs
Created June 9, 2021 08:47
Trait with inherent implementation
struct Person {
name: String, // NB there's a comma here
surname: String,
age: u32
}
// notice now we are defining impl METHOD for TYPE
impl WhoAreYou for Person {
fn who_are_you(&self) -> String{
return format!("Person name is {} surname {} and age {}", self.name, self.surname, self.age);
@Steboss89
Steboss89 / traitIndependent.rs
Created June 9, 2021 08:56
Independent implementation of a trait
struct Person {
name: String, // NB there's a comma here
surname: String,
age: u32
}
// Define a trait with the functionalities we want to use with a Person
trait PersonSpec {
fn compute_year_of_birth(&self) -> u32;
}
@Steboss89
Steboss89 / main_for_trait.rs
Created June 9, 2021 08:58
Use an independent trait in main
fn main() {
let myself = Person{ name: "Stefano".to_string(),
surname: "Bosisio".to_string(),
age: 32 // not yet :P
};
println!("I was born in the {}", myself.compute_year_of_birth());
}
@Steboss89
Steboss89 / Cargo.toml
Created June 9, 2021 11:44
Completed Cargo file
[package]
name = "rust-regression"
version = "0.1.0"
authors = ["Steboss89"]
edition = "2018"
[dependencies]
# to get the rusty-machine simply go to crates.io and copy the version you want
rusty-machine = "0.5.4"