Skip to content

Instantly share code, notes, and snippets.

@oem
oem / aoc2022-1.k
Last active December 1, 2022 09:41
|/n:+/'1_'(&^n)_n:`I$'0:"i/1"
+/n@3#>n
@oem
oem / day_6.jl
Created December 7, 2020 11:18
day 6, advent of code 2020, using Pipe
using Pipe: @pipe
getdata(file) = open(joinpath(@__DIR__, file)) do f read(f, String) end
part1(data) = @pipe data |>
split(_, "\n\n") |>
map(l -> replace(l, "\n" => ""), _) |>
map(l -> split(l, ""), _) |>
map(l -> unique(l), _) |>
map(l -> length(l), _) |>
@oem
oem / test.py
Created July 4, 2018 11:25
sanity checking our trained beer classifier
import turicreate as tc
import os
current_dir = os.path.dirname(__file__)
model = tc.load_model(os.path.join(
current_dir, 'model/v1.model'))
images = tc.load_images(os.path.join(current_dir, 'data/test'))
images['predictions'] = model.predict(images)
images.print_rows(num_rows=10)
@oem
oem / train.py
Last active July 4, 2018 09:22
trains the beer classifier
import turicreate as tc
import os
current_dir = os.path.dirname(__file__)
data = tc.SFrame(os.path.join(current_dir, 'data/processed/beers.sframe'))
train, test = data.random_split(0.8)
model = tc.image_classifier.create(train, target='label', max_iterations=100)
@oem
oem / prepare_data.py
Last active July 4, 2018 08:51
labelling the beer dataset for our beer-o-mat app
import turicreate as tc
import re
import os
def path_as_label(path):
label = re.search('(?<=raw/)(.+)?/.+$', path).group(1)
label = label.replace('_', ' ')
return label.title()
require 'minitest/autorun'
require 'matrix'
module LinearRegression
def predict(features, theta)
features * theta
end
def cost_function(features, labels, theta)
m = features.row_count
def normal_equation(features, labels)
(features.transpose * features).inverse * features.transpose * labels
end
@oem
oem / cost_function.rb
Created April 15, 2018 11:00
cost function for linear regression
# features is a Matrix, labels and theta are Vectors
def cost_function(features, labels, theta)
m = features.row_count
predictions = predict(features, theta)
squared_errors = (predictions - labels).map { |error| error**2 }
(1.0 / (2.0 * m)) * squared_errors.reduce { |a, b| a + b }
end
@oem
oem / predict_terminator_battle.rb
Created April 15, 2018 10:32
matrix multiplication to predict all labels in one go
require 'matrix'
Matrix[[10, 1000, 10], [5, 800, 2], [12, 2500, 3]] * Vector[200,-0.1,-10] # => Vector[1800.0, 900.0, 2120.0]
@oem
oem / main.go
Created January 22, 2016 23:09
package main
import (
"fmt"
"time"
)
const timeFormat = "2006-01-02 15:04 MST"
func main() {