Skip to content

Instantly share code, notes, and snippets.

@calippo
calippo / eblow.py
Last active November 11, 2019 13:21
[scikit-learn/sklearn, pandas] Plot percent of variance explained for KMeans (Elbow Method)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
from sklearn.cluster import KMeans
import numpy as np
from scipy.spatial.distance import cdist, pdist
def elbow(df, n):
kMeansVar = [KMeans(n_clusters=k).fit(df.values) for k in range(1, n)]
centroids = [X.cluster_centers_ for X in kMeansVar]
@calippo
calippo / fillWithMean.py
Last active March 5, 2018 16:31
[pandas] Replace `NaN` values with the mean of the column and remove all the completely empty columns
import pandas as pd
def fillWithMean(df):
return df.fillna(df.mean()).dropna(axis=1, how='all')
@calippo
calippo / varianceSelection.py
Created October 12, 2015 08:33
[pandas, scikit-learn] Feature selection using low variance
def varianceSelection(X, THRESHOLD = .95):
sel = VarianceThreshold(threshold=(THRESHOLD * (1 - THRESHOLD)))
sel.fit_transform(X)
return X[[c for (s, c) in zip(sel.get_support(), X.columns.values) if s]]
@calippo
calippo / Mappable.scala
Created July 27, 2016 21:51
Convert case class to map in shapeless
object Mappable {
implicit class ToMapOps[A](val a: A) extends AnyVal {
import shapeless._
import ops.record._
def toMap[L <: HList](implicit
gen: LabelledGeneric.Aux[A, L],
tmr: ToMap[L]
): Map[String, Any] = {
val m: Map[tmr.Key, tmr.Value] = tmr(gen.to(a))
@calippo
calippo / crane-jupyter-dev.yml
Last active April 22, 2017 10:06
Simple jupyter crane file bootstrap for dev
containers:
env:
image: davidshen84/scikit-learn
run:
publish:
- 8888:8888
interactive: true
tty: true
volume: ["./data:/data", "./notebook:/notebook"]
@calippo
calippo / CaseClassLink.scala
Last active August 29, 2017 11:10
Given a sealed trait A and a case class B, force the case class to have as parameters all and only the known subclasses of A (at compile time with shapeless)
@calippo
calippo / sbt_alias
Last active August 18, 2017 10:56
Alias sbt to log compilation errors
alias sbt='f() { sbt "$@" |tee -a ~/.sbtlogs; };f'
@calippo
calippo / midi.py
Last active November 7, 2017 22:04
python-midi example
import midi #pip install python-midi (https://github.com/vishnubob/python-midi)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
on = midi.NoteOnEvent(tick=0, velocity=100, pitch=60)
track.append(on)
off = midi.NoteOffEvent(tick=100, pitch=60)
track.append(off)
eot = midi.EndOfTrackEvent(tick=1)
@calippo
calippo / goemailgo.sh
Last active March 2, 2018 16:32
Send email from command line
echo “very spoofy” | mail -s “much boring” [email protected]
package intervals
import scala.language.implicitConversions
import cats.Monoid
import cats.syntax.foldable._
import cats.instances.list._
import scala.math.Ordering
object Main extends App {
import Interval._