Skip to content

Instantly share code, notes, and snippets.

View archena's full-sized avatar
🐝

Matt Squire archena

🐝
View GitHub Profile
%load_ext autoreload
%autoreload 2
import sys
sys.path.append("../")
import components.data
import components.features
import components.training
def fit(train_X, train_y):
rf_model = RandomForestRegressor(random_state=1, n_estimators=10)
rf_model.fit(train_X, train_y)
return rf_model
def validate(model, val_X, val_y):
rf_val_predictions = model.predict(val_X)
return mean_absolute_error(rf_val_predictions, val_y)
rf_model = fit(train_X, train_y)
rf_model = RandomForestRegressor(random_state=1, n_estimators=10)
rf_model.fit(train_X, train_y)
rf_val_predictions = rf_model.predict(val_X)
rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)
print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
/**
* We can't write 'new T()' in Java, and it wouldn't be typesafe if we could. Some people blame type erasure for this, but
* the real problem is that we haven't specified that T has a zero-argument constructor, we just assumed it.
* To do it properly, encode the constructors through generics. No runtime type information needed...
*/
interface Constructor0<T> {
T make();
}
boolean r = isOrdered(Arrays.asList(1, 3, 5, 7)); // true
boolean r2 = isOrdered(Arrays.asList(1, 5, 5, 7)); // false
boolean r3 = isOrdered(Arrays.asList(5, 3, 6, 7)); // false
public static <T extends Comparable<T>> boolean isOrdered(Collection<T> collection) {
final Iterator<T> i = collection.iterator();
final Iterator<T> j = collection.iterator();
j.next();
while(j.hasNext())
@archena
archena / bad-loops.scala
Created January 16, 2014 18:16
Some silly loops and perfectly sensible capture semantics. Based on an example of Erik Meijer, translated from c#. Normal people use the `for(i <- x)` style loop in Scala.
def loopA = {
// 0 1 2 3
var i = 0
while(i < 4) {
print(i)
i = i + 1
}
println("")
}
interface Semigroup<T> {
T apply(T a, T b);
}
interface Monoid<T> extends Semigroup<T> {
T identity();
}
interface Group<T> extends Monoid<T> {
T inverse(T a);
@archena
archena / reifiable.java
Created October 15, 2013 16:53
Reified generics in Java
class ReifiableType<T> {
final T value;
final Class<T> type;
public ReifiableType(T value, Class<T> type) {
this.value = value;
this.type = type;
}
}
@archena
archena / generators.java
Last active October 20, 2023 21:17
Generators in Java via Iterator
import java.util.Iterator;
class GeneratorTest {
public static void main(String[] args) {
/**
* A generator for Fibonacci numbers
*/
Generator<Integer> g = new Generator<>(new Sequencer<Integer>() {
private int a = 1, b = 1;
public Integer get() {